2013-06-28 73 views
1

我試圖從Django 1.5中受益並創建自定義用戶模型。爲了使用內置權限,我想在管理界面中限制訪問權限。我也從PermissionMixin繼承了我的用戶類。但是,當我創建新用戶並檢查工作人員框時,新用戶將獲得超級用戶擁有的所有訪問權限。Django自定義用戶模型:如何管理員工權限?

我在做什麼錯?

models.py

class MyUserManager(BaseUserManager): 
    def create_user(self, email, password=None): 
     if not email: 
      raise ValueError(_('Users must have an email address')) 
     user = self.model(email=MyUserManager.normalize_email(email),) 
     user.set_password(password) 
     user.save(using=self._db) 
     return user 

    def create_superuser(self, email, password): 
     user = self.create_user(email, password=password,) 
     user.is_superuser = True 
     user.is_staff = True 
     user.save(using=self._db) 
     return user 


class MyUser(AbstractBaseUser, PermissionsMixin): 
    email = models.EmailField(unique=True, db_index=True,) 
    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_staff = models.BooleanField(_('staff status'), default=False, 
     help_text=_('Designates whether the user can log into this admin site.')) 

    objects = MyUserManager() 
    USERNAME_FIELD = 'email' 
+0

檢查is_superuser的默認值是否設置爲False。代碼的這一部分不寫在這裏。 – Sudipta

+0

@Sudipta,謝謝你的回答,但是在PermissionMixin中這個字段是'default = False',MyUser從它繼承。無論如何,我可以在用戶編輯頁面看到** is_superuser **字段,並且不會檢查它。除了獲取全名和權限等幾個方法之外,沒有其他代碼部分。 –

回答

1

我改寫了自定義用戶模型。現在與django用戶模型的主要區別在於,我的域名沒有用戶名。這裏是代碼:

import warnings 
from django.core.exceptions import ImproperlyConfigured 
from django.core.mail import send_mail 
from django.db import models 
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin,\ 
    SiteProfileNotAvailable, BaseUserManager 
from django.utils import timezone 
from django.utils.http import urlquote 
from django.utils.translation import ugettext_lazy as _ 


class CustomUserManager(BaseUserManager): 
    def create_user(self, email=None, password=None, **extra_fields): 
     """ 
     Creates and saves a User with the given email and password. 
     """ 
     now = timezone.now() 
     if not email: 
      raise ValueError('The given email must be set') 
     email = CustomUserManager.normalize_email(email) 
     user = self.model(email=email, 
          is_staff=False, is_active=True, is_superuser=False, 
          last_login=now, date_joined=now, **extra_fields) 

     user.set_password(password) 
     user.save(using=self._db) 
     return user 

    def create_superuser(self, email, password, **extra_fields): 
     u = self.create_user(email, password, **extra_fields) 
     u.is_staff = True 
     u.is_active = True 
     u.is_superuser = True 
     u.save(using=self._db) 
     return u 


class CustomUser(AbstractBaseUser, PermissionsMixin): 
    email = models.EmailField(_('email address'), unique=True) 
    first_name = models.CharField(_('first name'), max_length=30, blank=True) 
    middle_name = models.CharField(_('middle name'), max_length=30, blank=True) 
    last_name = models.CharField(_('last name'), max_length=30, blank=True) 
    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.')) 
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now) 

    objects = CustomUserManager() 

    USERNAME_FIELD = 'email' 

    def get_absolute_url(self): 
     return "https://stackoverflow.com/users/%s/" % urlquote(self.username) 

    def get_full_name(self): 
     """ 
     Returns the first_name plus the last_name, with a space in between. 
     """ 
     full_name = '%s %s' % (self.first_name, self.last_name) 
     return full_name.strip() 

    def get_short_name(self): 
     "Returns the short name for the user." 
     return self.first_name 

    def email_user(self, subject, message, from_email=None): 
     """ 
     Sends an email to this User. 
     """ 
     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. 
     """ 
     warnings.warn("The use of AUTH_PROFILE_MODULE to define user profiles" 
         " has been deprecated.", 
      PendingDeprecationWarning) 
     if not hasattr(self, '_profile_cache'): 
      from django.conf import settings 
      if not getattr(settings, 'AUTH_PROFILE_MODULE', False): 
       raise SiteProfileNotAvailable(
        'You need to set AUTH_PROFILE_MODULE in your project ' 
        'settings') 
      try: 
       app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.') 
      except ValueError: 
       raise SiteProfileNotAvailable(
        'app_label and model_name should be separated by a dot in ' 
        'the AUTH_PROFILE_MODULE setting') 
      try: 
       model = models.get_model(app_label, model_name) 
       if model is None: 
        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) 
       self._profile_cache.user = self 
      except (ImportError, ImproperlyConfigured): 
       raise SiteProfileNotAvailable 
     return self._profile_cache 

現在它工作並保持所有默認權限。另請注意,對於管理員,您必須重寫用戶ModelAdminUserCreationForm以及UserChangeForm類。

2

我有同樣的問題,在我來說,我有這樣的:

class Estudiante(AbstractBaseUser,PermissionsMixin): 
name = models.CharField(max_length=250,null=False,blank=False)  
email = models.EmailField(
    verbose_name='Direccion de correo Electronico', 
    max_length=255, 
    unique=True, 
    db_index=True, 
) 

is_staff = models.BooleanField(u'staff status', default=False, 
    help_text=u'Designates whether the user can log into this admin ' 
       'site.') 
is_active = models.BooleanField(u'active', default=True, 
    help_text=u'Designates whether this user should be treated as ' 
       'active. Unselect this instead of deleting accounts.') 


objects = MyUserManager() 

USERNAME_FIELD = 'email' 
REQUIRED_FIELDS = ['name'] 

def get_full_name(self): 
    # The user is identified by their email address 
    return self.name 

def get_short_name(self): 
    # The user is identified by their email address 
    return self.email 

def __unicode__(self): 
    return self.email 

def has_perm(self, perm, obj=None): 
    "Does the user have a specific permission?" 
    # Simplest possible answer: Yes, always 
    return True 

def has_module_perms(self, app_label): 
    "Does the user have permissions to view the app `app_label`?" 
    # Simplest possible answer: Yes, always 
    return True 

和MyUserManager:

class MyUserManager(BaseUserManager): 
def create_user(self, name,email, password=None): 
    .... 
    return user 

def create_superuser(self, name,email, password): 
    """ 
    Creates and saves a superuser with the given email, date of 
    birth and password. 
    """ 
    user = self.model(
     email=MyUserManager.normalize_email(email), 
     name=name, 
    ) 
    user.is_staff = True 
    user.is_active = True 
    user.is_superuser = True 
    user.set_password(password) 
    user.save(using=self._db) 
    return user 

我固定的評論或問題消除方法「has_perm」和has_module_perms

class Estudiante(AbstractBaseUser,PermissionsMixin): 
name = models.CharField(max_length=250,null=False,blank=False)  
email = models.EmailField(
    verbose_name='Direccion de correo Electronico', 
    max_length=255, 
    unique=True, 
    db_index=True, 
) 

is_staff = models.BooleanField(u'staff status', default=False, 
    help_text=u'Designates whether the user can log into this admin ' 
       'site.') 
is_active = models.BooleanField(u'active', default=True, 
    help_text=u'Designates whether this user should be treated as ' 
       'active. Unselect this instead of deleting accounts.') 


objects = MyUserManager() 

USERNAME_FIELD = 'email' 
REQUIRED_FIELDS = ['name'] 

def get_full_name(self): 
    # The user is identified by their email address 
    return self.name 

def get_short_name(self): 
    # The user is identified by their email address 
    return self.email 

def __unicode__(self): 
    return self.email 
+0

對不起,很長時間以來,我真的沒有機會檢查你的建議,現在我又回到了這個問題。您不需要'has_perm'和'has_module_perms'方法,因爲您從** PermissionsMixin **繼承它們。我完全重寫了我的自定義用戶模型,您可以在我的答案中看到。 –