-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #805 from longguikeji/feature-12
feat: 🎸 权限部分
- Loading branch information
Showing
8 changed files
with
183 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
scim_sync, | ||
webhook, | ||
send_sms, | ||
permission, | ||
tenant, | ||
account_life, | ||
app_protocol, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,125 @@ | ||
|
||
from ninja import Schema | ||
from ninja import Field | ||
from ninja import ModelSchema | ||
from arkid.core.api import api | ||
from arkid.core.translation import gettext_default as _ | ||
from typing import List, Optional | ||
from django.db import transaction | ||
from ninja.pagination import paginate | ||
from arkid.core.error import ErrorCode | ||
from arkid.core.models import Permission | ||
from django.shortcuts import get_object_or_404 | ||
from arkid.core.event import Event, dispatch_event | ||
from arkid.core.event import CREATE_PERMISSION, UPDATE_PERMISSION, DELETE_PERMISSION | ||
from uuid import UUID | ||
|
||
import uuid | ||
|
||
|
||
class PermissionListSchemaOut(ModelSchema): | ||
|
||
class Config: | ||
model = Permission | ||
model_fields = ['id', 'name', 'category', 'is_system'] | ||
|
||
|
||
class PermissionSchemaOut(Schema): | ||
permission_id: str | ||
|
||
|
||
class PermissionSchemaIn(ModelSchema): | ||
|
||
app_id: str = None | ||
parent_id: str = None | ||
|
||
class Config: | ||
model = Permission | ||
model_fields = ['name', 'category'] | ||
|
||
|
||
class PermissionDetailSchemaOut(ModelSchema): | ||
|
||
app_id: UUID = Field(default=None) | ||
parent_id: UUID = Field(default=None) | ||
|
||
class Config: | ||
model = Permission | ||
model_fields = ['id', 'name', 'category'] | ||
|
||
|
||
@transaction.atomic | ||
@api.post("/{tenant_id}/permissions", response=PermissionSchemaOut, tags=['权限'], auth=None) | ||
def create_permission(request, tenant_id: str, data: PermissionSchemaIn): | ||
''' | ||
权限创建 | ||
''' | ||
permission = Permission() | ||
permission.tenant_id = tenant_id | ||
permission.name = data.name | ||
permission.category = data.category | ||
permission.code = 'other_{}'.format(uuid.uuid4()) | ||
if data.parent_id: | ||
permission.parent_id = data.parent_id | ||
if data.app_id: | ||
permission.app_id = data.app_id | ||
permission.is_system = False | ||
permission.save() | ||
# 分发事件开始 | ||
result = dispatch_event(Event(tag=CREATE_PERMISSION, tenant=request.tenant, request=request, data=permission)) | ||
# 分发事件结束 | ||
return {"permission_id": permission.id.hex} | ||
|
||
|
||
@api.get("/tenant/{tenant_id}/permissions/", tags=["权限"],auth=None) | ||
def get_permissions(request, tenant_id: str): | ||
""" 权限列表,TODO | ||
""" | ||
return [] | ||
@api.get("/{tenant_id}/permissions", response=List[PermissionListSchemaOut], tags=['权限'], auth=None) | ||
@paginate | ||
def list_permissions(request, tenant_id: str, parent_id: str = None): | ||
''' | ||
权限列表 | ||
''' | ||
permissions = Permission.valid_objects.filter( | ||
tenant_id=tenant_id | ||
) | ||
if parent_id: | ||
permissions = permissions.filter(parent_id=parent_id) | ||
return permissions | ||
|
||
@api.get(operation_id="",path="/tenant/{tenant_id}/permissions/{id}/", tags=["权限"],auth=None) | ||
def get_permission(request, tenant_id: str, id: str): | ||
""" 获取权限,TODO | ||
""" | ||
return {} | ||
|
||
@api.post("/tenant/{tenant_id}/permissions/", tags=["权限"],auth=None) | ||
def create_permission(request, tenant_id: str): | ||
""" 创建权限,TODO | ||
""" | ||
return {} | ||
@api.get("/{tenant_id}/permission/{permission_id}", response=PermissionDetailSchemaOut, tags=['权限'], auth=None) | ||
def get_permission(request, tenant_id: str, permission_id: str): | ||
''' | ||
获取权限 | ||
''' | ||
permission = get_object_or_404(Permission, id=permission_id, is_del=False) | ||
return permission | ||
|
||
@api.put("/tenant/{tenant_id}/permissions/{id}/", tags=["权限"],auth=None) | ||
def update_permission(request, tenant_id: str, id: str): | ||
""" 编辑权限,TODO | ||
""" | ||
return {} | ||
|
||
@api.delete("/tenant/{tenant_id}/permissions/{id}/", tags=["权限"],auth=None) | ||
def delete_permission(request, tenant_id: str, id: str): | ||
""" 删除权限,TODO | ||
""" | ||
return {} | ||
@api.put("/{tenant_id}/permission/{permission_id}", tags=['权限'], auth=None) | ||
def update_permission(request, tenant_id: str, permission_id: str, data: PermissionSchemaIn): | ||
''' | ||
修改权限 | ||
''' | ||
permission = get_object_or_404(Permission, id=permission_id, is_del=False) | ||
permission.name = data.name | ||
permission.category = data.category | ||
if data.parent_id: | ||
permission.parent_id = data.parent_id | ||
if data.app_id: | ||
permission.app_id = data.app_id | ||
permission.save() | ||
# 分发事件开始 | ||
dispatch_event(Event(tag=UPDATE_PERMISSION, tenant=request.tenant, request=request, data=permission)) | ||
# 分发事件结束 | ||
return {'error': ErrorCode.OK.value} | ||
|
||
|
||
@api.delete("/{tenant_id}/permission/{permission_id}", tags=['权限'], auth=None) | ||
def delete_permission(request, tenant_id: str, permission_id: str): | ||
''' | ||
删除权限 | ||
''' | ||
permission = get_object_or_404(Permission, id=permission_id, is_del=False) | ||
# 分发事件开始 | ||
dispatch_event(Event(tag=DELETE_PERMISSION, tenant=request.tenant, request=request, data=permission)) | ||
# 分发事件结束 | ||
permission.delete() | ||
return {'error': ErrorCode.OK.value} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
arkid/core/migrations/0005_merge_0003_auto_20220429_0847_0004_grouppermission.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Generated by Django 3.2.13 on 2022-05-05 02:34 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0003_auto_20220429_0847'), | ||
('core', '0004_grouppermission'), | ||
] | ||
|
||
operations = [ | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters