lablup / lablup/backend.ai

Implement RBAC DTOs and custom exceptions

Open
#7,307 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
670
Forks
183
Avg merge
17h 7m
Merged PRs (30d)
358

Description

## Overview

Implement all Data Transfer Objects (DTOs) and custom exceptions for the RBAC (Role-Based Access Control) management REST API. This forms the foundational type layer that will be used by adapters and handlers.

## Scope

### 1. Request DTOs

Location: `src/ai/backend/common/dto/manager/rbac/request.py`

- CreateRoleRequest: Role creation with name, source, status, description
- UpdateRoleRequest: Role modification with optional fields
- SearchRolesRequest: Role search with filters, orders, pagination
- RoleFilter: Filter conditions for role queries (name, source, status)
- RoleOrder: Sorting options for role lists
- AssignRoleRequest: Role assignment with user_id, role_id, granted_by
- RevokeRoleRequest: Role revocation with user_id, role_id
- SearchUsersAssignedToRoleRequest: Search users by role with filters
- AssignedUserFilter: Filter conditions for assigned users
- AssignedUserOrder: Sorting options for assigned user lists

### 2. Response DTOs

Location: `src/ai/backend/common/dto/manager/rbac/response.py`

- RoleDTO: Core role data model (id, name, source, status, timestamps, description)
- CreateRoleResponse: Response for role creation
- GetRoleResponse: Response for single role retrieval
- SearchRolesResponse: Response for role list with pagination
- UpdateRoleResponse: Response for role update
- DeleteRoleResponse: Response for role deletion
- AssignRoleResponse: Response for role assignment
- RevokeRoleResponse: Response for role revocation
- SearchUsersAssignedToRoleResponse: Response for assigned users list
- AssignedUserDTO: User role assignment data model
- PaginationInfo: Common pagination metadata (total, offset, limit)

### 3. Type Definitions

Location: `src/ai/backend/common/dto/manager/rbac/types.py`

- RoleSource: Enum (BUILTIN, CUSTOM)
- RoleStatus: Enum (ACTIVE, INACTIVE)

### 4. Path Parameter DTOs

Location: `src/ai/backend/manager/dto/rbac_request.py`

- GetRolePathParam: Path parameter for GET /roles/{role_id}
- UpdateRolePathParam: Path parameter for PATCH /roles/{role_id}
- DeleteRolePathParam: Path parameter for DELETE /roles/{role_id}
- SearchUsersAssignedToRolePathParam: Path parameter for role user search

### 5. Custom Exceptions

Location: `src/ai/backend/manager/errors/permission.py`

- InsufficientPermission: Raised when user lacks required permissions
- RoleNotFound: Raised when requested role doesn't exist
- RoleAlreadyExists: Raised when creating duplicate role
- UserRoleAssignmentNotFound: Raised when assignment doesn't exist

## Technical Requirements

- Request DTOs: Inherit from BaseRequestModel (Pydantic)
- Response DTOs: Inherit from BaseResponseModel (Pydantic)
- Exceptions: Inherit from BackendAIError and aiohttp web exceptions
- Use Pydantic Field validators for all DTOs
- Add appropriate constraints (max_length, min_value, etc.)
- Include comprehensive field descriptions
- All fields must have type annotations
- Use Optional[] for nullable fields
- Use proper datetime types from Python stdlib

## Acceptance Criteria

- All 10 Request DTO classes implemented with validation
- All 11 Response DTO classes implemented
- All enum types defined (RoleSource, RoleStatus)
- All 4 path parameter DTOs implemented
- All 4 custom exceptions implemented with proper error codes
- Type checks pass (`pants check`)
- All DTOs properly exported in `__all__`
- Field descriptions added for API documentation

## Estimated Effort

**Story Points**: 5
**Lines of Code**: ~550 lines

## Related Issues

- GitHub: #7189
- Parent Branch: feat/rearrange-rbac-data-type
- Blocks: BA-3378 (Adapters need DTOs)
- Blocks: BA-3379 (Handlers need DTOs)

JIRA Issue: BA-3377

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.