5 次代码提交 2479082314 ... 3d4514e58d

作者 SHA1 备注 提交日期
  DYaiu 3d4514e58d 实现根据当前登入用户来获取对应角色的菜单 4 年之前
  DYaiu ee8a0845ca 两张表的默认view,路由菜单的crud 4 年之前
  DYaiu 4679af728e 补充两张表的序列化类与路由 4 年之前
  DYaiu 3661fd59e0 补充前端路由备份表一个标题字段 4 年之前
  DYaiu 7fcb0b522c 安装角色权限插件``django-role-permissions`` 4 年之前

+ 1 - 0
Pipfile

@@ -13,6 +13,7 @@ mysql = "*"
 mysqlclient = "*"
 django-filter = "*"
 django-mptt = "*"
+django-role-permissions = "*"
 
 [requires]
 python_version = "3.7"

+ 46 - 0
basic/migrations/0002_grouproutermenu_routermenu.py

@@ -0,0 +1,46 @@
+# Generated by Django 3.0.8 on 2020-08-06 11:20
+
+from django.db import migrations, models
+import django.db.models.deletion
+import mptt.fields
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('auth', '0011_update_proxy_permissions'),
+        ('basic', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='RouterMenu',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('name', models.CharField(max_length=50)),
+                ('path', models.CharField(max_length=50)),
+                ('url', models.CharField(max_length=50)),
+                ('lft', models.PositiveIntegerField(editable=False)),
+                ('rght', models.PositiveIntegerField(editable=False)),
+                ('tree_id', models.PositiveIntegerField(db_index=True, editable=False)),
+                ('level', models.PositiveIntegerField(editable=False)),
+                ('parent', mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='basic.RouterMenu')),
+            ],
+            options={
+                'verbose_name': '前端菜单',
+                'verbose_name_plural': '前端菜单列表',
+            },
+        ),
+        migrations.CreateModel(
+            name='GroupRouterMenu',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('group', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='auth.Group')),
+                ('menu', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='basic.RouterMenu')),
+            ],
+            options={
+                'verbose_name': '前端菜单与角色的中间表',
+                'verbose_name_plural': '前端菜单和角色的中间表列表',
+            },
+        ),
+    ]

+ 18 - 0
basic/migrations/0003_routermenu_title.py

@@ -0,0 +1,18 @@
+# Generated by Django 3.0.8 on 2020-08-07 02:38
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('basic', '0002_grouproutermenu_routermenu'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='routermenu',
+            name='title',
+            field=models.CharField(max_length=50, null=True),
+        ),
+    ]

+ 29 - 1
basic/models.py

@@ -1,4 +1,5 @@
-from django.contrib.auth.models import User
+from django.contrib.auth.models import User, Group
+from django.db.models import Model
 from mptt.fields import TreeForeignKey
 from mptt.models import MPTTModel
 from django.db import models
@@ -17,3 +18,30 @@ class Menu(MPTTModel):
         db_table = 'basic_menu'
         verbose_name = '报表菜单'
         verbose_name_plural = '报表菜单列表'
+
+
+class RouterMenu(MPTTModel):
+    """
+    前端路由备份,用作中间表做权限
+    """
+    name = models.CharField(max_length=50)
+    path = models.CharField(max_length=50)
+    title = models.CharField(max_length=50, null=True)
+    url = models.CharField(max_length=50)
+    parent = TreeForeignKey('self', blank=True, null=True, on_delete=models.CASCADE)
+
+    class Meta:
+        verbose_name = '前端菜单'
+        verbose_name_plural = '前端菜单列表'
+
+
+class GroupRouterMenu(Model):
+    """
+    前端菜单的权限与角色绑定
+    """
+    group = models.ForeignKey(Group, blank=True, null=True, on_delete=models.CASCADE)
+    menu = models.ForeignKey(RouterMenu, blank=True, null=True, on_delete=models.CASCADE)
+
+    class Meta:
+        verbose_name = '前端菜单与角色的中间表'
+        verbose_name_plural = '前端菜单和角色的中间表列表'

+ 15 - 1
basic/serializer.py

@@ -1,7 +1,7 @@
 from rest_framework import serializers
 from rest_framework.serializers import ListSerializer
 
-from basic.models import Menu
+from basic.models import Menu, RouterMenu, GroupRouterMenu
 
 
 class MenuSerializer(serializers.ModelSerializer):
@@ -9,3 +9,17 @@ class MenuSerializer(serializers.ModelSerializer):
         model = Menu
         list_serializer = ListSerializer
         fields = '__all__'
+
+
+class RouterMenuSerializer(serializers.ModelSerializer):
+    class Meta:
+        model = RouterMenu
+        list_serializer = ListSerializer
+        fields = '__all__'
+
+
+class GroupRouterMenuSerializer(serializers.ModelSerializer):
+    class Meta:
+        model = GroupRouterMenu
+        list_serializer = ListSerializer
+        fields = '__all__'

+ 4 - 2
basic/urls.py

@@ -1,12 +1,14 @@
 from rest_framework_bulk.routes import BulkRouter
 from django.conf.urls import url
-from basic.views import MenuView, StatisticsView
+from basic.views import MenuView, StatisticsView, GroupRouterMenuView, RouterMenuView
 
 router = BulkRouter()
 router.register(r'menu', MenuView)
+router.register(r'router/menu', RouterMenuView)
 
 urlpatterns = router.urls
 
 urlpatterns += [
-    url('statistics', StatisticsView.as_view())
+    url('statistics', StatisticsView.as_view()),
+    url(r'user/menu', GroupRouterMenuView.as_view())
 ]

+ 31 - 2
basic/views.py

@@ -1,11 +1,13 @@
+from django.contrib.auth.models import Group
 from django.db.models import Sum
 from rest_framework import status
 from rest_framework.response import Response
 from rest_framework.views import APIView
 from rest_framework.viewsets import ModelViewSet
+from rolepermissions.roles import get_user_roles
 
-from basic.models import Menu
-from basic.serializer import MenuSerializer
+from basic.models import Menu, RouterMenu, GroupRouterMenu
+from basic.serializer import MenuSerializer, RouterMenuSerializer, GroupRouterMenuSerializer
 from payment.models import Payment
 
 
@@ -21,3 +23,30 @@ class StatisticsView(APIView):
         data = list(
             Payment.objects.values('payment_type').annotate(price_sum=Sum('index_price')).order_by('-price_sum'))
         return Response(data, status=status.HTTP_200_OK)
+
+
+class GroupRouterMenuView(APIView):
+    queryset = Menu.objects.all()
+    serializer_class = GroupRouterMenuSerializer
+    pagination_class = None
+
+    def get(self, request):
+        user = request.user
+
+        if user.is_superuser:
+            return Response(RouterMenu.objects.all().values(), status=status.HTTP_200_OK)
+
+        role = get_user_roles(user)
+        # 根据当前用户的角色获取角色id
+        roleArr = Group.objects.filter(name__in=[r.get_name() for r in role]).all().values('id')
+        # 根据角色id查到所有相关的角色与菜单对应的记录
+        menu = GroupRouterMenu.objects.filter(group_id__in=[arr['id'] for arr in roleArr]).all()
+        # 生成菜单列表
+        data = [RouterMenuSerializer(m.menu).data for m in menu]
+        return Response(data, status=status.HTTP_200_OK)
+
+
+class RouterMenuView(ModelViewSet):
+    queryset = Menu.objects.all()
+    serializer_class = RouterMenuSerializer
+    pagination_class = None

+ 17 - 0
my_project/roles.py

@@ -0,0 +1,17 @@
+from rolepermissions.roles import AbstractUserRole
+
+
+class ReportManager(AbstractUserRole):
+    """
+    日报角色
+    """
+    available_permissions = {
+    }
+
+
+class PaymentManager(AbstractUserRole):
+    """
+    支出的角色
+    """
+    available_permissions = {
+    }

+ 3 - 0
my_project/settings.py

@@ -43,6 +43,7 @@ INSTALLED_APPS = [
     'treeAndTable',
     'payment',
     'basic',
+    'rolepermissions',
 ]
 
 MIDDLEWARE = [
@@ -142,3 +143,5 @@ REST_FRAMEWORK = {
 AUTHENTICATION_BACKENDS = (
     'django.contrib.auth.backends.ModelBackend'
 )
+
+ROLEPERMISSIONS_MODULE = 'my_project.roles'