django中权限框架设计
2019-07-05 本文已影响15人
我爱学python
一:admin下的权限了解
(一)默认权限表是在自带auth模块,中permission表中

可以使用has_perm方法获取用户是否有这个权限
(二)Django自定义权限
(1)添加表

(2)settings文件中设置
AUTH_USER_MODEL ='repository.UserProfile' #上面的值表示Django应用的名称(必须位于INSTALLLED_APPS中)和你想使用的User模型的名称。
(3)在admin文件中设置展示内容

二:通用权限框架设计
(一)业务场景分析(如何去实现将不同权限分配给用户)

(二)权限管理分析


主要实现:将权限列表定义出来,与角色(用户组)相互关联就可以
权限列表实现:


(三)实现方法
(1)定义权限列表文件,将权限列表定义。设置为装饰器,根据每个用户访问的url去反解,获取到对应的权限列表名,从而去数据库中获取,使用has_prem获取是否拥有权限。从而在用户访问url时进行判断
permission_list.py(这部分最好是放入数据库中,可以改进)


resolve方法可以翻转获取url的数据


(2)permission文件,用于生成装饰器,验证权限列表

总结:

(3)钩子函数案例(使当前用户只能访问自己的客户)
