简体   繁体   English

Django:编外人员可以登录到管理页面

[英]Django: Non-staff users can login to admin page

Python 3.5.1 and Django 1.9 Python 3.5.1和Django 1.9

I have created a custom user model in my project: 我在项目中创建了一个自定义用户模型:

from django.db import models
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)


class UserManager(BaseUserManager):
    def create_user(self, username, password=None):
        user = self.model(
            username=username,
        )

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

    def create_superuser(self, username, password):
        user = self.create_user(
            username,
            password=password,
        )

        user.is_admin = True
        user.is_superuser = True
        user.save(using=self._db)
        return user


# Users
class User(AbstractBaseUser):
    username = models.CharField(
        unique=True,
        max_length=50,
    )
    bio = models.TextField()

    is_active = models.BooleanField(default=True,
                                    verbose_name="Active",
                                    help_text="lorem")
    is_admin = models.BooleanField(default=False,
                                   verbose_name="Staff status",
                                   help_text="lorem")
    is_superuser = models.BooleanField(default=False,
                                       verbose_name="Superuser status",
                                       help_text="lorem")

    objects = UserManager()

    USERNAME_FIELD = 'username'

    def get_full_name(self):
        return self.username

    def get_short_name(self):
        return self.username

    def is_staff(self):
        return self.is_admin

    def __str__(self):
        return self.username

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

    def has_module_perms(self, db):
        return True

I followed the guide in the official documentation here 我在这里遵循了官方文档中的指南

I have defined AUTH_USER_MODEL in settings.py 在settings.py定义AUTH_USER_MODEL

Everything seems to work correctly with the custom user model, execpt... 一切似乎都可以与自定义用户模型execpt一起正常使用...

Any user can login to the admin interface. 任何用户都可以登录管理界面。 This of course is not a good thing 这当然不是一件好事

Here is a screenshot of the admin interface when logged in as a non-admin user: 这是以非管理员用户身份登录时管理界面的屏幕截图: 非管理员登录

And here when logged as an actual admin user 当以实际管理员用户身份登录时 管理员登录

Note the top right corners. 请注意右上角。 When logged in with a non-admin user, the menu dose not appear. 使用非管理员用户登录时,菜单不会出现。 Strange. 奇怪。

Both admin and non-admin users have full access to the admin interface, they can both add, change and delete entries. 管理员和非管理员用户都具有对管理界面的完全访问权限,他们都可以添加,更改和删除条目。

Clearly this is a terrible security issue, I don't even know where to start dubugging 显然,这是一个可怕的安全问题,我什至不知道从哪里开始调试

You'll need to turn is_staff into a property. 您需要将is_staff变成一个属性。 If you use the built-in User model, Django expects it to be a model field, which means all checks for staff status happen as if user.is_staff: , not as if user.is_staff(): . 如果您使用内置的User模型,则Django希望它是一个model字段,这意味着所有对人员状态的检查都if user.is_staff: ,而不是if user.is_staff(): All you need to do is to include a single line before your is_staff method definition: 您需要做的就是在is_staff方法定义之前包含一行:

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

Since Python considers every function object to be True when used in a boolean context, all your users pass the check for is_staff . 由于Python在布尔上下文中使用时会认为每个函数对象都为True,因此所有用户都通过了is_staff的检查。

If you want to know why the admin displays the correct value in the “STAFF STATUS” column, it's because is_staff is listed in list_display of the default ModelAdmin for the user model, and ModelAdmin does check items in list_display if they're callable or not, and in case they are, it calls them. 如果您想知道管理员为什么在“ STAFF STATUS”列中显示正确的值,那是因为is_staff列在用户模型的默认ModelAdmin list_display中,并且ModelAdmin会检查list_display项目是否可调用,并在可能的情况下调用它们。 However, this is only done for the purpose of displaying the value (since list_display is a more general mechanism), but not for any actual access control checks. 但是,这样做仅出于显示值的目的(因为list_display是更通用的机制),而不是为了进行任何实际的访问控制检查。

You musst inheritat from AbstractBaseUser and PermissionsMixin . 您要从AbstractBaseUserPermissionsMixin继承继承。

You just included a "bio" field in your User class. 您刚刚在User类中添加了一个“ bio”字段。 Maybe just extend from AbstractUser and not AbstractBaseUser . 也许只是从AbstractUser而不是AbstractBaseUser扩展。 The AbstractUser has most of the important stuff and you can include you bio field there AbstractUser拥有大部分重要内容,您可以在其中包括生物领域

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM