| | |
| | | import uuid |
| | | |
| | | from app.Log import logger |
| | | from app.models.resource_model import ResourceModel, ResourceTypeModel |
| | | from Log import logger |
| | | from app.models.resource_model import ResourceModel |
| | | from app.models.role_model import RoleModel |
| | | |
| | | |
| | | |
| | | async def role_list(db, page_size: int, page_index: int, keyword: str): |
| | | async def role_list(db, page_size: int, page_index: int, keyword: str, role_key:str, user_id): |
| | | query = db.query(RoleModel) |
| | | if role_key != "admin": |
| | | query.filter(RoleModel.creator==user_id) |
| | | if keyword: |
| | | query = query.filter(RoleModel.name.like('%{}%'.format(keyword))) |
| | | roles = query.order_by(RoleModel.id.desc()).limit(page_size).offset( |
| | |
| | | return {"total": query.count(), "rows": [role.to_json() for role in roles]} |
| | | |
| | | |
| | | async def create_role(db, role_name: str, description: str, user_id): |
| | | async def create_role(db, role_name: str, description: str, role_key, data_scope, user_id): |
| | | try: |
| | | role_model = RoleModel(id=str(uuid.uuid4()),name=role_name, description=description,creator=user_id) |
| | | role_model = RoleModel(id=str(uuid.uuid4()),name=role_name, description=description,creator=user_id, data_scope=data_scope) |
| | | if role_key: |
| | | role_model.roleKey = role_key |
| | | db.add(role_model) |
| | | db.commit() |
| | | db.refresh(role_model) |
| | |
| | | return True |
| | | |
| | | |
| | | async def edit_role_resource(db, role_id:str, role_name:str, description:str, resources:list): |
| | | async def edit_role_resource(db, role_id:str, role_name:str, description:str,role_key:str, data_scope:int, resources:list, edit_type:int): |
| | | try: |
| | | role = db.query(RoleModel).filter(RoleModel.id == role_id).first() |
| | | if role_name: role.name = role_name |
| | | if description: role.description = description |
| | | if resources: |
| | | if edit_type == 1: |
| | | role.name = role_name |
| | | role.description = description |
| | | |
| | | if role_key: |
| | | role.roleKey = role_key |
| | | if data_scope: |
| | | role.data_scope = data_scope |
| | | else: |
| | | role.resources = [db.get(ResourceModel, resourcesId) for resourcesId in resources] |
| | | db.add(role) |
| | | db.commit() |