| 107 | | class UserManager(models.Manager): |
|---|
| 108 | | def create_user(self, username, email, password=None): |
|---|
| 109 | | "Creates and saves a User with the given username, e-mail and password." |
|---|
| 110 | | now = datetime.datetime.now() |
|---|
| 111 | | user = self.model(None, username, '', '', email.strip().lower(), 'placeholder', False, True, False, now, now) |
|---|
| 112 | | if password: |
|---|
| 113 | | user.set_password(password) |
|---|
| 114 | | else: |
|---|
| 115 | | user.set_unusable_password() |
|---|
| 116 | | user.save() |
|---|
| 117 | | return user |
|---|
| | 108 | class UserTemplate(models.Model): |
|---|
| | 109 | """ Base class from which all User models inherit. """ |
|---|
| 119 | | def create_superuser(self, username, email, password): |
|---|
| 120 | | u = self.create_user(username, email, password) |
|---|
| 121 | | u.is_staff = True |
|---|
| 122 | | u.is_active = True |
|---|
| 123 | | u.is_superuser = True |
|---|
| 124 | | u.save() |
|---|
| 125 | | |
|---|
| 126 | | def make_random_password(self, length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'): |
|---|
| 127 | | "Generates a random password with the given length and given allowed_chars" |
|---|
| 128 | | # Note that default value of allowed_chars does not have "I" or letters |
|---|
| 129 | | # that look like it -- just to avoid confusion. |
|---|
| 130 | | from random import choice |
|---|
| 131 | | return ''.join([choice(allowed_chars) for i in range(length)]) |
|---|
| 132 | | |
|---|
| 133 | | class User(models.Model): |
|---|
| 134 | | """Users within the Django authentication system are represented by this model. |
|---|
| 135 | | |
|---|
| 136 | | Username and password are required. Other fields are optional. |
|---|
| 137 | | """ |
|---|
| 138 | | username = models.CharField(_('username'), max_length=30, unique=True, validator_list=[validators.isAlphaNumeric], help_text=_("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores).")) |
|---|
| 139 | | first_name = models.CharField(_('first name'), max_length=30, blank=True) |
|---|
| 140 | | last_name = models.CharField(_('last name'), max_length=30, blank=True) |
|---|
| 141 | | email = models.EmailField(_('e-mail address'), blank=True) |
|---|
| 142 | | password = models.CharField(_('password'), max_length=128, help_text=_("Use '[algo]$[salt]$[hexdigest]' or use the <a href=\"password/\">change password form</a>.")) |
|---|
| 143 | | is_staff = models.BooleanField(_('staff status'), default=False, help_text=_("Designates whether the user can log into this admin site.")) |
|---|
| 144 | | is_active = models.BooleanField(_('active'), default=True, help_text=_("Designates whether this user should be treated as active. Unselect this instead of deleting accounts.")) |
|---|
| 145 | | is_superuser = models.BooleanField(_('superuser status'), default=False, help_text=_("Designates that this user has all permissions without explicitly assigning them.")) |
|---|
| 146 | | last_login = models.DateTimeField(_('last login'), default=datetime.datetime.now) |
|---|
| 147 | | date_joined = models.DateTimeField(_('date joined'), default=datetime.datetime.now) |
|---|
| 148 | | groups = models.ManyToManyField(Group, verbose_name=_('groups'), blank=True, |
|---|
| 149 | | help_text=_("In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in.")) |
|---|
| 150 | | user_permissions = models.ManyToManyField(Permission, verbose_name=_('user permissions'), blank=True, filter_interface=models.HORIZONTAL) |
|---|
| 151 | | objects = UserManager() |
|---|
| 152 | | |
|---|
| 157 | | class Admin: |
|---|
| 158 | | fields = ( |
|---|
| 159 | | (None, {'fields': ('username', 'password')}), |
|---|
| 160 | | (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}), |
|---|
| 161 | | (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}), |
|---|
| 162 | | (_('Important dates'), {'fields': ('last_login', 'date_joined')}), |
|---|
| 163 | | (_('Groups'), {'fields': ('groups',)}), |
|---|
| 164 | | ) |
|---|
| 165 | | list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff') |
|---|
| 166 | | list_filter = ('is_staff', 'is_superuser') |
|---|
| 167 | | search_fields = ('username', 'first_name', 'last_name', 'email') |
|---|
| 168 | | ordering = ('username',) |
|---|
| 169 | | |
|---|
| 170 | | def __unicode__(self): |
|---|
| 171 | | return self.username |
|---|
| 172 | | |
|---|
| 173 | | def get_absolute_url(self): |
|---|
| 174 | | return "/users/%s/" % urllib.quote(smart_str(self.username)) |
|---|
| 175 | | |
|---|
| 184 | | |
|---|
| 185 | | def get_full_name(self): |
|---|
| 186 | | "Returns the first_name plus the last_name, with a space in between." |
|---|
| 187 | | full_name = u'%s %s' % (self.first_name, self.last_name) |
|---|
| 188 | | return full_name.strip() |
|---|
| 189 | | |
|---|
| 190 | | def set_password(self, raw_password): |
|---|
| 191 | | import random |
|---|
| 192 | | algo = 'sha1' |
|---|
| 193 | | salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5] |
|---|
| 194 | | hsh = get_hexdigest(algo, salt, raw_password) |
|---|
| 195 | | self.password = '%s$%s$%s' % (algo, salt, hsh) |
|---|
| 196 | | |
|---|
| 197 | | def check_password(self, raw_password): |
|---|
| 198 | | """ |
|---|
| 199 | | Returns a boolean of whether the raw_password was correct. Handles |
|---|
| 200 | | encryption formats behind the scenes. |
|---|
| 201 | | """ |
|---|
| 202 | | # Backwards-compatibility check. Older passwords won't include the |
|---|
| 203 | | # algorithm or salt. |
|---|
| 204 | | if '$' not in self.password: |
|---|
| 205 | | is_correct = (self.password == get_hexdigest('md5', '', raw_password)) |
|---|
| 206 | | if is_correct: |
|---|
| 207 | | # Convert the password to the new, more secure format. |
|---|
| 208 | | self.set_password(raw_password) |
|---|
| 209 | | self.save() |
|---|
| 210 | | return is_correct |
|---|
| 211 | | return check_password(raw_password, self.password) |
|---|
| 212 | | |
|---|
| 213 | | def set_unusable_password(self): |
|---|
| 214 | | # Sets a value that will never be a valid hash |
|---|
| 215 | | self.password = UNUSABLE_PASSWORD |
|---|
| 216 | | |
|---|
| 217 | | def has_usable_password(self): |
|---|
| 218 | | return self.password != UNUSABLE_PASSWORD |
|---|
| 219 | | |
|---|
| | 123 | |
|---|
| | 212 | |
|---|
| | 213 | # Grab the AUTH_USER_MODULE path and classname from the settings. |
|---|
| | 214 | # auth_user_module_parts[0] = module path |
|---|
| | 215 | # auth_user_module_parts[1] = class name |
|---|
| | 216 | auth_user_module_parts = settings.AUTH_USER_MODULE.rsplit('.', 1) |
|---|
| | 217 | auth_user_module = __import__(auth_user_module_parts[0], {}, {}, [auth_user_module_parts[0]]) |
|---|
| | 218 | # Store the auth_user model so it is accessible with the standard 'from django.contrib.auth.models import User' |
|---|
| | 219 | User = getattr(auth_user_module, auth_user_module_parts[1]) |
|---|
| | 220 | |
|---|
| | 221 | # Add the User model to the django models cache |
|---|
| | 222 | # These two lines allow the custom auth_user model to play nicely with syncdb and other systems that rely |
|---|
| | 223 | # on functions like django.db.models.loading.get_model(...) |
|---|
| | 224 | from django.db.models.loading import cache |
|---|
| | 225 | cache.register_models('auth', User) |
|---|
| | 226 | |
|---|