Skip to content

Commit

Permalink
Merge pull request #1033 from longguikeji/feature-402
Browse files Browse the repository at this point in the history
Feature 402
  • Loading branch information
hanbinloop authored Jul 14, 2022
2 parents f4e7d96 + e263c64 commit 5ea69a4
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 44 deletions.
36 changes: 16 additions & 20 deletions api/v1/pages/mine/grant_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
name = _("授权管理")


page = pages.TablePage(
page = pages.TreePage(
name=name,
tag=tag
)
permission_page = pages.TablePage(
name=_("申请权限"),
select=True,
)
app_permission_page = pages.TablePage(name=_("该应用权限"))
# permission_page = pages.TablePage(
# name=_("申请权限"),
# select=True,
# )


pages.register_front_pages(page)
pages.register_front_pages(permission_page)
pages.register_front_pages(app_permission_page)
# pages.register_front_pages(permission_page)

router = routers.FrontRouter(
path=tag,
Expand All @@ -29,25 +31,19 @@

page.create_actions(
init_action=actions.DirectAction(
path='/api/v1/mine/tenant/{tenant_id}/permissions/',
path='/api/v1/tenant/{tenant_id}/all_apps_in_arkid/',
method=actions.FrontActionMethod.GET,
),
global_actions={
'open': actions.OpenAction(
name=("申请权限"),
page=permission_page
node_actions=[
actions.CascadeAction(
page=app_permission_page
)
}
]
)

permission_page.create_actions(
app_permission_page.create_actions(
init_action=actions.DirectAction(
path='/api/v1/mine/tenant/{tenant_id}/all_permissions/',
method=actions.FrontActionMethod.GET,
path='/api/v1/mine/tenant/{tenant_id}/permissions/?app_id={app_id}',
method=actions.FrontActionMethod.GET
),
global_actions={
'confirm': actions.ConfirmAction(
path="/api/v1/mine/tenant/{tenant_id}/permissions/"
),
}
)
18 changes: 17 additions & 1 deletion api/v1/schema/mine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from arkid.core.schema import ResponseSchema
from ninja import ModelSchema, Schema
from typing import List, Optional
from arkid.core.models import App, Tenant, User
from arkid.core.models import App, Tenant, User, Permission
from pydantic import Field
from arkid.core import pages,actions
from arkid.core.translation import gettext_default as _


Expand Down Expand Up @@ -36,6 +37,21 @@ class Config:
model = Tenant
model_fields = ["id", "name", "slug", "icon"]

class MinePermissionListSchemaOut(Schema):

id: UUID = Field(hidden=True)
name: str
# app_name: str = Field(default=None, alias="app.name", title=_("应用名称"))
category: str
sort_id: int = Field(hidden=True)
in_current: bool = Field(item_action={"path":"/api/v1/mine/tenant/{tenant_id}/permissions/{permission_id}/add_permisssion", "method":actions.FrontActionMethod.GET.value, "close": False})
# is_system: bool
# is_open: bool = Field(item_action={"path":"/api/v1/tenant/{tenant_id}/permission/{id}/toggle_open", "method":actions.FrontActionMethod.POST.value})

# class Config:
# model = Permission
# model_fields = ['id', 'name', 'category', 'is_system']


class MineTenantListOut(ResponseSchema):
data: List[MineTenantListItemOut]
Expand Down
2 changes: 1 addition & 1 deletion api/v1/views/approve_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def create_approve_action(request, tenant_id: str, data: ApproveActionCreateIn):
"""创建审批动作"""
extension = Extension.valid_objects.get(id=data.extension_id)
action = ApproveAction.valid_objects.filter(
path=data.path, method=data.method, extension=extension, tenant=request.tenant
path=data.path, method=data.method, tenant=request.tenant
).first()
if action:
return ErrorDict(ErrorCode.APPROVE_ACTION_DUPLICATED)
Expand Down
65 changes: 46 additions & 19 deletions api/v1/views/mine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from typing import List
from django.shortcuts import render
from arkid.core.api import api, operation
from arkid.core.error import ErrorCode, ErrorDict
from arkid.core.translation import gettext_default as _
from arkid.core.pagenation import CustomPagination
from arkid.core.models import App, Tenant, ApproveRequest, User
from arkid.core.constants import NORMAL_USER, TENANT_ADMIN, PLATFORM_ADMIN
from ninja.pagination import paginate
from django.db.models import Q
from ..schema.mine import MineAppsOut, MineLogoutOut, MineTenantListItemOut, ProfileSchemaOut, ProfileSchemaIn, MineTenantListOut
from ..schema.mine import *


@api.get("/mine/tenant/{tenant_id}/apps/", tags=["我的"], response=MineAppsOut)
Expand Down Expand Up @@ -47,25 +48,51 @@ def update_mine_profile(request, tenant_id: str, data: ProfileSchemaIn):
return user


@api.get("/mine/tenant/{tenant_id}/permissions/", tags=["我的"])
@api.get("/mine/tenant/{tenant_id}/permissions/", response=List[MinePermissionListSchemaOut], tags=["我的"])
@operation(roles=[NORMAL_USER, TENANT_ADMIN, PLATFORM_ADMIN])
def get_mine_permissions(request, tenant_id: str):
"""我的权限列表,TODO"""
return []


@api.post("/mine/tenant/{tenant_id}/permissions/", tags=["我的"])
@operation(roles=[NORMAL_USER, TENANT_ADMIN, PLATFORM_ADMIN])
def update_mine_permissions(request, tenant_id: str):
"""更新我的权限列表,TODO"""
return []


@api.get("/mine/tenant/{tenant_id}/all_permissions/", tags=["我的"])
@operation(roles=[NORMAL_USER, TENANT_ADMIN, PLATFORM_ADMIN])
def get_mine_all_permissions(request, tenant_id: str):
"""获取所有权限并附带是否已授权给我的状态"""
return []
@paginate(CustomPagination)
def get_mine_permissions(request, tenant_id: str, app_id: str = None, app_name: str = None, category: str = None):
"""我的权限列表"""
login_user = request.user
from arkid.core.perm.permission_data import PermissionData
permissiondata = PermissionData()
items = permissiondata.get_permissions_by_mine_search(tenant_id, app_id, None, None, login_user, app_name=app_name, category=category)
return items


# @api.get("/mine/tenant/{tenant_id}/permissions/{permission_id}/open", tags=["我的"])
# @operation(roles=[NORMAL_USER, TENANT_ADMIN, PLATFORM_ADMIN])
# def update_mine_permissions(request, tenant_id: str, permission_id: str, in_current: bool):
# """更新我的权限列表"""
# if in_current is False:
# return ErrorDict(ErrorCode.PERMISSION_NOT_CLOSE)
# # 需要申请更新权限列表

# return {'error': ErrorCode.OK.value}


@api.get("/mine/tenant/{tenant_id}/permissions/{permission_id}/add_permisssion", tags=['权限'])
@operation(roles=[NORMAL_USER])
def mine_add_permission(request, tenant_id: str, permission_id: str):
'''
添加用户权限
'''
from arkid.core.event import Event, dispatch_event
from arkid.core.event import ADD_USER_MANY_PERMISSION
user = request.user
if user:
dispatch_event(Event(tag=ADD_USER_MANY_PERMISSION, tenant=request.tenant, request=request, data={
'user_ids': [str(user.id)],
'tenant_id': tenant_id,
'data_arr': [permission_id]
}))
return {'error': ErrorCode.OK.value}

# @api.get("/mine/tenant/{tenant_id}/all_permissions/", tags=["我的"])
# @operation(roles=[NORMAL_USER, TENANT_ADMIN, PLATFORM_ADMIN])
# def get_mine_all_permissions(request, tenant_id: str):
# """获取所有权限并附带是否已授权给我的状态"""
# return []


from api.v1.schema.approve_request import (
Expand Down
36 changes: 35 additions & 1 deletion arkid/core/approve.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from arkid.common.logger import logger
from django.core.handlers.wsgi import WSGIRequest
from django.urls import resolve
from arkid.core.models import ApproveRequest
from arkid.core.models import ApproveRequest, ApproveAction
from arkid.extension.models import Extension


def restore_approve_request(approve_request):
Expand Down Expand Up @@ -35,3 +36,36 @@ def create_approve_request(http_request, user, approve_action):
body=http_request.body,
)
return approve_request


def create_approve_action(
name,
path,
method,
description=None,
extension=None,
tenant=None,
):
"""
如果tenant为None, 则为平台级别审批动作,对所有租户起作用
"""
action = ApproveAction.valid_objects.filter(
path=path, method=method, tenant=tenant
).first()
if action:
return action

if not extension:
extension = Extension.valid_objects.get(
package='com.longgui.approve.system.arkid'
)

action = ApproveAction.valid_objects.create(
name=name,
description=description,
path=path,
method=method,
extension=extension,
tenant=tenant,
)
return action
8 changes: 7 additions & 1 deletion arkid/core/approve_request_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ def process_view(self, request, view_func, view_args, view_kwargs):
path = ('/' + request.resolver_match.route).replace("<", "{").replace(">", "}")
method = request.method


approve_action = ApproveAction.valid_objects.filter(
tenant=tenant, path=path, method=method
).first()
if not approve_action:
approve_action = ApproveAction.valid_objects.filter(
tenant=None,
path=path,
method=method,
).first()

if not approve_action or not approve_action.extension:
return None

Expand Down
2 changes: 1 addition & 1 deletion arkid/core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def ready(self):
tenant.create_tenant_user_admin_permission(user)
tenant.users.add(user)
tenant.save()

except Exception as e:
print(e)

# 监听
from arkid.core import listener
from arkid.core import preset_approve_action
1 change: 1 addition & 0 deletions arkid/core/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ErrorCode(Enum):
PERMISSION_EXISTS_ERROR = ('10033', _('the permission not exists', '该权限不存在'))
# APP_EXISTS_ERROR = '10032'
PERMISSION_NOT_EDIT = ('10033', _('the permission not edit', '该权限不允许编辑'))
PERMISSION_NOT_CLOSE = ('10033', _('the permission not edit', '该权限不允许关闭'))
# PERMISSION_NOT_DELETE = ('10034', _('the permission not delete', '该权限不允许删除'))
BAN_REMOVE_GROUP_PERMISSION = ('10035', _('ban remove group permission', '该分组权限或范围不允许移除'))
BAN_REMOVE_GROUP_SCOPE = ('10036', _('ban remove group permission', '该分组范围不允许移除'))
Expand Down
19 changes: 19 additions & 0 deletions arkid/core/migrations/0017_alter_approveaction_tenant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.13 on 2022-07-14 08:26

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('core', '0016_grouppermissionresult'),
]

operations = [
migrations.AlterField(
model_name='approveaction',
name='tenant',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='approve_action_set', related_query_name='actions', to='core.tenant', verbose_name='Tenant'),
),
]
1 change: 1 addition & 0 deletions arkid/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ class Meta(object):
tenant = models.ForeignKey(
'Tenant',
default=None,
null=True,
on_delete=models.CASCADE,
verbose_name=_('Tenant', '租户'),
related_name="approve_action_set",
Expand Down
Loading

0 comments on commit 5ea69a4

Please sign in to comment.