custom_user.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from django.db import models
  2. from django.contrib.auth.models import (
  3. BaseUserManager,
  4. AbstractBaseUser,
  5. AbstractUser,
  6. UserManager,
  7. PermissionsMixin,
  8. Group,
  9. Permission,
  10. )
  11. # The custom User uses email as the unique identifier, and requires
  12. # that every user provide a date of birth. This lets us test
  13. # changes in username datatype, and non-text required fields.
  14. class CustomUserManager(BaseUserManager):
  15. def create_user(self, email, date_of_birth, password=None):
  16. """
  17. Creates and saves a User with the given email and password.
  18. """
  19. if not email:
  20. raise ValueError('Users must have an email address')
  21. user = self.model(
  22. email=self.normalize_email(email),
  23. date_of_birth=date_of_birth,
  24. )
  25. user.set_password(password)
  26. user.save(using=self._db)
  27. return user
  28. def create_superuser(self, email, password, date_of_birth):
  29. u = self.create_user(email, password=password, date_of_birth=date_of_birth)
  30. u.is_admin = True
  31. u.save(using=self._db)
  32. return u
  33. class CustomUser(AbstractBaseUser):
  34. email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
  35. is_active = models.BooleanField(default=True)
  36. is_admin = models.BooleanField(default=False)
  37. date_of_birth = models.DateField()
  38. custom_objects = CustomUserManager()
  39. USERNAME_FIELD = 'email'
  40. REQUIRED_FIELDS = ['date_of_birth']
  41. class Meta:
  42. app_label = 'auth'
  43. def get_full_name(self):
  44. return self.email
  45. def get_short_name(self):
  46. return self.email
  47. def __unicode__(self):
  48. return self.email
  49. # Maybe required?
  50. def get_group_permissions(self, obj=None):
  51. return set()
  52. def get_all_permissions(self, obj=None):
  53. return set()
  54. def has_perm(self, perm, obj=None):
  55. return True
  56. def has_perms(self, perm_list, obj=None):
  57. return True
  58. def has_module_perms(self, app_label):
  59. return True
  60. # Admin required fields
  61. @property
  62. def is_staff(self):
  63. return self.is_admin
  64. # At this point, temporarily remove the groups and user_permissions M2M
  65. # fields from the AbstractUser class, so they don't clash with the related_name
  66. # that sets.
  67. old_au_local_m2m = AbstractUser._meta.local_many_to_many
  68. old_pm_local_m2m = PermissionsMixin._meta.local_many_to_many
  69. groups = models.ManyToManyField(Group, blank=True)
  70. groups.contribute_to_class(PermissionsMixin, "groups")
  71. user_permissions = models.ManyToManyField(Permission, blank=True)
  72. user_permissions.contribute_to_class(PermissionsMixin, "user_permissions")
  73. PermissionsMixin._meta.local_many_to_many = [groups, user_permissions]
  74. AbstractUser._meta.local_many_to_many = [groups, user_permissions]
  75. # The extension user is a simple extension of the built-in user class,
  76. # adding a required date_of_birth field. This allows us to check for
  77. # any hard references to the name "User" in forms/handlers etc.
  78. class ExtensionUser(AbstractUser):
  79. date_of_birth = models.DateField()
  80. custom_objects = UserManager()
  81. REQUIRED_FIELDS = AbstractUser.REQUIRED_FIELDS + ['date_of_birth']
  82. class Meta:
  83. app_label = 'auth'
  84. # The CustomPermissionsUser users email as the identifier, but uses the normal
  85. # Django permissions model. This allows us to check that the PermissionsMixin
  86. # includes everything that is needed to interact with the ModelBackend.
  87. class CustomPermissionsUserManager(CustomUserManager):
  88. def create_superuser(self, email, password, date_of_birth):
  89. u = self.create_user(email, password=password, date_of_birth=date_of_birth)
  90. u.is_superuser = True
  91. u.save(using=self._db)
  92. return u
  93. class CustomPermissionsUser(AbstractBaseUser, PermissionsMixin):
  94. email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
  95. date_of_birth = models.DateField()
  96. custom_objects = CustomPermissionsUserManager()
  97. USERNAME_FIELD = 'email'
  98. REQUIRED_FIELDS = ['date_of_birth']
  99. class Meta:
  100. app_label = 'auth'
  101. def get_full_name(self):
  102. return self.email
  103. def get_short_name(self):
  104. return self.email
  105. def __unicode__(self):
  106. return self.email
  107. class IsActiveTestUser1(AbstractBaseUser):
  108. """
  109. This test user class and derivatives test the default is_active behavior
  110. """
  111. username = models.CharField(max_length=30, unique=True)
  112. custom_objects = BaseUserManager()
  113. USERNAME_FIELD = 'username'
  114. class Meta:
  115. app_label = 'auth'
  116. # the is_active attr is provided by AbstractBaseUser
  117. class CustomUserNonUniqueUsername(AbstractBaseUser):
  118. "A user with a non-unique username"
  119. username = models.CharField(max_length=30)
  120. USERNAME_FIELD = 'username'
  121. class Meta:
  122. app_label = 'auth'
  123. class CustomUserNonListRequiredFields(AbstractBaseUser):
  124. "A user with a non-list REQUIRED_FIELDS"
  125. username = models.CharField(max_length=30, unique=True)
  126. date_of_birth = models.DateField()
  127. USERNAME_FIELD = 'username'
  128. REQUIRED_FIELDS = 'date_of_birth'
  129. class Meta:
  130. app_label = 'auth'
  131. class CustomUserBadRequiredFields(AbstractBaseUser):
  132. "A user with a non-unique username"
  133. username = models.CharField(max_length=30, unique=True)
  134. date_of_birth = models.DateField()
  135. USERNAME_FIELD = 'username'
  136. REQUIRED_FIELDS = ['username', 'date_of_birth']
  137. class Meta:
  138. app_label = 'auth'
  139. # Undo swap hack
  140. AbstractUser._meta.local_many_to_many = old_au_local_m2m
  141. PermissionsMixin._meta.local_many_to_many = old_pm_local_m2m