2008-10-06 13:14:17 +08:00
|
|
|
import urllib
|
|
|
|
|
2006-07-19 20:48:30 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2011-06-27 00:51:12 +08:00
|
|
|
from django.core.mail import send_mail
|
2010-01-22 22:30:06 +08:00
|
|
|
from django.db import models
|
2007-09-16 02:01:29 +08:00
|
|
|
from django.db.models.manager import EmptyManager
|
2011-12-23 19:22:13 +08:00
|
|
|
from django.utils.crypto import get_random_string
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.encoding import smart_str
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2011-11-20 18:50:18 +08:00
|
|
|
from django.utils import timezone
|
2011-06-27 00:51:12 +08:00
|
|
|
|
2011-06-27 00:51:46 +08:00
|
|
|
from django.contrib import auth
|
2011-07-13 17:35:51 +08:00
|
|
|
# UNUSABLE_PASSWORD is still imported here for backwards compatibility
|
2011-12-23 11:46:06 +08:00
|
|
|
from django.contrib.auth.hashers import (
|
|
|
|
check_password, make_password, is_password_usable, UNUSABLE_PASSWORD)
|
|
|
|
from django.contrib.auth.signals import user_logged_in
|
2011-06-27 00:51:46 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-01-02 22:21:50 +08:00
|
|
|
|
2010-11-26 21:33:27 +08:00
|
|
|
def update_last_login(sender, user, **kwargs):
|
|
|
|
"""
|
|
|
|
A signal receiver which updates the last_login date for
|
|
|
|
the user logging in.
|
|
|
|
"""
|
2011-11-20 18:50:18 +08:00
|
|
|
user.last_login = timezone.now()
|
2010-11-26 21:33:27 +08:00
|
|
|
user.save()
|
|
|
|
user_logged_in.connect(update_last_login)
|
|
|
|
|
2012-01-02 22:21:50 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class SiteProfileNotAvailable(Exception):
|
|
|
|
pass
|
|
|
|
|
2012-01-02 22:21:50 +08:00
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
class PermissionManager(models.Manager):
|
|
|
|
def get_by_natural_key(self, codename, app_label, model):
|
|
|
|
return self.get(
|
|
|
|
codename=codename,
|
2012-01-02 22:21:50 +08:00
|
|
|
content_type=ContentType.objects.get_by_natural_key(app_label,
|
|
|
|
model),
|
2009-12-14 20:39:20 +08:00
|
|
|
)
|
|
|
|
|
2012-01-02 22:21:50 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Permission(models.Model):
|
2012-01-02 22:21:50 +08:00
|
|
|
"""
|
|
|
|
The permissions system provides a way to assign permissions to specific
|
|
|
|
users and groups of users.
|
2006-06-20 12:07:32 +08:00
|
|
|
|
2012-01-02 22:21:50 +08:00
|
|
|
The permission system is used by the Django admin site, but may also be
|
|
|
|
useful in your own code. The Django admin site uses permissions as follows:
|
2006-06-20 12:07:32 +08:00
|
|
|
|
2012-01-02 22:21:50 +08:00
|
|
|
- The "add" permission limits the user's ability to view the "add" form
|
|
|
|
and add an object.
|
|
|
|
- The "change" permission limits a user's ability to view the change
|
|
|
|
list, view the "change" form and change an object.
|
2006-06-20 12:07:32 +08:00
|
|
|
- The "delete" permission limits the ability to delete an object.
|
2006-06-20 12:14:10 +08:00
|
|
|
|
2012-01-02 22:21:50 +08:00
|
|
|
Permissions are set globally per type of object, not per specific object
|
|
|
|
instance. It is possible to say "Mary may change news stories," but it's
|
|
|
|
not currently possible to say "Mary may change news stories, but only the
|
|
|
|
ones she created herself" or "Mary may only change news stories that have a
|
|
|
|
certain status or publication date."
|
2006-06-20 12:07:32 +08:00
|
|
|
|
2012-01-02 22:21:50 +08:00
|
|
|
Three basic permissions -- add, change and delete -- are automatically
|
|
|
|
created for each Django model.
|
2006-06-20 12:07:32 +08:00
|
|
|
"""
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(_('name'), max_length=50)
|
2006-05-02 09:31:56 +08:00
|
|
|
content_type = models.ForeignKey(ContentType)
|
2007-08-05 13:14:46 +08:00
|
|
|
codename = models.CharField(_('codename'), max_length=100)
|
2009-12-14 20:39:20 +08:00
|
|
|
objects = PermissionManager()
|
2007-04-07 12:21:31 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('permission')
|
|
|
|
verbose_name_plural = _('permissions')
|
|
|
|
unique_together = (('content_type', 'codename'),)
|
2012-01-02 22:21:50 +08:00
|
|
|
ordering = ('content_type__app_label', 'content_type__model',
|
|
|
|
'codename')
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2008-08-11 23:13:00 +08:00
|
|
|
return u"%s | %s | %s" % (
|
|
|
|
unicode(self.content_type.app_label),
|
|
|
|
unicode(self.content_type),
|
|
|
|
unicode(self.name))
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
def natural_key(self):
|
|
|
|
return (self.codename,) + self.content_type.natural_key()
|
|
|
|
natural_key.dependencies = ['contenttypes.contenttype']
|
|
|
|
|
2006-06-20 12:07:32 +08:00
|
|
|
|
2012-02-04 20:48:01 +08:00
|
|
|
class GroupManager(models.Manager):
|
|
|
|
"""
|
|
|
|
The manager for the auth's Group model.
|
|
|
|
"""
|
|
|
|
def get_by_natural_key(self, name):
|
|
|
|
return self.get(name=name)
|
|
|
|
|
2012-01-02 22:21:50 +08:00
|
|
|
class Group(models.Model):
|
|
|
|
"""
|
|
|
|
Groups are a generic way of categorizing users to apply permissions, or
|
|
|
|
some other label, to those users. A user can belong to any number of
|
|
|
|
groups.
|
|
|
|
|
|
|
|
A user in a group automatically has all the permissions granted to that
|
|
|
|
group. For example, if the group Site editors has the permission
|
|
|
|
can_edit_home_page, any user in that group will have that permission.
|
|
|
|
|
|
|
|
Beyond permissions, groups are a convenient way to categorize users to
|
|
|
|
apply some label, or extended functionality, to them. For example, you
|
|
|
|
could create a group 'Special users', and you could write code that would
|
|
|
|
do special things to those users -- such as giving them access to a
|
|
|
|
members-only portion of your site, or sending them members-only email
|
|
|
|
messages.
|
2006-06-20 12:07:32 +08:00
|
|
|
"""
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(_('name'), max_length=80, unique=True)
|
2012-01-02 22:21:50 +08:00
|
|
|
permissions = models.ManyToManyField(Permission,
|
|
|
|
verbose_name=_('permissions'), blank=True)
|
2007-04-07 12:21:31 +08:00
|
|
|
|
2012-02-04 20:48:01 +08:00
|
|
|
objects = GroupManager()
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('group')
|
|
|
|
verbose_name_plural = _('groups')
|
2008-08-11 23:13:00 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.name
|
|
|
|
|
2012-02-04 20:48:01 +08:00
|
|
|
def natural_key(self):
|
|
|
|
return (self.name,)
|
|
|
|
|
2012-01-02 22:21:50 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class UserManager(models.Manager):
|
2012-02-10 02:58:53 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def normalize_email(cls, email):
|
2010-03-02 04:30:44 +08:00
|
|
|
"""
|
2012-02-10 02:58:53 +08:00
|
|
|
Normalize the address by lowercasing the domain part of the email
|
|
|
|
address.
|
2010-03-02 04:30:44 +08:00
|
|
|
"""
|
2011-06-28 12:29:48 +08:00
|
|
|
email = email or ''
|
2010-03-02 04:30:44 +08:00
|
|
|
try:
|
2012-02-10 02:58:53 +08:00
|
|
|
email_name, domain_part = email.strip().rsplit('@', 1)
|
2010-03-02 04:30:44 +08:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
email = '@'.join([email_name, domain_part.lower()])
|
2012-02-10 02:58:53 +08:00
|
|
|
return email
|
2010-03-02 04:30:44 +08:00
|
|
|
|
2012-02-10 02:58:53 +08:00
|
|
|
def create_user(self, username, email=None, password=None):
|
|
|
|
"""
|
|
|
|
Creates and saves a User with the given username, email and password.
|
|
|
|
"""
|
|
|
|
now = timezone.now()
|
2012-03-03 00:56:20 +08:00
|
|
|
if not username:
|
|
|
|
raise ValueError('The given username must be set')
|
2012-02-10 02:58:53 +08:00
|
|
|
email = UserManager.normalize_email(email)
|
|
|
|
user = self.model(username=username, email=email,
|
|
|
|
is_staff=False, is_active=True, is_superuser=False,
|
|
|
|
last_login=now, date_joined=now)
|
2010-03-02 04:30:44 +08:00
|
|
|
|
2010-10-09 11:34:08 +08:00
|
|
|
user.set_password(password)
|
2010-02-22 21:09:02 +08:00
|
|
|
user.save(using=self._db)
|
2006-05-02 09:31:56 +08:00
|
|
|
return user
|
|
|
|
|
2008-06-08 13:31:16 +08:00
|
|
|
def create_superuser(self, username, email, password):
|
|
|
|
u = self.create_user(username, email, password)
|
|
|
|
u.is_staff = True
|
|
|
|
u.is_active = True
|
|
|
|
u.is_superuser = True
|
2010-02-22 21:09:02 +08:00
|
|
|
u.save(using=self._db)
|
2009-03-31 06:00:07 +08:00
|
|
|
return u
|
2008-06-08 13:31:16 +08:00
|
|
|
|
2012-01-02 22:21:50 +08:00
|
|
|
def make_random_password(self, length=10,
|
|
|
|
allowed_chars='abcdefghjkmnpqrstuvwxyz'
|
|
|
|
'ABCDEFGHJKLMNPQRSTUVWXYZ'
|
|
|
|
'23456789'):
|
2011-06-27 00:51:12 +08:00
|
|
|
"""
|
2012-01-02 22:21:50 +08:00
|
|
|
Generates a random password with the given length and given
|
|
|
|
allowed_chars. Note that the default value of allowed_chars does not
|
|
|
|
have "I" or "O" or letters and digits that look similar -- just to
|
|
|
|
avoid confusion.
|
2011-06-27 00:51:12 +08:00
|
|
|
"""
|
|
|
|
return get_random_string(length, allowed_chars)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-02-04 20:48:01 +08:00
|
|
|
def get_by_natural_key(self, username):
|
|
|
|
return self.get(username=username)
|
|
|
|
|
2010-01-28 09:47:23 +08:00
|
|
|
|
|
|
|
# A few helper functions for common logic between User and AnonymousUser.
|
|
|
|
def _user_get_all_permissions(user, obj):
|
|
|
|
permissions = set()
|
|
|
|
for backend in auth.get_backends():
|
2011-09-11 05:00:32 +08:00
|
|
|
if hasattr(backend, "get_all_permissions"):
|
|
|
|
if obj is not None:
|
|
|
|
permissions.update(backend.get_all_permissions(user, obj))
|
|
|
|
else:
|
|
|
|
permissions.update(backend.get_all_permissions(user))
|
2010-01-28 09:47:23 +08:00
|
|
|
return permissions
|
|
|
|
|
|
|
|
|
|
|
|
def _user_has_perm(user, perm, obj):
|
2011-11-13 01:23:07 +08:00
|
|
|
anon = user.is_anonymous()
|
|
|
|
active = user.is_active
|
2010-01-28 09:47:23 +08:00
|
|
|
for backend in auth.get_backends():
|
2011-11-13 01:23:07 +08:00
|
|
|
if anon or active or backend.supports_inactive_user:
|
|
|
|
if hasattr(backend, "has_perm"):
|
|
|
|
if obj is not None:
|
|
|
|
if backend.has_perm(user, perm, obj):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
if backend.has_perm(user, perm):
|
2010-01-28 09:47:23 +08:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _user_has_module_perms(user, app_label):
|
2011-11-13 01:23:07 +08:00
|
|
|
anon = user.is_anonymous()
|
|
|
|
active = user.is_active
|
2010-01-28 09:47:23 +08:00
|
|
|
for backend in auth.get_backends():
|
2011-11-13 01:23:07 +08:00
|
|
|
if anon or active or backend.supports_inactive_user:
|
|
|
|
if hasattr(backend, "has_module_perms"):
|
|
|
|
if backend.has_module_perms(user, app_label):
|
|
|
|
return True
|
2010-01-28 09:47:23 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class User(models.Model):
|
2009-12-10 09:05:35 +08:00
|
|
|
"""
|
2012-01-02 22:21:50 +08:00
|
|
|
Users within the Django authentication system are represented by this
|
|
|
|
model.
|
2006-06-20 12:07:32 +08:00
|
|
|
|
2006-06-20 12:14:10 +08:00
|
|
|
Username and password are required. Other fields are optional.
|
2006-06-20 12:07:32 +08:00
|
|
|
"""
|
2012-01-02 22:21:50 +08:00
|
|
|
username = models.CharField(_('username'), max_length=30, unique=True,
|
|
|
|
help_text=_('Required. 30 characters or fewer. Letters, numbers and '
|
|
|
|
'@/./+/-/_ characters'))
|
2007-08-05 13:14:46 +08:00
|
|
|
first_name = models.CharField(_('first name'), max_length=30, blank=True)
|
|
|
|
last_name = models.CharField(_('last name'), max_length=30, blank=True)
|
2006-05-02 09:31:56 +08:00
|
|
|
email = models.EmailField(_('e-mail address'), blank=True)
|
2011-12-10 06:10:03 +08:00
|
|
|
password = models.CharField(_('password'), max_length=128)
|
2012-01-02 22:21:50 +08:00
|
|
|
is_staff = models.BooleanField(_('staff status'), default=False,
|
|
|
|
help_text=_('Designates whether the user can log into this admin '
|
|
|
|
'site.'))
|
|
|
|
is_active = models.BooleanField(_('active'), default=True,
|
|
|
|
help_text=_('Designates whether this user should be treated as '
|
|
|
|
'active. Unselect this instead of deleting accounts.'))
|
|
|
|
is_superuser = models.BooleanField(_('superuser status'), default=False,
|
|
|
|
help_text=_('Designates that this user has all permissions without '
|
|
|
|
'explicitly assigning them.'))
|
2011-11-20 18:50:18 +08:00
|
|
|
last_login = models.DateTimeField(_('last login'), default=timezone.now)
|
|
|
|
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
|
2012-01-02 22:21:50 +08:00
|
|
|
groups = models.ManyToManyField(Group, verbose_name=_('groups'),
|
|
|
|
blank=True, help_text=_('The groups this user belongs to. A user will '
|
|
|
|
'get all permissions granted to each of '
|
|
|
|
'his/her group.'))
|
|
|
|
user_permissions = models.ManyToManyField(Permission,
|
|
|
|
verbose_name=_('user permissions'), blank=True,
|
|
|
|
help_text='Specific permissions for this user.')
|
2006-05-02 09:31:56 +08:00
|
|
|
objects = UserManager()
|
2007-04-07 12:21:31 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('user')
|
|
|
|
verbose_name_plural = _('users')
|
2008-08-11 23:13:00 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.username
|
|
|
|
|
2012-02-04 20:48:01 +08:00
|
|
|
def natural_key(self):
|
|
|
|
return (self.username,)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def get_absolute_url(self):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return "/users/%s/" % urllib.quote(smart_str(self.username))
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def is_anonymous(self):
|
2009-12-10 09:05:35 +08:00
|
|
|
"""
|
|
|
|
Always returns False. This is a way of comparing User objects to
|
|
|
|
anonymous users.
|
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
return False
|
2006-10-25 00:42:03 +08:00
|
|
|
|
2006-07-19 10:09:26 +08:00
|
|
|
def is_authenticated(self):
|
2009-12-10 09:05:35 +08:00
|
|
|
"""
|
|
|
|
Always return True. This is a way to tell if the user has been
|
|
|
|
authenticated in templates.
|
2006-07-19 10:09:26 +08:00
|
|
|
"""
|
|
|
|
return True
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def get_full_name(self):
|
2011-06-27 00:51:12 +08:00
|
|
|
"""
|
|
|
|
Returns the first_name plus the last_name, with a space in between.
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
full_name = u'%s %s' % (self.first_name, self.last_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
return full_name.strip()
|
|
|
|
|
|
|
|
def set_password(self, raw_password):
|
2011-12-23 11:46:06 +08:00
|
|
|
self.password = make_password(raw_password)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def check_password(self, raw_password):
|
|
|
|
"""
|
|
|
|
Returns a boolean of whether the raw_password was correct. Handles
|
2011-10-19 04:28:52 +08:00
|
|
|
hashing formats behind the scenes.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2011-12-23 11:46:06 +08:00
|
|
|
def setter(raw_password):
|
|
|
|
self.set_password(raw_password)
|
|
|
|
self.save()
|
|
|
|
return check_password(raw_password, self.password, setter)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-07-29 02:30:40 +08:00
|
|
|
def set_unusable_password(self):
|
|
|
|
# Sets a value that will never be a valid hash
|
2011-12-23 11:46:06 +08:00
|
|
|
self.password = make_password(None)
|
2007-07-29 02:30:40 +08:00
|
|
|
|
|
|
|
def has_usable_password(self):
|
2011-06-27 00:51:46 +08:00
|
|
|
return is_password_usable(self.password)
|
2007-07-29 02:30:40 +08:00
|
|
|
|
2009-12-10 09:05:35 +08:00
|
|
|
def get_group_permissions(self, obj=None):
|
2007-09-20 00:50:30 +08:00
|
|
|
"""
|
2012-01-02 22:21:50 +08:00
|
|
|
Returns a list of permission strings that this user has through his/her
|
|
|
|
groups. This method queries all available auth backends. If an object
|
|
|
|
is passed in, only permissions matching this object are returned.
|
2007-09-20 00:50:30 +08:00
|
|
|
"""
|
|
|
|
permissions = set()
|
|
|
|
for backend in auth.get_backends():
|
|
|
|
if hasattr(backend, "get_group_permissions"):
|
2009-12-31 06:12:57 +08:00
|
|
|
if obj is not None:
|
2012-01-02 22:21:50 +08:00
|
|
|
permissions.update(backend.get_group_permissions(self,
|
|
|
|
obj))
|
2009-12-10 09:05:35 +08:00
|
|
|
else:
|
2009-12-31 06:12:57 +08:00
|
|
|
permissions.update(backend.get_group_permissions(self))
|
2007-09-20 00:50:30 +08:00
|
|
|
return permissions
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-10 09:05:35 +08:00
|
|
|
def get_all_permissions(self, obj=None):
|
2010-01-28 09:47:23 +08:00
|
|
|
return _user_get_all_permissions(self, obj)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-10 09:05:35 +08:00
|
|
|
def has_perm(self, perm, obj=None):
|
2007-09-20 00:50:30 +08:00
|
|
|
"""
|
|
|
|
Returns True if the user has the specified permission. This method
|
|
|
|
queries all available auth backends, but returns immediately if any
|
|
|
|
backend returns True. Thus, a user who has permission from a single
|
2012-01-02 22:21:50 +08:00
|
|
|
auth backend is assumed to have permission in general. If an object is
|
|
|
|
provided, permissions for this specific object are checked.
|
2007-09-20 00:50:30 +08:00
|
|
|
"""
|
2008-04-13 09:50:29 +08:00
|
|
|
|
2010-12-22 03:18:12 +08:00
|
|
|
# Active superusers have all permissions.
|
|
|
|
if self.is_active and self.is_superuser:
|
2006-05-02 09:31:56 +08:00
|
|
|
return True
|
2008-04-13 09:50:29 +08:00
|
|
|
|
2007-09-20 00:50:30 +08:00
|
|
|
# Otherwise we need to check the backends.
|
2010-01-28 09:47:23 +08:00
|
|
|
return _user_has_perm(self, perm, obj)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-10 09:05:35 +08:00
|
|
|
def has_perms(self, perm_list, obj=None):
|
|
|
|
"""
|
2012-01-02 22:21:50 +08:00
|
|
|
Returns True if the user has each of the specified permissions. If
|
|
|
|
object is passed, it checks if the user has all required perms for this
|
|
|
|
object.
|
2009-12-10 09:05:35 +08:00
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
for perm in perm_list:
|
2009-12-10 09:05:35 +08:00
|
|
|
if not self.has_perm(perm, obj):
|
2006-05-02 09:31:56 +08:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def has_module_perms(self, app_label):
|
2007-09-20 00:50:30 +08:00
|
|
|
"""
|
2012-01-02 22:21:50 +08:00
|
|
|
Returns True if the user has any permissions in the given app label.
|
|
|
|
Uses pretty much the same logic as has_perm, above.
|
2007-09-20 00:50:30 +08:00
|
|
|
"""
|
2010-12-22 03:18:12 +08:00
|
|
|
# Active superusers have all permissions.
|
|
|
|
if self.is_active and self.is_superuser:
|
2006-05-02 09:31:56 +08:00
|
|
|
return True
|
2007-09-20 00:50:30 +08:00
|
|
|
|
2010-01-28 09:47:23 +08:00
|
|
|
return _user_has_module_perms(self, app_label)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def email_user(self, subject, message, from_email=None):
|
2011-06-27 00:51:12 +08:00
|
|
|
"""
|
|
|
|
Sends an email to this User.
|
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
send_mail(subject, message, from_email, [self.email])
|
|
|
|
|
|
|
|
def get_profile(self):
|
|
|
|
"""
|
|
|
|
Returns site-specific profile for this user. Raises
|
|
|
|
SiteProfileNotAvailable if this site does not allow profiles.
|
|
|
|
"""
|
|
|
|
if not hasattr(self, '_profile_cache'):
|
|
|
|
from django.conf import settings
|
2008-08-26 00:56:59 +08:00
|
|
|
if not getattr(settings, 'AUTH_PROFILE_MODULE', False):
|
2012-01-02 22:21:50 +08:00
|
|
|
raise SiteProfileNotAvailable(
|
|
|
|
'You need to set AUTH_PROFILE_MODULE in your project '
|
|
|
|
'settings')
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
|
|
|
app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
|
2010-02-23 13:52:37 +08:00
|
|
|
except ValueError:
|
2012-01-02 22:21:50 +08:00
|
|
|
raise SiteProfileNotAvailable(
|
|
|
|
'app_label and model_name should be separated by a dot in '
|
|
|
|
'the AUTH_PROFILE_MODULE setting')
|
2010-02-23 13:52:37 +08:00
|
|
|
try:
|
2006-05-02 09:31:56 +08:00
|
|
|
model = models.get_model(app_label, model_name)
|
2010-02-23 13:52:37 +08:00
|
|
|
if model is None:
|
2012-01-02 22:21:50 +08:00
|
|
|
raise SiteProfileNotAvailable(
|
|
|
|
'Unable to load the profile model, check '
|
|
|
|
'AUTH_PROFILE_MODULE in your project settings')
|
|
|
|
self._profile_cache = model._default_manager.using(
|
|
|
|
self._state.db).get(user__id__exact=self.id)
|
2008-10-05 20:07:10 +08:00
|
|
|
self._profile_cache.user = self
|
2006-07-11 22:03:24 +08:00
|
|
|
except (ImportError, ImproperlyConfigured):
|
2006-05-02 09:31:56 +08:00
|
|
|
raise SiteProfileNotAvailable
|
|
|
|
return self._profile_cache
|
|
|
|
|
2009-12-10 00:57:23 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class AnonymousUser(object):
|
|
|
|
id = None
|
|
|
|
username = ''
|
2007-09-16 02:01:29 +08:00
|
|
|
is_staff = False
|
2007-12-11 14:37:07 +08:00
|
|
|
is_active = False
|
2007-09-16 02:01:29 +08:00
|
|
|
is_superuser = False
|
|
|
|
_groups = EmptyManager()
|
|
|
|
_user_permissions = EmptyManager()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2007-07-03 20:24:46 +08:00
|
|
|
return 'AnonymousUser'
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __str__(self):
|
|
|
|
return unicode(self).encode('utf-8')
|
|
|
|
|
2006-10-25 00:42:03 +08:00
|
|
|
def __eq__(self, other):
|
|
|
|
return isinstance(other, self.__class__)
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return 1 # instances always return the same hash value
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def save(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def set_password(self, raw_password):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def check_password(self, raw_password):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def _get_groups(self):
|
2007-09-16 02:01:29 +08:00
|
|
|
return self._groups
|
2006-05-02 09:31:56 +08:00
|
|
|
groups = property(_get_groups)
|
|
|
|
|
|
|
|
def _get_user_permissions(self):
|
2007-09-16 02:01:29 +08:00
|
|
|
return self._user_permissions
|
2006-05-02 09:31:56 +08:00
|
|
|
user_permissions = property(_get_user_permissions)
|
|
|
|
|
2010-01-28 09:47:23 +08:00
|
|
|
def get_group_permissions(self, obj=None):
|
|
|
|
return set()
|
|
|
|
|
|
|
|
def get_all_permissions(self, obj=None):
|
|
|
|
return _user_get_all_permissions(self, obj=obj)
|
|
|
|
|
2009-12-10 09:05:35 +08:00
|
|
|
def has_perm(self, perm, obj=None):
|
2010-01-28 09:47:23 +08:00
|
|
|
return _user_has_perm(self, perm, obj=obj)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-10 09:05:35 +08:00
|
|
|
def has_perms(self, perm_list, obj=None):
|
2010-01-28 09:47:23 +08:00
|
|
|
for perm in perm_list:
|
|
|
|
if not self.has_perm(perm, obj):
|
|
|
|
return False
|
|
|
|
return True
|
2008-07-22 11:05:40 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def has_module_perms(self, module):
|
2010-01-28 09:47:23 +08:00
|
|
|
return _user_has_module_perms(self, module)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def is_anonymous(self):
|
|
|
|
return True
|
2006-10-25 00:42:03 +08:00
|
|
|
|
2006-07-19 10:09:26 +08:00
|
|
|
def is_authenticated(self):
|
|
|
|
return False
|