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( State(state): State, Extension(ctx): Extension, Query(pagination): Query, ) -> Result>>, AppError> where AuthState: FromRef, 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( State(state): State, Extension(ctx): Extension, Json(req): Json, ) -> Result>, AppError> where AuthState: FromRef, 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( State(state): State, Extension(ctx): Extension, Path(id): Path, ) -> Result>, AppError> where AuthState: FromRef, 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( State(state): State, Extension(ctx): Extension, Path(id): Path, Json(req): Json, ) -> Result>, AppError> where AuthState: FromRef, 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( State(state): State, Extension(ctx): Extension, Path(id): Path, ) -> Result>, AppError> where AuthState: FromRef, 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( State(state): State, Extension(ctx): Extension, Path(id): Path, Json(req): Json, ) -> Result>, AppError> where AuthState: FromRef, 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( State(state): State, Extension(ctx): Extension, Path(id): Path, ) -> Result>>, AppError> where AuthState: FromRef, 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( State(state): State, Extension(ctx): Extension, ) -> Result>>, AppError> where AuthState: FromRef, 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))) }