2026-01-17 14:18:00 +05:30
|
|
|
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) -> Role:
|
|
|
|
|
return RoleService.get_role_by_id(db, role_id)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def update_role(
|
|
|
|
|
db: Session,
|
|
|
|
|
role_id: uuid.UUID,
|
|
|
|
|
role_data: RoleUpdate,
|
|
|
|
|
is_superadmin: bool = False,
|
|
|
|
|
) -> Role:
|
|
|
|
|
return RoleService.update_role(
|
|
|
|
|
db, role_id, role_data, is_superadmin=is_superadmin
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def delete_role(db: Session, role_id: uuid.UUID, is_superadmin: bool = False):
|
|
|
|
|
return RoleService.delete_role(db, role_id, is_superadmin=is_superadmin)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_role_with_accesses(
|
|
|
|
|
db: Session, role_id: uuid.UUID
|
|
|
|
|
) -> RoleWithAccessesResponse:
|
|
|
|
|
role = RoleService.get_role_by_id(db, role_id)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
]
|
2026-02-02 17:33:35 +05:30
|
|
|
|
|
|
|
|
# Add Module Permissions
|
|
|
|
|
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
|
|
|
|
|
])
|
2026-01-17 14:18:00 +05:30
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
) -> 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,
|
|
|
|
|
)
|