django 权限管理

Posted zhangqing979797

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django 权限管理相关的知识,希望对你有一定的参考价值。

1.views.py

技术分享图片
 1 from django.db import models
 2 
 3 class Menu(models.Model):
 4     title = models.CharField(max_length=32, verbose_name=菜单)
 5     icon = models.CharField(max_length=32, verbose_name=图标, null=True, blank=True)
 6 
 7 
 8 class Permission(models.Model):
 9     """
10     权限表
11     """
12     title = models.CharField(max_length=32, verbose_name=标题,blank=True)
13     url = models.CharField(max_length=32, verbose_name=权限,blank=True)
14     menu=models.ForeignKey("Menu",on_delete=models.CASCADE,null=True,blank=True)
15     name=models.CharField(max_length=32,verbose_name=url别名,default="",blank=True)
16     pid=models.ForeignKey(self,on_delete=models.CASCADE,null=True,verbose_name="父权限",blank=True)
17 
18     # is_menu = models.BooleanField(default=False, verbose_name=‘是否是菜单‘)
19     # icon = models.CharField(max_length=32, verbose_name=‘图标‘, null=True, blank=True)
20     
21     class Meta:
22         verbose_name_plural = 权限表
23         verbose_name = 权限表
24     
25     def __str__(self):
26         return self.title
27 
28 
29 class Role(models.Model):
30     name = models.CharField(max_length=32, verbose_name=角色名称)
31     permissions = models.ManyToManyField(to=Permission, verbose_name=角色所拥有的权限, blank=True)
32     
33     def __str__(self):
34         return self.name
35 
36 
37 class User(models.Model):
38     """
39     用户表
40     """
41     name = models.CharField(max_length=32, verbose_name=用户名)
42     password = models.CharField(max_length=32, verbose_name=密码)
43     roles = models.ManyToManyField(to=Role, verbose_name=用户所拥有的角色, blank=True)
44     
45     def __str__(self):
46         return self.name
权限设置.py

2.html.py

技术分享图片
  1 {% extends web/layout.html %}
  2 {% load rbac %}
  3 {% block content %}
  4     <div class="luffy-container">
  5         <div class="col-md-3 user-area">
  6             <div class="panel panel-default">
  7                 <!-- Default panel contents -->
  8                 <div class="panel-heading">
  9                     <i class="fa fa-address-book-o" aria-hidden="true"></i> 用户信息
 10                 </div>
 11 
 12                 <div class="panel-body">
 13                     <ul>
 14                         {% for user in user_list %}
 15 
 16                             <li class= {% if user.id|safe == uid %} "active" {% endif %}>
 17                                 <a href="?uid={{ user.id }}">{{ user.name }}</a></li>
 18 
 19                         {% endfor %}
 20                     </ul>
 21                 </div>
 22 
 23             </div>
 24         </div>
 25 
 26         <div class="col-md-3 role-area">
 27             <form method="post">
 28                 {% csrf_token %}
 29                 <input type="hidden" name="postType" value="role">
 30                 <div class="panel panel-default">
 31                     <!-- Default panel contents -->
 32                     <div class="panel-heading">
 33                         <i class="fa fa-book" aria-hidden="true"></i> 角色
 34                         {% if uid %}
 35                             <button type="submit" class="right btn btn-success btn-xs"
 36                                     style="padding: 2px 8px;margin: -3px;">
 37                                 <i class="fa fa-save" aria-hidden="true"></i>
 38                                 保存
 39                             </button>
 40                         {% endif %}
 41                     </div>
 42                     <div class="panel-body" style="color: #d4d4d4;padding:10px  5px;">
 43                         提示:点击用户后才能为其分配角色
 44                     </div>
 45                     <table class="table">
 46                         <thead>
 47                         <tr>
 48                             <th>角色</th>
 49                             <th>选择</th>
 50                         </tr>
 51                         </thead>
 52                         <tbody>
 53                         {% for role in role_list %}
 54                             <tr {% if role.id|safe == rid %} class="active"  {% endif %}>
 55 
 56                                 <td><a href="?{% gen_role_url request role.id %}">{{ role.name }}</a></td>
 57                                 <td>
 58                                     {% if role.id in role_id_list %}
 59                                         <input type="checkbox" name="roles" value="{{ role.id }}" checked/>
 60                                     {% else %}
 61                                         <input  type="checkbox" name="roles" value="{{ role.id }}"/>
 62                                     {% endif %}
 63                                 </td>
 64                             </tr>
 65                         {% endfor %}
 66 
 67                         </tbody>
 68                     </table>
 69 
 70                 </div>
 71             </form>
 72         </div>
 73 
 74         <div class="col-md-6 permission-area">
 75             <form method="post">
 76                 {% csrf_token %}
 77                 <input type="hidden" name="postType" value="permission">
 78                 <div class="panel panel-default">
 79                     <!-- Default panel contents -->
 80                     <div class="panel-heading">
 81                         <i class="fa fa-sitemap" aria-hidden="true"></i> 权限分配
 82                         {% if rid %}
 83                             <button class="right btn btn-success btn-xs" style="padding: 2px 8px;margin: -3px;">
 84                                 <i class="fa fa-save" aria-hidden="true"></i>
 85                                 保存
 86                             </button>
 87                         {% endif %}
 88                     </div>
 89                     <div class="panel-body" style="color: #d4d4d4;padding: 10px 5px;">
 90                         提示:点击角色后,才能为其分配权限。
 91                     </div>
 92                     <table class="table" id="body">
 93                         <tbody>
 94 
 95                         </tbody>
 96                     </table>
 97                 </div>
 98             </form>
 99         </div>
100 
101     </div>
102 {% endblock %}
103 {% block js %}
104     <script>
105         $(function () {
106             bindRootPermissionClick();
107         });
108 
109         function bindRootPermissionClick() {
110             $(.permission-area).on(click, .root, function () {
111                 var caret = $(this).find(i);
112                 if (caret.hasClass(fa-caret-right)) {
113                     caret.removeClass(fa-caret-right).addClass(fa-caret-down);
114                     $(this).next().removeClass(hide);
115                 } else {
116                     caret.removeClass(fa-caret-down).addClass(fa-caret-right);
117                     $(this).next().addClass(hide);
118 
119                 }
120             })
121         }
122 
123 
124 
125         $.ajax({
126             url:"/permissions_tree/",
127             type:"get",
128             success:function(res){
129                 console.log(res);
130                 $.each(res,function(i,permission){
131                     console.log(i,permission);
132                     var menu_title=permission["menu__title"];
133                     var menu_pk=permission["menu__pk"];
134                     var url=permission["url"];
135                     var parent_id=permission["pid_id"];
136                     var pk=permission["pk"];
137                     var title=permission["title"];
138 
139                     if (menu_title){
140 
141                         if($("#menu_"+menu_pk).length){
142 
143 
144                              var s=`
145                                 <tr class=node id="per_${pk}">
146                                    <td><input  name="permissions_id" value=${pk} type="checkbox">${title}</td>
147                                 <tr>`;
148                              $("#menu_"+menu_pk).parent().append(s)
149                         }else{
150 
151                               var s=`<tr class=root id=menu_${menu_pk}><td>${menu_title}</td></tr>
152                                 <tr class=node id="per_${pk}">
153                                    <td><input  name="permissions_id" value=${pk} type="checkbox">${title}</td>
154                                 <tr>`;
155                               $("#body").append(s);
156                         }
157 
158                     }else{
159                          var s=`
160 
161                           <td><input name="permissions_id" value=${pk} type="checkbox">${title}</td>
162                         `;
163                              $("#per_"+parent_id).append(s)
164 
165                     }
166 
167 
168                 })
169 
170 
171                 var per_id_list={{ per_id_list }}
172                 $.each(per_id_list,function(i,j){
173                     console.log($("[value=‘"+j+"‘]")[0])
174                     $("#body [value=‘"+j+"‘]").prop("checked",true)
175                 })
176             }
177         });
178 
179 
180     </script>
181 {% endblock %}
html

3.

技术分享图片
1 @register.simple_tag
2 def gen_role_url(request, rid):
3     params = request.GET.copy()
4     params._mutable = True
5     params[rid] = rid
6     return params.urlencode()
过滤器

 

以上是关于django 权限管理的主要内容,如果未能解决你的问题,请参考以下文章

Django之权限管理

Django之路- 如何开发通用且万能的的权限框架组件

Django之路- 如何开发通用且万能的的权限框架组件

gitlab 权限说明

Django管理图标和图像

Django-权限管理