diff --git a/AGSS/Controllers/Admin/AdminRoleControllers.cs b/AGSS/Controllers/Admin/AdminRoleControllers.cs
index d235cbe..e33728f 100644
--- a/AGSS/Controllers/Admin/AdminRoleControllers.cs
+++ b/AGSS/Controllers/Admin/AdminRoleControllers.cs
@@ -1,13 +1,16 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
+using AGSS.Models;
using AGSS.Models.DTOs;
using AGSS.Models.Entities;
using AGSS.Models.Template;
+using AGSS.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
+using Microsoft.EntityFrameworkCore;
namespace AGSS.Controllers.Admin;
@@ -19,6 +22,20 @@ namespace AGSS.Controllers.Admin;
[Route("api/v1/[controller]/[action]")]
public class AdminRoleControllers:ControllerBase
{
+ ///
+ /// 用户服务实例,用于执行与用户相关的操作。
+ /// 该服务提供了一系列方法来处理用户的查询和更新等操作,
+ /// 包括但不限于获取用户详细信息、修改用户资料等功能。
+ ///
+ private readonly UserService _userService;
+
+ public AdminRoleControllers(UserService userService, RoleManager roleManager, UserManager userManager)
+ {
+ _userService = userService;
+ _roleManager = roleManager;
+ _userManager = userManager;
+ }
+
///
/// 角色管理器,用于处理角色相关的操作,如创建、查询等。
/// 此角色管理器实例主要用于与RoleModel类型的实体进行交互,
@@ -36,11 +53,7 @@ public class AdminRoleControllers:ControllerBase
/// 管理员角色控制器,用于处理与角色相关的操作,如添加角色、分配角色给用户以及通过角色查询用户。
/// 该控制器下的所有方法都需要管理员权限才能访问。
///
- public AdminRoleControllers(RoleManager roleManager, UserManager userManager)
- {
- _roleManager = roleManager;
- _userManager = userManager;
- }
+
///
/// 添加新角色
@@ -48,12 +61,12 @@ public class AdminRoleControllers:ControllerBase
/// 要添加的角色信息
/// 返回操作结果,包含状态码、消息和数据
[HttpPost]
- public async Task AddRole(string rolename)
+ public async Task AddRole(string rolename,string normalizedname)
{
- var result = await _roleManager.CreateAsync(new RoleModel(){Id = new Guid().ToString(),Name = rolename,NormalizedName = rolename});
+ var result = await _roleManager.CreateAsync(new RoleModel(){Id = Guid.Empty.ToString(),Name = rolename,NormalizedName = normalizedname});
if (result.Succeeded)
{
return Ok(new ReturnTemplate(200,"创建成功",""));
@@ -169,18 +182,21 @@ public class AdminRoleControllers:ControllerBase
[HttpPost]
public async Task SearchUserFromRole([FromBody] SearchUserFromRoleRequest request)
{
+
+
+
+
+ IList usersInRole = null;
if (string.IsNullOrWhiteSpace(request.RoleName))
{
- return Ok(new ReturnTemplate(400, "角色名称不能为空,就像凌云心里不能没有我一样", null));
+ usersInRole = _userManager.Users.ToList();
}
-
- var role = await _roleManager.FindByNameAsync(request.RoleName);
- if (role == null)
+ else
{
- return Ok(new ReturnTemplate(400, "你输入的角色不存在哦!", null));
+ usersInRole = await _userService.GetUsersProfileInRoleAsync(request.RoleName);
+
}
- var usersInRole = await _userManager.GetUsersInRoleAsync(role.Name);
var totalUsers = usersInRole.Count;
var pagedUsers = usersInRole
@@ -202,6 +218,8 @@ public class AdminRoleControllers:ControllerBase
///
public class SearchUserFromRoleRequest
{
+
+
///
/// 表示角色的名称。此属性用于指定或获取与用户管理相关的角色名称。
/// 在进行角色分配、查询等操作时,需要提供正确的角色名称以确保操作的成功执行。
@@ -236,6 +254,6 @@ public class AdminRoleControllers:ControllerBase
/// 表示属于特定角色的用户列表。该属性用于存储和返回在给定角色下的所有用户。
///
/// 此列表通常作为查询结果的一部分,例如通过角色名搜索用户时返回的数据。
- public List Users { get; set; }
+ public List Users { get; set; }
}
}
\ No newline at end of file
diff --git a/AGSS/Controllers/User/UserControllers.cs b/AGSS/Controllers/User/UserControllers.cs
index 8e16b8e..41c9f14 100644
--- a/AGSS/Controllers/User/UserControllers.cs
+++ b/AGSS/Controllers/User/UserControllers.cs
@@ -2,6 +2,7 @@ using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using AGSS.Models.Entities;
using AGSS.Models.Template;
+using AGSS.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
@@ -29,7 +30,7 @@ public class UserControllers:ControllerBase
public UserControllers(UserService userService, UserManager userManager)
{
_userService = userService;
- }
+ }
///
diff --git a/AGSS/Program.cs b/AGSS/Program.cs
index c970228..3e306db 100644
--- a/AGSS/Program.cs
+++ b/AGSS/Program.cs
@@ -4,6 +4,7 @@ using AGSS.DbSet;
using AGSS.Models;
using AGSS.Models.Entities;
using AGSS.Models.Template;
+using AGSS.Services;
using AGSS.Utilities;
using asg_form;
using Microsoft.AspNetCore.Authentication.JwtBearer;
@@ -53,7 +54,7 @@ builder.Services.AddIdentityCore(options =>
// builder.Services.AddScoped();
builder.Services.AddScoped();
-
+builder.Services.AddScoped();
@@ -75,7 +76,7 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
IssuerSigningKey = secKey
};
})
- .AddCookie("Identity.External");
+ .AddCookie("Identity.External").AddCookie("Identity.Application");
diff --git a/AGSS/Services/UserService.cs b/AGSS/Services/UserService.cs
index 343c590..29ec325 100644
--- a/AGSS/Services/UserService.cs
+++ b/AGSS/Services/UserService.cs
@@ -2,6 +2,8 @@ using AGSS.Models;
using AGSS.Models.Entities;
using Microsoft.AspNetCore.Identity;
+namespace AGSS.Services;
+
public class UserService
{
private readonly UserManager _userManager;
@@ -34,4 +36,37 @@ public class UserService
MenuName = user.MenuName
};
}
-}
+
+
+ public async Task> GetUsersProfileInRoleAsync(string roleName)
+ {
+ var usersInRole = await _userManager.GetUsersInRoleAsync(roleName);
+ if (usersInRole == null || !usersInRole.Any())
+ {
+ throw new ArgumentException("No users found in the specified role");
+ }
+
+ var userProfiles = new List();
+ foreach (var user in usersInRole)
+ {
+ userProfiles.Add(new UserProfile
+ {
+ Id = user.Id,
+ UserName = user.UserName,
+ Email = user.Email,
+ Sex = user.Sex,
+ Description = user.Description,
+ Config = user.Config,
+ JobCode = user.JobCode,
+ JobName = user.JobName,
+ Birthday = user.Birthday,
+ MenuCode = user.MenuCode,
+ MenuName = user.MenuName
+ });
+ }
+
+ // Assuming you want to return a single UserProfile, you might need to adjust this logic
+ // For now, returning the first user's profile
+ return userProfiles;
+ }
+}
\ No newline at end of file
diff --git a/AGSS/Utilities/LINQ.cs b/AGSS/Utilities/LINQ.cs
new file mode 100644
index 0000000..467e000
--- /dev/null
+++ b/AGSS/Utilities/LINQ.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace AGSS.Utilities;
+
+public static class LINQ
+{
+ ///
+ /// 从给定的序列中分页获取元素。
+ ///
+ /// 序列中的元素类型。
+ /// 要从中分页的源序列。
+ /// 请求的页码,从0开始。
+ /// 每页包含的元素数量。
+ /// 返回指定页码和页大小对应的子序列。
+ /// 如果source为null。
+ /// 如果pageIndex是负数或者pageSize是非正数。
+ public static IEnumerable Paginate(this IEnumerable source, int pageIndex, int pageSize)
+ {
+ if (source == null) throw new ArgumentNullException(nameof(source));
+ if (pageIndex < 0) throw new ArgumentOutOfRangeException(nameof(pageIndex), "Page index must be non-negative.");
+ if (pageSize <= 0) throw new ArgumentOutOfRangeException(nameof(pageSize), "Page size must be positive.");
+
+ return source.Skip(pageIndex * pageSize).Take(pageSize);
+ }
+}
\ No newline at end of file