You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
7.6 KiB
207 lines
7.6 KiB
using Microsoft.AspNetCore.Mvc;
|
|
using MediatR;
|
|
using Microsoft.Extensions.Logging;
|
|
using CellularManagement.Application.Features.RolePermissions.Commands.AddRolePermissions;
|
|
using CellularManagement.Application.Features.RolePermissions.Commands.DeleteRolePermissions;
|
|
using CellularManagement.Application.Features.RolePermissions.Queries.GetRolePermissions;
|
|
using CellularManagement.Application.Common;
|
|
using Microsoft.AspNetCore.Http;
|
|
using CellularManagement.Presentation.Abstractions;
|
|
using CellularManagement.Domain.Common;
|
|
|
|
namespace CellularManagement.Presentation.Controllers;
|
|
|
|
/// <summary>
|
|
/// 角色权限控制器
|
|
/// 提供角色权限管理相关的 API 接口,包括添加、删除和查询角色权限功能
|
|
/// </summary>
|
|
public class RolePermissionController : ApiController
|
|
{
|
|
private readonly ILogger<RolePermissionController> _logger;
|
|
|
|
/// <summary>
|
|
/// 初始化角色权限控制器
|
|
/// </summary>
|
|
/// <param name="mediator">MediatR 中介者,用于处理命令和查询</param>
|
|
/// <param name="logger">日志记录器</param>
|
|
public RolePermissionController(
|
|
IMediator mediator,
|
|
ILogger<RolePermissionController> logger) : base(mediator)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取角色权限
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 示例请求:
|
|
///
|
|
/// GET /api/role-permissions/{roleId}?includeDetails=true
|
|
///
|
|
/// </remarks>
|
|
/// <param name="roleId">角色ID</param>
|
|
/// <param name="includeDetails">是否包含权限详情</param>
|
|
/// <returns>
|
|
/// 角色权限信息,包含:
|
|
/// - 角色基本信息
|
|
/// - 权限列表
|
|
/// </returns>
|
|
/// <response code="200">获取成功,返回角色权限信息</response>
|
|
/// <response code="400">获取失败,返回错误信息</response>
|
|
[HttpGet("{roleId}")]
|
|
[ProducesResponseType(typeof(OperationResult<GetRolePermissionsResponse>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(OperationResult<GetRolePermissionsResponse>), StatusCodes.Status400BadRequest)]
|
|
public async Task<ActionResult<OperationResult<GetRolePermissionsResponse>>> GetRolePermissions(
|
|
string roleId,
|
|
[FromQuery] bool includeDetails = true)
|
|
{
|
|
try
|
|
{
|
|
var query = new GetRolePermissionsQuery
|
|
{
|
|
RoleId = roleId,
|
|
IncludeDetails = includeDetails
|
|
};
|
|
|
|
var result = await mediator.Send(query);
|
|
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogInformation(
|
|
"成功获取角色 {RoleId} 的权限信息,共 {Count} 个权限",
|
|
roleId,
|
|
result.Data?.Permissions.Count());
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning(
|
|
"获取角色 {RoleId} 的权限信息失败: {Error}",
|
|
roleId,
|
|
result.ErrorMessages?.FirstOrDefault());
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "获取角色 {RoleId} 的权限信息时发生异常", roleId);
|
|
return StatusCode(StatusCodes.Status500InternalServerError,
|
|
OperationResult<GetRolePermissionsResponse>.CreateFailure("系统错误,请稍后重试"));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加角色权限
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 示例请求:
|
|
///
|
|
/// POST /api/role-permissions
|
|
/// {
|
|
/// "roleId": "角色ID",
|
|
/// "permissionIds": ["权限ID1", "权限ID2"]
|
|
/// }
|
|
///
|
|
/// </remarks>
|
|
/// <param name="command">添加角色权限命令</param>
|
|
/// <returns>
|
|
/// 添加结果,包含:
|
|
/// - 成功:返回添加成功的权限ID列表
|
|
/// - 失败:返回错误信息
|
|
/// </returns>
|
|
/// <response code="200">添加成功,返回添加结果</response>
|
|
/// <response code="400">添加失败,返回错误信息</response>
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(OperationResult<AddRolePermissionsResponse>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(OperationResult<AddRolePermissionsResponse>), StatusCodes.Status400BadRequest)]
|
|
public async Task<ActionResult<OperationResult<AddRolePermissionsResponse>>> AddRolePermissions(
|
|
[FromBody] AddRolePermissionsCommand command)
|
|
{
|
|
try
|
|
{
|
|
var result = await mediator.Send(command);
|
|
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogInformation(
|
|
"成功为角色 {RoleId} 添加权限,添加成功:{SuccessCount},添加失败:{FailedCount}",
|
|
command.RoleId,
|
|
result.Data?.AddedPermissionIds.Count(),
|
|
result.Data?.FailedPermissionIds.Count());
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning(
|
|
"为角色 {RoleId} 添加权限失败: {Error}",
|
|
command.RoleId,
|
|
result.ErrorMessages?.FirstOrDefault());
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "为角色 {RoleId} 添加权限时发生异常", command.RoleId);
|
|
return StatusCode(StatusCodes.Status500InternalServerError,
|
|
OperationResult<AddRolePermissionsResponse>.CreateFailure("系统错误,请稍后重试"));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除角色权限
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 示例请求:
|
|
///
|
|
/// DELETE /api/role-permissions
|
|
/// {
|
|
/// "roleId": "角色ID",
|
|
/// "permissionIds": ["权限ID1", "权限ID2"]
|
|
/// }
|
|
///
|
|
/// </remarks>
|
|
/// <param name="command">删除角色权限命令</param>
|
|
/// <returns>
|
|
/// 删除结果,包含:
|
|
/// - 成功:返回删除的权限数量
|
|
/// - 失败:返回错误信息
|
|
/// </returns>
|
|
/// <response code="200">删除成功,返回删除结果</response>
|
|
/// <response code="400">删除失败,返回错误信息</response>
|
|
[HttpDelete]
|
|
[ProducesResponseType(typeof(OperationResult<DeleteRolePermissionsResponse>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(OperationResult<DeleteRolePermissionsResponse>), StatusCodes.Status400BadRequest)]
|
|
public async Task<ActionResult<OperationResult<DeleteRolePermissionsResponse>>> DeleteRolePermissions(
|
|
[FromBody] DeleteRolePermissionsCommand command)
|
|
{
|
|
try
|
|
{
|
|
var result = await mediator.Send(command);
|
|
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogInformation(
|
|
"成功从角色 {RoleId} 删除权限,删除数量:{DeletedCount},删除失败:{FailedCount}",
|
|
command.RoleId,
|
|
result.Data?.DeletedCount,
|
|
result.Data?.FailedPermissionIds.Count());
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning(
|
|
"从角色 {RoleId} 删除权限失败: {Error}",
|
|
command.RoleId,
|
|
result.ErrorMessages?.FirstOrDefault());
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "从角色 {RoleId} 删除权限时发生异常", command.RoleId);
|
|
return StatusCode(StatusCodes.Status500InternalServerError,
|
|
OperationResult<DeleteRolePermissionsResponse>.CreateFailure("系统错误,请稍后重试"));
|
|
}
|
|
}
|
|
}
|