namespace Fengling.Console.Controllers;
///
/// 角色管理控制器
/// 提供角色的增删改查以及用户角色关联管理功能
///
[ApiController]
[Route("api/console/[controller]")]
[Authorize(AuthenticationSchemes = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme)]
public class RolesController : ControllerBase
{
private readonly IRoleService _roleService;
private readonly ILogger _logger;
public RolesController(IRoleService roleService, ILogger logger)
{
_roleService = roleService;
_logger = logger;
}
///
/// 获取角色列表
///
/// 分页查询参数,支持按名称和租户ID筛选
/// 分页的角色列表,包含角色基本信息和关联统计
/// 成功返回角色分页列表
/// 服务器内部错误
[HttpGet]
[Produces("application/json")]
[ProducesResponseType(typeof(PagedResultDto), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)]
public async Task>> GetRoles([FromQuery] RoleQueryDto query)
{
try
{
var (items, totalCount) = await _roleService.GetRolesAsync(query.Page, query.PageSize, query.Name, query.TenantId);
var result = new PagedResultDto
{
Items = items.ToList(),
TotalCount = totalCount,
Page = query.Page,
PageSize = query.PageSize
};
return Ok(result);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting roles");
return StatusCode(500, new { message = ex.Message });
}
}
///
/// 获取单个角色详情
///
/// 角色ID
/// 角色的详细信息,包括权限配置等
/// 成功返回角色详情
/// 角色不存在
/// 服务器内部错误
[HttpGet("{id}")]
[Produces("application/json")]
[ProducesResponseType(typeof(RoleDto), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)]
public async Task> GetRole(long id)
{
try
{
var role = await _roleService.GetRoleAsync(id);
if (role == null)
{
return NotFound();
}
return Ok(role);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting role {RoleId}", id);
return StatusCode(500, new { message = ex.Message });
}
}
///
/// 获取指定角色的用户列表
///
/// 角色ID
/// 属于该角色的所有用户列表
/// 成功返回用户列表
/// 角色不存在
/// 服务器内部错误
[HttpGet("{id}/users")]
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)]
public async Task>> GetRoleUsers(long id)
{
try
{
var users = await _roleService.GetRoleUsersAsync(id);
return Ok(users);
}
catch (KeyNotFoundException ex)
{
_logger.LogWarning(ex, "Role not found: {RoleId}", id);
return NotFound();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting users for role {RoleId}", id);
return StatusCode(500, new { message = ex.Message });
}
}
///
/// 创建新角色
///
/// 创建角色所需的配置信息
/// 创建的角色详情
/// 成功创建角色
/// 请求参数无效或角色名称已存在
/// 服务器内部错误
[HttpPost]
[Produces("application/json")]
[ProducesResponseType(typeof(RoleDto), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)]
public async Task> CreateRole([FromBody] CreateRoleDto dto)
{
try
{
var role = await _roleService.CreateRoleAsync(dto);
return CreatedAtAction(nameof(GetRole), new { id = role.Id }, role);
}
catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, "Validation error creating role");
return BadRequest(new { message = ex.Message });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error creating role");
return StatusCode(500, new { message = ex.Message });
}
}
///
/// 更新角色信息
///
/// 角色ID
/// 需要更新的角色配置信息
/// 无内容响应
/// 成功更新角色
/// 角色不存在
/// 请求参数无效
/// 服务器内部错误
[HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)]
public async Task UpdateRole(long id, [FromBody] UpdateRoleDto dto)
{
try
{
await _roleService.UpdateRoleAsync(id, dto);
return NoContent();
}
catch (KeyNotFoundException ex)
{
_logger.LogWarning(ex, "Role not found: {RoleId}", id);
return NotFound();
}
catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, "Validation error updating role {RoleId}", id);
return BadRequest(new { message = ex.Message });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating role {RoleId}", id);
return StatusCode(500, new { message = ex.Message });
}
}
///
/// 删除角色
///
/// 角色ID
/// 无内容响应
/// 成功删除角色
/// 角色不存在
/// 请求参数无效(如角色下有关联用户)
/// 服务器内部错误
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)]
public async Task DeleteRole(long id)
{
try
{
await _roleService.DeleteRoleAsync(id);
return NoContent();
}
catch (KeyNotFoundException ex)
{
_logger.LogWarning(ex, "Role not found: {RoleId}", id);
return NotFound();
}
catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, "Validation error deleting role {RoleId}", id);
return BadRequest(new { message = ex.Message });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting role {RoleId}", id);
return StatusCode(500, new { message = ex.Message });
}
}
///
/// 将用户添加到角色
///
/// 角色ID
/// 用户ID
/// 无内容响应
/// 成功添加用户到角色
/// 角色或用户不存在
/// 请求参数无效或用户已在角色中
/// 服务器内部错误
[HttpPost("{id}/users/{userId}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)]
public async Task AddUserToRole(long id, long userId)
{
try
{
await _roleService.AddUserToRoleAsync(id, userId);
return NoContent();
}
catch (KeyNotFoundException ex)
{
_logger.LogWarning(ex, "Role or user not found: RoleId={RoleId}, UserId={UserId}", id, userId);
return NotFound();
}
catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, "Validation error adding user {UserId} to role {RoleId}", userId, id);
return BadRequest(new { message = ex.Message });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error adding user {UserId} to role {RoleId}", userId, id);
return StatusCode(500, new { message = ex.Message });
}
}
///
/// 将用户从角色中移除
///
/// 角色ID
/// 用户ID
/// 无内容响应
/// 成功从角色中移除用户
/// 角色或用户不存在
/// 请求参数无效
/// 服务器内部错误
[HttpDelete("{id}/users/{userId}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)]
public async Task RemoveUserFromRole(long id, long userId)
{
try
{
await _roleService.RemoveUserFromRoleAsync(id, userId);
return NoContent();
}
catch (KeyNotFoundException ex)
{
_logger.LogWarning(ex, "Role or user not found: RoleId={RoleId}, UserId={UserId}", id, userId);
return NotFound();
}
catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, "Validation error removing user {UserId} from role {RoleId}", userId, id);
return BadRequest(new { message = ex.Message });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error removing user {UserId} from role {RoleId}", userId, id);
return StatusCode(500, new { message = ex.Message });
}
}
}