16-crm项目-kingadmin,权限管理

权限管理

怎么实现权限的管理呢?

首先是model,这个表继承了权限类,

class UserProfile(AbstractBaseUser, PermissionsMixin):
    '''账号表'''
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
        null=True
    )
    password = models.CharField(_('password'), max_length=128,
                                help_text=mark_safe('''<a href='password/'>修改密码</a>'''))
    name = models.CharField(max_length=32)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    roles = models.ManyToManyField("Role", blank=True)
    objects = UserProfileManager()

    stu_account = models.ForeignKey("Customer", verbose_name="关联学员账号", blank=True, null=True,
                                    help_text="只有学员报名后方可为其创建账号")
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name']

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

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

    def __str__(self):  # __unicode__ on Python 2
        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

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_active

    class Meta:
        permissions = (('can_fuck_him_to_death', '弄死小虎逼'),
                       ('can_access_my_course', '可以访问我的课程'),
                       ('can_access_customer_list', '可以访问客户列表'),
                       ('can_access_customer_detail', '可以访问客户详细'),
                       ('can_access_studyrecords', '可以访问学习记录页面'),
                       ('can_access_homework_detail', '可以访问作业详情页面'),
                       ('can_upload_homework', '可以交作业'),
                       ('access_kingadmin_table_obj_detail', '可以访问kingadmin每个表的对象'),
                       ('change_kingadmin_table_obj_detail', '可以修改kingadmin每个表的对象'),
                       )

继承了权限类之后,

需要进行一次数据库的迁移,这样会增加两个字段,一个权限,一个组,

自己写的admin

class UserProfileAdmin(BaseAdmin):
    list_display = ('email','name')
    readonly_fields = ('password',)
    modelform_exclude_fields = ["last_login",]
    filter_horizontal = ("user_permissions","groups")
原文地址:https://www.cnblogs.com/andy0816/p/13731742.html