Ben
Ben

Reputation: 7124

django admin not working with custom user

I feel like I'm missing somehting obvious on this one.

I've created a custom user and user manger

class UserManager(BaseUserManager):
    # create a normal user
    # an email and password must be provided
    def create_user(self, email, password, first_name, last_name,
                    location, date_of_birth):

        if not email:
            raise ValueError("User must have an email")

        if not password:
            raise ValueError("User must have a password")

        email = email.lower()

        user = self.model(
                email=email,
                first_name=first_name,
                last_name=last_name,
                location=location,
                date_of_birth=date_of_birth
                )

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

    # Make an administrator   
    def create_superuser(self, email, password, first_name, last_name,
                         location, date_of_birth):
        user = self.create_user(
                email=email, 
                password=password,
                first_name=first_name,
                last_name=last_name,
                location=location,
                date_of_birth=date_of_birth
                )
        user.is_admin = True
        user.is_moderator = True
        user.save(using=self._db)
        return user

class User(AbstractBaseUser):
    email = models.EmailField(
            verbose_name='email address',
            max_length=255,
            unique=True
            )
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    location = models.ForeignKey(Location)
    date_of_birth = models.DateField()
    date_joined = models.DateTimeField(auto_now_add=True)

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_moderator =  models.BooleanField(default=False)

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name', 'location', 'date_of_birth']

    def __unicode__(self):
        return self.email

    def get_full_name(self):
        return self.first_name + ' ' + self.last_name

    def get_age(self):
        age = date.today() - self.date_of_birth
        return age.days / 365

    def is_staff(self):
        return self.is_admin

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

However if I visit the admin site, It will happily authorize a user who is not an admin is_admin=False

Has anyone run into this problem, Is there something I need to change when using django admin with a custom user?

EDIT

setting.py

AUTH_USER_MODEL = 'userAccount.User'
AUTHENTICATION_BACKEND = (
    'django.contrib.auth.backends.ModelBackend',
)

Upvotes: 0

Views: 1903

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174708

is_admin is not something that django's authentication system knows about. In the authentication form that is used, it is only checking if the user is active or is staff:

class AdminAuthenticationForm(AuthenticationForm):
    """
    A custom authentication form used in the admin app.
    """
    error_messages = {
        'invalid_login': _("Please enter the correct %(username)s and password "
                           "for a staff account. Note that both fields may be "
                           "case-sensitive."),
    }
    required_css_class = 'required'

    def confirm_login_allowed(self, user):
        if not user.is_active or not user.is_staff:
            raise forms.ValidationError(
                self.error_messages['invalid_login'],
                code='invalid_login',
                params={'username': self.username_field.verbose_name}
            )

In the original user model, is_staff is a model field. You do not have such a field, but rather a method. This could be a why its not working.

You can solve this problem two ways:

  1. Create your own AdminAuthenticationForm and adjust the confirm_login_allowed method to check for is_admin rather than is_staff.

  2. Create a is_staff property in your custom user model:

    @property
    def is_staff(self):
        return self._is_admin
    

Upvotes: 1

Related Questions