- RoleService: CRUD, assign_permissions, get_role_permissions - PermissionService: list all tenant permissions - Role handlers: 8 endpoints with RBAC permission checks - Frontend Roles page: table, create/edit modal, permission assignment - Frontend Roles API: full CRUD + permission operations - Routes registered in AuthModule protected_routes
218 lines
5.9 KiB
Rust
218 lines
5.9 KiB
Rust
use axum::Extension;
|
|
use axum::extract::{FromRef, Path, Query, State};
|
|
use axum::response::Json;
|
|
use validator::Validate;
|
|
|
|
use erp_core::error::AppError;
|
|
use erp_core::types::{ApiResponse, PaginatedResponse, Pagination, TenantContext};
|
|
use uuid::Uuid;
|
|
|
|
use crate::auth_state::AuthState;
|
|
use crate::dto::{AssignPermissionsReq, CreateRoleReq, PermissionResp, RoleResp, UpdateRoleReq};
|
|
use crate::middleware::rbac::require_permission;
|
|
use crate::service::permission_service::PermissionService;
|
|
use crate::service::role_service::RoleService;
|
|
|
|
/// GET /api/v1/roles
|
|
///
|
|
/// List roles within the current tenant with pagination.
|
|
/// Requires the `role.list` permission.
|
|
pub async fn list_roles<S>(
|
|
State(state): State<AuthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Query(pagination): Query<Pagination>,
|
|
) -> Result<Json<ApiResponse<PaginatedResponse<RoleResp>>>, AppError>
|
|
where
|
|
AuthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "role.list")?;
|
|
|
|
let (roles, total) = RoleService::list(ctx.tenant_id, &pagination, &state.db).await?;
|
|
|
|
let page = pagination.page.unwrap_or(1);
|
|
let page_size = pagination.limit();
|
|
let total_pages = (total + page_size - 1) / page_size;
|
|
|
|
Ok(Json(ApiResponse::ok(PaginatedResponse {
|
|
data: roles,
|
|
total,
|
|
page,
|
|
page_size,
|
|
total_pages,
|
|
})))
|
|
}
|
|
|
|
/// POST /api/v1/roles
|
|
///
|
|
/// Create a new role within the current tenant.
|
|
/// Requires the `role.create` permission.
|
|
pub async fn create_role<S>(
|
|
State(state): State<AuthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Json(req): Json<CreateRoleReq>,
|
|
) -> Result<Json<ApiResponse<RoleResp>>, AppError>
|
|
where
|
|
AuthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "role.create")?;
|
|
|
|
req.validate()
|
|
.map_err(|e| AppError::Validation(e.to_string()))?;
|
|
|
|
let role = RoleService::create(
|
|
ctx.tenant_id,
|
|
ctx.user_id,
|
|
&req.name,
|
|
&req.code,
|
|
&req.description,
|
|
&state.db,
|
|
&state.event_bus,
|
|
)
|
|
.await?;
|
|
|
|
Ok(Json(ApiResponse::ok(role)))
|
|
}
|
|
|
|
/// GET /api/v1/roles/:id
|
|
///
|
|
/// Fetch a single role by ID within the current tenant.
|
|
/// Requires the `role.read` permission.
|
|
pub async fn get_role<S>(
|
|
State(state): State<AuthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<Json<ApiResponse<RoleResp>>, AppError>
|
|
where
|
|
AuthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "role.read")?;
|
|
|
|
let role = RoleService::get_by_id(id, ctx.tenant_id, &state.db).await?;
|
|
Ok(Json(ApiResponse::ok(role)))
|
|
}
|
|
|
|
/// PUT /api/v1/roles/:id
|
|
///
|
|
/// Update editable role fields (name, description).
|
|
/// Requires the `role.update` permission.
|
|
pub async fn update_role<S>(
|
|
State(state): State<AuthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(id): Path<Uuid>,
|
|
Json(req): Json<UpdateRoleReq>,
|
|
) -> Result<Json<ApiResponse<RoleResp>>, AppError>
|
|
where
|
|
AuthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "role.update")?;
|
|
|
|
let role = RoleService::update(
|
|
id,
|
|
ctx.tenant_id,
|
|
ctx.user_id,
|
|
&req.name,
|
|
&req.description,
|
|
&state.db,
|
|
)
|
|
.await?;
|
|
Ok(Json(ApiResponse::ok(role)))
|
|
}
|
|
|
|
/// DELETE /api/v1/roles/:id
|
|
///
|
|
/// Soft-delete a role by ID within the current tenant.
|
|
/// System roles cannot be deleted.
|
|
/// Requires the `role.delete` permission.
|
|
pub async fn delete_role<S>(
|
|
State(state): State<AuthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<Json<ApiResponse<()>>, AppError>
|
|
where
|
|
AuthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "role.delete")?;
|
|
|
|
RoleService::delete(id, ctx.tenant_id, ctx.user_id, &state.db, &state.event_bus).await?;
|
|
|
|
Ok(Json(ApiResponse {
|
|
success: true,
|
|
data: None,
|
|
message: Some("角色已删除".to_string()),
|
|
}))
|
|
}
|
|
|
|
/// POST /api/v1/roles/:id/permissions
|
|
///
|
|
/// Replace all permission assignments for a role.
|
|
/// Requires the `role.update` permission.
|
|
pub async fn assign_permissions<S>(
|
|
State(state): State<AuthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(id): Path<Uuid>,
|
|
Json(req): Json<AssignPermissionsReq>,
|
|
) -> Result<Json<ApiResponse<()>>, AppError>
|
|
where
|
|
AuthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "role.update")?;
|
|
|
|
RoleService::assign_permissions(
|
|
id,
|
|
ctx.tenant_id,
|
|
ctx.user_id,
|
|
&req.permission_ids,
|
|
&state.db,
|
|
)
|
|
.await?;
|
|
|
|
Ok(Json(ApiResponse {
|
|
success: true,
|
|
data: None,
|
|
message: Some("权限分配成功".to_string()),
|
|
}))
|
|
}
|
|
|
|
/// GET /api/v1/roles/:id/permissions
|
|
///
|
|
/// Fetch all permissions assigned to a role.
|
|
/// Requires the `role.read` permission.
|
|
pub async fn get_role_permissions<S>(
|
|
State(state): State<AuthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<Json<ApiResponse<Vec<PermissionResp>>>, AppError>
|
|
where
|
|
AuthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "role.read")?;
|
|
|
|
let perms = RoleService::get_role_permissions(id, ctx.tenant_id, &state.db).await?;
|
|
Ok(Json(ApiResponse::ok(perms)))
|
|
}
|
|
|
|
/// GET /api/v1/permissions
|
|
///
|
|
/// List all permissions within the current tenant.
|
|
/// Requires the `permission.list` permission.
|
|
pub async fn list_permissions<S>(
|
|
State(state): State<AuthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
) -> Result<Json<ApiResponse<Vec<PermissionResp>>>, AppError>
|
|
where
|
|
AuthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "permission.list")?;
|
|
|
|
let perms = PermissionService::list(ctx.tenant_id, &state.db).await?;
|
|
Ok(Json(ApiResponse::ok(perms)))
|
|
}
|