142 lines
4.2 KiB
Python
142 lines
4.2 KiB
Python
from sqlalchemy.orm import Session
|
|
from fastapi import HTTPException, status
|
|
from app.models.auth.role_model import Role
|
|
from app.schemas.auth.role_schema import (
|
|
RoleCreate,
|
|
RoleUpdate,
|
|
RoleResponse,
|
|
RoleWithAccessesResponse,
|
|
RolePaginatedResponse,
|
|
)
|
|
from app.services.auth.role_service import RoleService
|
|
from typing import List, Optional
|
|
import uuid
|
|
|
|
class RoleController:
|
|
@staticmethod
|
|
def create_role(
|
|
db: Session, role_data: RoleCreate, tenant_id: uuid.UUID = None
|
|
) -> Role:
|
|
if tenant_id:
|
|
role_data.tenant_id = tenant_id
|
|
return RoleService.create_role(db, role_data)
|
|
|
|
@staticmethod
|
|
def get_all_roles(db: Session, tenant_id: uuid.UUID = None) -> List[Role]:
|
|
return RoleService.get_all_roles(db, tenant_id)
|
|
|
|
@staticmethod
|
|
def get_role_by_id(
|
|
db: Session,
|
|
role_id: uuid.UUID,
|
|
*,
|
|
actor_tenant_id: Optional[uuid.UUID],
|
|
actor_is_superadmin: bool,
|
|
) -> Role:
|
|
return RoleService.get_role_by_id(
|
|
db,
|
|
role_id,
|
|
actor_tenant_id=actor_tenant_id,
|
|
actor_is_superadmin=actor_is_superadmin,
|
|
)
|
|
|
|
@staticmethod
|
|
def update_role(
|
|
db: Session,
|
|
role_id: uuid.UUID,
|
|
role_data: RoleUpdate,
|
|
is_superadmin: bool = False,
|
|
*,
|
|
actor_tenant_id: Optional[uuid.UUID] = None,
|
|
) -> Role:
|
|
return RoleService.update_role(
|
|
db,
|
|
role_id,
|
|
role_data,
|
|
is_superadmin=is_superadmin,
|
|
actor_tenant_id=actor_tenant_id,
|
|
)
|
|
|
|
@staticmethod
|
|
def delete_role(
|
|
db: Session,
|
|
role_id: uuid.UUID,
|
|
is_superadmin: bool = False,
|
|
*,
|
|
actor_tenant_id: Optional[uuid.UUID] = None,
|
|
):
|
|
return RoleService.delete_role(
|
|
db, role_id, is_superadmin=is_superadmin, actor_tenant_id=actor_tenant_id
|
|
)
|
|
|
|
@staticmethod
|
|
def get_role_with_accesses(
|
|
db: Session,
|
|
role_id: uuid.UUID,
|
|
*,
|
|
actor_tenant_id: Optional[uuid.UUID],
|
|
actor_is_superadmin: bool,
|
|
) -> RoleWithAccessesResponse:
|
|
role = RoleService.get_role_by_id(
|
|
db,
|
|
role_id,
|
|
actor_tenant_id=actor_tenant_id,
|
|
actor_is_superadmin=actor_is_superadmin,
|
|
)
|
|
|
|
accesses = [
|
|
{
|
|
"id": str(ra.access.id),
|
|
"access_code": ra.access.access_code,
|
|
"category": ra.access.category,
|
|
"name": ra.access.name,
|
|
"parent_id": str(ra.access.parent_id) if ra.access.parent_id else None,
|
|
}
|
|
for ra in role.role_accesses
|
|
]
|
|
|
|
accesses.extend([
|
|
{
|
|
"id": str(rma.module_access.id),
|
|
"access_code": rma.module_access.access_code,
|
|
"category": rma.module_access.category,
|
|
"name": rma.module_access.name,
|
|
"parent_id": str(rma.module_access.parent_id) if rma.module_access.parent_id else None,
|
|
}
|
|
for rma in role.role_module_accesses
|
|
])
|
|
|
|
return RoleWithAccessesResponse(
|
|
id=role.id,
|
|
role_name=role.role_name,
|
|
tenant_id=role.tenant_id,
|
|
created_at=role.created_at,
|
|
updated_at=role.updated_at,
|
|
accesses=accesses,
|
|
)
|
|
|
|
@staticmethod
|
|
def get_roles_paginated(
|
|
db: Session,
|
|
current_user_tenant_id: Optional[uuid.UUID],
|
|
page: int = 1,
|
|
page_size: int = 10,
|
|
search: Optional[str] = None,
|
|
filter_role_names: Optional[List[str]] = None,
|
|
filter_tenant_ids: Optional[List[uuid.UUID]] = None,
|
|
sort_by: Optional[str] = None,
|
|
sort_order: Optional[str] = None,
|
|
) -> RolePaginatedResponse:
|
|
tenant_id = None if current_user_tenant_id is None else current_user_tenant_id
|
|
return RoleService.get_roles_paginated(
|
|
db=db,
|
|
tenant_id=tenant_id,
|
|
page=page,
|
|
page_size=page_size,
|
|
search=search,
|
|
filter_role_names=filter_role_names,
|
|
filter_tenant_ids=filter_tenant_ids,
|
|
sort_by=sort_by,
|
|
sort_order=sort_order,
|
|
)
|