编写权限路由

This commit is contained in:
wcq 2023-02-09 10:54:07 +08:00
parent 888efad38c
commit 535c71eb29
5 changed files with 105 additions and 6 deletions

View File

@ -0,0 +1,32 @@
from typing import Union
from sqlalchemy.orm import Session
from Exceptions.common import CommonException
from Model.AuthRuleModel import AuthRuleNode
def add_auth_rule_node(db: Session, name: str, belong: Union[int, None], node_type: str) -> AuthRuleNode:
parent_node = get_auth_rule_node_by_id(belong)
if not parent_node:
raise CommonException("父节点不存在")
if parent_node.node_type != 'category':
raise CommonException("父节点类型不为分类节点")
item = AuthRuleNode(name=name, belong=belong, node_type=node_type)
db.add(item)
db.commit()
db.refresh(item)
return item
def get_auth_rule_node_by_id(db: Session, node_id: int) -> AuthRuleNode:
auth_rule_node = db.query(AuthRuleNode).filter_by(id=node_id).first()
return auth_rule_node
def delete_auth_rule_node(db: Session, node_id: int):
pass
def get_auth_rule_tree(db: Session):
pass

View File

@ -4,13 +4,14 @@ from sqlalchemy import Column, String, Boolean, ForeignKey, Enum, Text, DateTime
from Utils.SqlAlchemyUtils import Base from Utils.SqlAlchemyUtils import Base
class AuthRule(Base): class AuthRuleNode(Base):
__tablename__ = "AUTH_RULE_TABLE" __tablename__ = "AUTH_RULE_NODE_TABLE"
id = Column(String(32), primary_key=True, comment="应用id") id = Column(Integer, primary_key=True, comment="规则节点id")
name = Column(String(32), unique=True, comment="应用名称") name = Column(String(32), unique=True, comment="规则节点名称")
secret_key = Column(String(255), comment="验证密匙") node_type = Column(Enum("category", "rule", name="节点类型"))
token_key = Column(String(255), comment="app随机token加密密匙", default=lambda: uuid.uuid4().hex) belong = Column(Integer, comment="所属节点")
def to_dict(self): def to_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns} return {c.name: getattr(self, c.name) for c in self.__table__.columns}

View File

@ -6,6 +6,7 @@ class Post(Base):
__tablename__ = "POST_TABLE" __tablename__ = "POST_TABLE"
id = Column(Integer, primary_key=True, comment="职务id") id = Column(Integer, primary_key=True, comment="职务id")
name = Column(String(32), comment="职务名称") name = Column(String(32), comment="职务名称")
default_auth = Column(Text, comment="职务默认权限")
def to_dict(self): def to_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns} return {c.name: getattr(self, c.name) for c in self.__table__.columns}

View File

@ -0,0 +1,35 @@
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from Schemas import AuthRuleSchemas
from Crud import AuthRuleCrud
from Utils.SqlAlchemyUtils import get_db
router = APIRouter(
tags=["权限规则"],
prefix="/api/auth_rule",
# dependencies=[Depends(auth_token_depend)]
)
@router.post("/add_auth_rule_node", summary="添加权限规则节点", response_model=AuthRuleSchemas.AuthRuleNodeInfo)
def add_auth_rule_node(body: AuthRuleSchemas.AuthRuleNodeAddInfo, db: Session = Depends(get_db)):
new_auth_rule_node = AuthRuleCrud.add_auth_rule_node(db, body.name, body.belong, body.node_type)
return new_auth_rule_node
@router.post("/delete_auth_rule_node", summary="删除权限规则节点")
def delete_auth_rule_node(body: AuthRuleSchemas.AuthRuleNodeId, db: Session = Depends(get_db)):
AuthRuleCrud.delete_auth_rule_node(db, body.id)
return {"msg": "删除成功", "state": 1}
@router.post("/delete_auth_rule_node", summary="批量删除权限规则节点")
def delete_auth_rule_node(body: AuthRuleSchemas.AuthRuleNodeId, db: Session = Depends(get_db)):
AuthRuleCrud.delete_auth_rule_node(db, body.id)
return {"msg": "删除成功", "state": 1}
@router.post("/change_auth_rule_node", summary="修改权限规矩节点")
def change_auth_rule_node(body: AuthRuleSchemas.AuthRuleNodeChangeInfo):
pass

View File

@ -0,0 +1,30 @@
from enum import Enum
from pydantic import BaseModel
from typing import Union
class AuthRuleNodeTypeEnum(Enum):
rule = 'rule'
category = 'category'
class AuthRuleNodeAddInfo(BaseModel):
name: str
belong: Union[None, int]
node_type: AuthRuleNodeTypeEnum
class AuthRuleNodeInfo(BaseModel):
id: int
name: str
belong: Union[None, int]
node_type: AuthRuleNodeTypeEnum
class AuthRuleNodeId(BaseModel):
id: int
class AuthRuleNodeIdList(BaseModel):
id_list: int