2025-07-11 16:17:31 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2025-07-11 22:59:29 +08:00
|
|
|
|
using AGSS.Models;
|
2025-07-09 23:13:11 +08:00
|
|
|
|
using AGSS.Models.DTOs;
|
2025-07-09 13:57:43 +08:00
|
|
|
|
using AGSS.Models.Entities;
|
|
|
|
|
using AGSS.Models.Template;
|
2025-07-11 22:59:29 +08:00
|
|
|
|
using AGSS.Services;
|
2025-07-09 13:57:43 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2025-07-11 16:17:31 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2025-07-09 13:57:43 +08:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2025-07-11 22:59:29 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-07-09 13:57:43 +08:00
|
|
|
|
|
|
|
|
|
namespace AGSS.Controllers.Admin;
|
|
|
|
|
|
2025-07-09 16:08:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 控制器类,用于管理角色相关的操作,包括添加角色、分配角色给用户以及通过角色查询用户。
|
|
|
|
|
/// 该控制器仅限具有"Admin"角色的用户访问。
|
|
|
|
|
/// </summary>
|
2025-07-11 16:17:31 +08:00
|
|
|
|
[Authorize]
|
2025-07-09 15:40:47 +08:00
|
|
|
|
[Route("api/v1/[controller]/[action]")]
|
2025-07-09 13:57:43 +08:00
|
|
|
|
public class AdminRoleControllers:ControllerBase
|
|
|
|
|
{
|
2025-07-11 22:59:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 用户服务实例,用于执行与用户相关的操作。
|
|
|
|
|
/// 该服务提供了一系列方法来处理用户的查询和更新等操作,
|
|
|
|
|
/// 包括但不限于获取用户详细信息、修改用户资料等功能。
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly UserService _userService;
|
|
|
|
|
|
|
|
|
|
public AdminRoleControllers(UserService userService, RoleManager<RoleModel> roleManager, UserManager<UserModel> userManager)
|
|
|
|
|
{
|
|
|
|
|
_userService = userService;
|
|
|
|
|
_roleManager = roleManager;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 16:08:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 角色管理器,用于处理角色相关的操作,如创建、查询等。
|
|
|
|
|
/// 此角色管理器实例主要用于与RoleModel类型的实体进行交互,
|
|
|
|
|
/// 支持添加新角色、为用户分配角色等功能。
|
|
|
|
|
/// </summary>
|
2025-07-09 13:57:43 +08:00
|
|
|
|
private readonly RoleManager<RoleModel> _roleManager;
|
2025-07-09 16:08:20 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 用户管理器实例,用于处理用户相关的操作如添加角色、查询用户等。
|
|
|
|
|
/// 此实例通过依赖注入的方式在构造函数中初始化,并在整个控制器生命周期内可用。
|
|
|
|
|
/// </summary>
|
2025-07-09 13:57:43 +08:00
|
|
|
|
private readonly UserManager<UserModel> _userManager; // Assuming UserModel is the type of user
|
|
|
|
|
|
2025-07-09 16:08:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 管理员角色控制器,用于处理与角色相关的操作,如添加角色、分配角色给用户以及通过角色查询用户。
|
|
|
|
|
/// 该控制器下的所有方法都需要管理员权限才能访问。
|
|
|
|
|
/// </summary>
|
2025-07-11 22:59:29 +08:00
|
|
|
|
|
2025-07-09 13:57:43 +08:00
|
|
|
|
|
2025-07-09 16:08:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 添加新角色
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="role">要添加的角色信息</param>
|
|
|
|
|
/// <returns>返回操作结果,包含状态码、消息和数据</returns>
|
2025-07-09 13:57:43 +08:00
|
|
|
|
[HttpPost]
|
2025-07-11 22:59:29 +08:00
|
|
|
|
public async Task<IActionResult> AddRole(string rolename,string normalizedname)
|
2025-07-09 13:57:43 +08:00
|
|
|
|
{
|
2025-07-11 16:17:31 +08:00
|
|
|
|
|
|
|
|
|
|
2025-07-09 13:57:43 +08:00
|
|
|
|
|
2025-07-11 22:59:29 +08:00
|
|
|
|
var result = await _roleManager.CreateAsync(new RoleModel(){Id = Guid.Empty.ToString(),Name = rolename,NormalizedName = normalizedname});
|
2025-07-09 13:57:43 +08:00
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
{
|
2025-07-11 16:17:31 +08:00
|
|
|
|
return Ok(new ReturnTemplate(200,"创建成功",""));
|
2025-07-09 13:57:43 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Ok(new ReturnTemplate(StatusCodes.Status500InternalServerError,"创建失败","Failed to create role: " + string.Join(", ", result.Errors.Select(e => e.Description))));
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-09 16:08:20 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 为指定用户分配角色
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId">用户的唯一标识符</param>
|
|
|
|
|
/// <param name="roleName">要分配的角色名称</param>
|
|
|
|
|
/// <returns>返回一个包含操作结果的ReturnTemplate对象,其中Code表示状态码,Msg表示消息,Data表示附加数据(如果有的话)</returns>
|
2025-07-09 13:57:43 +08:00
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> EndowRole(string userId, string roleName)
|
|
|
|
|
{
|
|
|
|
|
var user = await _userManager.FindByIdAsync(userId);
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
return Ok(new ReturnTemplate(400, "用户不存在", ""));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var role = await _roleManager.FindByNameAsync(roleName);
|
|
|
|
|
if (role == null)
|
|
|
|
|
{
|
|
|
|
|
return Ok(new ReturnTemplate(400, "角色不存在", ""));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await _userManager.AddToRoleAsync(user, role.Name);
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
{
|
2025-07-10 23:15:29 +08:00
|
|
|
|
return Ok(new ReturnTemplate(200, "现在该用户已经被赋予这个角色了", user));
|
2025-07-09 13:57:43 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Ok(new ReturnTemplate(StatusCodes.Status500InternalServerError, "角色分配失败", "Failed to endow role: " + string.Join(", ", result.Errors.Select(e => e.Description))));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-07-09 23:13:11 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除指定用户。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId">要删除的用户的唯一标识符。</param>
|
|
|
|
|
/// <returns>返回操作结果,包含状态码、消息和数据。如果删除成功,则返回200状态码;如果用户ID为空或未找到指定用户,则分别返回400或404状态码;若删除过程中出现错误,则返回500状态码并附带错误信息。</returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> DelUser(string userId)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(userId))
|
|
|
|
|
{
|
2025-07-10 23:15:29 +08:00
|
|
|
|
return Ok(new ReturnTemplate(400, "你打算拿个空的Id来骗我吗?", null));
|
2025-07-09 23:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var user = await _userManager.FindByIdAsync(userId);
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
2025-07-10 23:15:29 +08:00
|
|
|
|
return Ok(new ReturnTemplate(404, "你输了个假的用户吧......", null));
|
2025-07-09 23:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除用户
|
|
|
|
|
var result = await _userManager.DeleteAsync(user);
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
return Ok(new ReturnTemplate(200, "用户删除成功,不要留念这个用户哦~", null));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return StatusCode(500, new ReturnTemplate(500, "发生了一些不可预料的错误,555", result.Errors));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> SetMenu([FromBody]MenuRequest request)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(request.Id) || string.IsNullOrWhiteSpace(request.MenuName))
|
|
|
|
|
{
|
|
|
|
|
return Ok(new ReturnTemplate(400, "请求参数无效(有的参数是空的哦~)", ""));
|
|
|
|
|
}
|
|
|
|
|
var user=await _userManager.FindByIdAsync(request.Id);
|
|
|
|
|
if (user==null)
|
|
|
|
|
{
|
|
|
|
|
return Ok(new ReturnTemplate(404, "Sorry,你输入的用户我们找不到!", ""));
|
|
|
|
|
}
|
|
|
|
|
user.MenuCode = request.MenuCode;
|
|
|
|
|
user.MenuName = request.MenuName;
|
|
|
|
|
var result= await _userManager.UpdateAsync(user);
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
return Ok(new ReturnTemplate(200, "配置成功啦!", ""));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-07-10 23:15:29 +08:00
|
|
|
|
return StatusCode(500, new ReturnTemplate(500, "删除用户时发生错误,原因请看ErrorResult", result.Errors));
|
2025-07-09 23:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-09 16:08:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过角色查询用户,支持分页
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">包含角色名称、页码和每页大小的请求对象</param>
|
|
|
|
|
/// <returns>返回包含总用户数和当前页用户的响应对象</returns>
|
2025-07-09 13:57:43 +08:00
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> SearchUserFromRole([FromBody] SearchUserFromRoleRequest request)
|
|
|
|
|
{
|
2025-07-11 22:59:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IList<UserProfile> usersInRole = null;
|
2025-07-09 13:57:43 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(request.RoleName))
|
|
|
|
|
{
|
2025-07-11 22:59:29 +08:00
|
|
|
|
usersInRole = _userManager.Users.ToList();
|
2025-07-09 13:57:43 +08:00
|
|
|
|
}
|
2025-07-11 22:59:29 +08:00
|
|
|
|
else
|
2025-07-09 13:57:43 +08:00
|
|
|
|
{
|
2025-07-11 22:59:29 +08:00
|
|
|
|
usersInRole = await _userService.GetUsersProfileInRoleAsync(request.RoleName);
|
|
|
|
|
|
2025-07-09 13:57:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var totalUsers = usersInRole.Count;
|
|
|
|
|
|
|
|
|
|
var pagedUsers = usersInRole
|
|
|
|
|
.Skip((request.Page - 1) * request.PageSize)
|
|
|
|
|
.Take(request.PageSize)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var response = new SearchUserFromRoleResponse
|
|
|
|
|
{
|
|
|
|
|
TotalCount = totalUsers,
|
|
|
|
|
Users = pagedUsers
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Ok(new ReturnTemplate(200, "查询成功", response));
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 16:08:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 用于通过角色名称查询用户列表的请求模型。支持分页功能。
|
|
|
|
|
/// </summary>
|
2025-07-09 13:57:43 +08:00
|
|
|
|
public class SearchUserFromRoleRequest
|
|
|
|
|
{
|
2025-07-11 22:59:29 +08:00
|
|
|
|
|
|
|
|
|
|
2025-07-09 16:08:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表示角色的名称。此属性用于指定或获取与用户管理相关的角色名称。
|
|
|
|
|
/// 在进行角色分配、查询等操作时,需要提供正确的角色名称以确保操作的成功执行。
|
|
|
|
|
/// </summary>
|
2025-07-09 13:57:43 +08:00
|
|
|
|
public string RoleName { get; set; }
|
2025-07-09 16:08:20 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表示当前请求的页码,默认为1。用于分页查询用户时指定从哪一页开始获取数据。
|
|
|
|
|
/// </summary>
|
2025-07-09 13:57:43 +08:00
|
|
|
|
public int Page { get; set; } = 1;
|
2025-07-09 16:08:20 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 每页显示的用户数量。默认值为10。
|
|
|
|
|
/// 该属性用于分页查询中指定每一页应包含的用户条目数。
|
|
|
|
|
/// </summary>
|
2025-07-09 13:57:43 +08:00
|
|
|
|
public int PageSize { get; set; } = 10;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 16:08:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表示通过角色查询用户后返回的响应数据。
|
|
|
|
|
/// 该类用于封装查询结果,包括总用户数和分页后的用户列表。
|
|
|
|
|
/// </summary>
|
2025-07-09 13:57:43 +08:00
|
|
|
|
public class SearchUserFromRoleResponse
|
|
|
|
|
{
|
2025-07-09 16:08:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表示属于特定角色的用户总数。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>此属性用于分页查询中,返回匹配给定角色名称的所有用户的数量。</remarks>
|
2025-07-09 13:57:43 +08:00
|
|
|
|
public int TotalCount { get; set; }
|
2025-07-09 16:08:20 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 表示属于特定角色的用户列表。该属性用于存储和返回在给定角色下的所有用户。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>此列表通常作为查询结果的一部分,例如通过角色名搜索用户时返回的数据。</remarks>
|
2025-07-11 22:59:29 +08:00
|
|
|
|
public List<UserProfile> Users { get; set; }
|
2025-07-09 13:57:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|