develop #5
@ -1,13 +1,16 @@
 | 
				
			|||||||
using System.Collections.Generic;
 | 
					using System.Collections.Generic;
 | 
				
			||||||
using System.Linq;
 | 
					using System.Linq;
 | 
				
			||||||
using System.Threading.Tasks;
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					using AGSS.Models;
 | 
				
			||||||
using AGSS.Models.DTOs;
 | 
					using AGSS.Models.DTOs;
 | 
				
			||||||
using AGSS.Models.Entities;
 | 
					using AGSS.Models.Entities;
 | 
				
			||||||
using AGSS.Models.Template;
 | 
					using AGSS.Models.Template;
 | 
				
			||||||
 | 
					using AGSS.Services;
 | 
				
			||||||
using Microsoft.AspNetCore.Authorization;
 | 
					using Microsoft.AspNetCore.Authorization;
 | 
				
			||||||
using Microsoft.AspNetCore.Http;
 | 
					using Microsoft.AspNetCore.Http;
 | 
				
			||||||
using Microsoft.AspNetCore.Identity;
 | 
					using Microsoft.AspNetCore.Identity;
 | 
				
			||||||
using Microsoft.AspNetCore.Mvc;
 | 
					using Microsoft.AspNetCore.Mvc;
 | 
				
			||||||
 | 
					using Microsoft.EntityFrameworkCore;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace AGSS.Controllers.Admin;
 | 
					namespace AGSS.Controllers.Admin;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -19,6 +22,20 @@ namespace AGSS.Controllers.Admin;
 | 
				
			|||||||
[Route("api/v1/[controller]/[action]")]
 | 
					[Route("api/v1/[controller]/[action]")]
 | 
				
			||||||
public class AdminRoleControllers:ControllerBase
 | 
					public class AdminRoleControllers:ControllerBase
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    /// <summary>
 | 
				
			||||||
 | 
					    /// 用户服务实例,用于执行与用户相关的操作。
 | 
				
			||||||
 | 
					    /// 该服务提供了一系列方法来处理用户的查询和更新等操作,
 | 
				
			||||||
 | 
					    /// 包括但不限于获取用户详细信息、修改用户资料等功能。
 | 
				
			||||||
 | 
					    /// </summary>
 | 
				
			||||||
 | 
					    private readonly UserService _userService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public AdminRoleControllers(UserService userService, RoleManager<RoleModel> roleManager, UserManager<UserModel> userManager)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        _userService = userService;
 | 
				
			||||||
 | 
					        _roleManager = roleManager;
 | 
				
			||||||
 | 
					        _userManager = userManager;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// 角色管理器,用于处理角色相关的操作,如创建、查询等。
 | 
					    /// 角色管理器,用于处理角色相关的操作,如创建、查询等。
 | 
				
			||||||
    /// 此角色管理器实例主要用于与RoleModel类型的实体进行交互,
 | 
					    /// 此角色管理器实例主要用于与RoleModel类型的实体进行交互,
 | 
				
			||||||
@ -36,11 +53,7 @@ public class AdminRoleControllers:ControllerBase
 | 
				
			|||||||
    /// 管理员角色控制器,用于处理与角色相关的操作,如添加角色、分配角色给用户以及通过角色查询用户。
 | 
					    /// 管理员角色控制器,用于处理与角色相关的操作,如添加角色、分配角色给用户以及通过角色查询用户。
 | 
				
			||||||
    /// 该控制器下的所有方法都需要管理员权限才能访问。
 | 
					    /// 该控制器下的所有方法都需要管理员权限才能访问。
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    public AdminRoleControllers(RoleManager<RoleModel> roleManager, UserManager<UserModel> userManager)
 | 
					
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        _roleManager = roleManager;
 | 
					 | 
				
			||||||
        _userManager = userManager;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// 添加新角色
 | 
					    /// 添加新角色
 | 
				
			||||||
@ -48,12 +61,12 @@ public class AdminRoleControllers:ControllerBase
 | 
				
			|||||||
    /// <param name="role">要添加的角色信息</param>
 | 
					    /// <param name="role">要添加的角色信息</param>
 | 
				
			||||||
    /// <returns>返回操作结果,包含状态码、消息和数据</returns>
 | 
					    /// <returns>返回操作结果,包含状态码、消息和数据</returns>
 | 
				
			||||||
    [HttpPost]
 | 
					    [HttpPost]
 | 
				
			||||||
    public async Task<IActionResult> AddRole(string rolename)
 | 
					    public async Task<IActionResult> 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)
 | 
					        if (result.Succeeded)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            return Ok(new ReturnTemplate(200,"创建成功",""));
 | 
					            return Ok(new ReturnTemplate(200,"创建成功",""));
 | 
				
			||||||
@ -169,18 +182,21 @@ public class AdminRoleControllers:ControllerBase
 | 
				
			|||||||
    [HttpPost]
 | 
					    [HttpPost]
 | 
				
			||||||
    public async Task<IActionResult> SearchUserFromRole([FromBody] SearchUserFromRoleRequest request)
 | 
					    public async Task<IActionResult> SearchUserFromRole([FromBody] SearchUserFromRoleRequest request)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					       
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        IList<UserProfile> usersInRole = null;
 | 
				
			||||||
        if (string.IsNullOrWhiteSpace(request.RoleName))
 | 
					        if (string.IsNullOrWhiteSpace(request.RoleName))
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            return Ok(new ReturnTemplate(400, "角色名称不能为空,就像凌云心里不能没有我一样", null));
 | 
					            usersInRole = _userManager.Users.ToList();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
        var role = await _roleManager.FindByNameAsync(request.RoleName);
 | 
					 | 
				
			||||||
        if (role == null)
 | 
					 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            return Ok(new ReturnTemplate(400, "你输入的角色不存在哦!", null));
 | 
					            usersInRole = await _userService.GetUsersProfileInRoleAsync(request.RoleName);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var usersInRole = await _userManager.GetUsersInRoleAsync(role.Name);
 | 
					 | 
				
			||||||
        var totalUsers = usersInRole.Count;
 | 
					        var totalUsers = usersInRole.Count;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var pagedUsers = usersInRole
 | 
					        var pagedUsers = usersInRole
 | 
				
			||||||
@ -202,6 +218,8 @@ public class AdminRoleControllers:ControllerBase
 | 
				
			|||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    public class SearchUserFromRoleRequest
 | 
					    public class SearchUserFromRoleRequest
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					     
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// 表示角色的名称。此属性用于指定或获取与用户管理相关的角色名称。
 | 
					        /// 表示角色的名称。此属性用于指定或获取与用户管理相关的角色名称。
 | 
				
			||||||
        /// 在进行角色分配、查询等操作时,需要提供正确的角色名称以确保操作的成功执行。
 | 
					        /// 在进行角色分配、查询等操作时,需要提供正确的角色名称以确保操作的成功执行。
 | 
				
			||||||
@ -236,6 +254,6 @@ public class AdminRoleControllers:ControllerBase
 | 
				
			|||||||
        /// 表示属于特定角色的用户列表。该属性用于存储和返回在给定角色下的所有用户。
 | 
					        /// 表示属于特定角色的用户列表。该属性用于存储和返回在给定角色下的所有用户。
 | 
				
			||||||
        /// </summary>
 | 
					        /// </summary>
 | 
				
			||||||
        /// <remarks>此列表通常作为查询结果的一部分,例如通过角色名搜索用户时返回的数据。</remarks>
 | 
					        /// <remarks>此列表通常作为查询结果的一部分,例如通过角色名搜索用户时返回的数据。</remarks>
 | 
				
			||||||
        public List<UserModel> Users { get; set; }
 | 
					        public List<UserProfile> Users { get; set; }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -2,6 +2,7 @@ using System.IdentityModel.Tokens.Jwt;
 | 
				
			|||||||
using System.Security.Claims;
 | 
					using System.Security.Claims;
 | 
				
			||||||
using AGSS.Models.Entities;
 | 
					using AGSS.Models.Entities;
 | 
				
			||||||
using AGSS.Models.Template;
 | 
					using AGSS.Models.Template;
 | 
				
			||||||
 | 
					using AGSS.Services;
 | 
				
			||||||
using Microsoft.AspNetCore.Authorization;
 | 
					using Microsoft.AspNetCore.Authorization;
 | 
				
			||||||
using Microsoft.AspNetCore.Identity;
 | 
					using Microsoft.AspNetCore.Identity;
 | 
				
			||||||
using Microsoft.AspNetCore.Mvc;
 | 
					using Microsoft.AspNetCore.Mvc;
 | 
				
			||||||
 | 
				
			|||||||
@ -4,6 +4,7 @@ using AGSS.DbSet;
 | 
				
			|||||||
using AGSS.Models;
 | 
					using AGSS.Models;
 | 
				
			||||||
using AGSS.Models.Entities;
 | 
					using AGSS.Models.Entities;
 | 
				
			||||||
using AGSS.Models.Template;
 | 
					using AGSS.Models.Template;
 | 
				
			||||||
 | 
					using AGSS.Services;
 | 
				
			||||||
using AGSS.Utilities;
 | 
					using AGSS.Utilities;
 | 
				
			||||||
using asg_form;
 | 
					using asg_form;
 | 
				
			||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
 | 
					using Microsoft.AspNetCore.Authentication.JwtBearer;
 | 
				
			||||||
@ -53,7 +54,7 @@ builder.Services.AddIdentityCore<UserModel>(options =>
 | 
				
			|||||||
  //  builder.Services.AddScoped<UserService>();
 | 
					  //  builder.Services.AddScoped<UserService>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
builder.Services.AddScoped<Jwt>();
 | 
					builder.Services.AddScoped<Jwt>();
 | 
				
			||||||
 | 
					builder.Services.AddScoped<UserService>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -75,7 +76,7 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
 | 
				
			|||||||
            IssuerSigningKey = secKey
 | 
					            IssuerSigningKey = secKey
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
    .AddCookie("Identity.External");
 | 
					    .AddCookie("Identity.External").AddCookie("Identity.Application");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,8 @@ using AGSS.Models;
 | 
				
			|||||||
using AGSS.Models.Entities;
 | 
					using AGSS.Models.Entities;
 | 
				
			||||||
using Microsoft.AspNetCore.Identity;
 | 
					using Microsoft.AspNetCore.Identity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace AGSS.Services;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class UserService
 | 
					public class UserService
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    private readonly UserManager<UserModel> _userManager;
 | 
					    private readonly UserManager<UserModel> _userManager;
 | 
				
			||||||
@ -34,4 +36,37 @@ public class UserService
 | 
				
			|||||||
            MenuName = user.MenuName
 | 
					            MenuName = user.MenuName
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    public async Task<List<UserProfile>> 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<UserProfile>();
 | 
				
			||||||
 | 
					        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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										27
									
								
								AGSS/Utilities/LINQ.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								AGSS/Utilities/LINQ.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Linq;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace AGSS.Utilities;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public static class LINQ
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    /// <summary>
 | 
				
			||||||
 | 
					    /// 从给定的序列中分页获取元素。
 | 
				
			||||||
 | 
					    /// </summary>
 | 
				
			||||||
 | 
					    /// <typeparam name="T">序列中的元素类型。</typeparam>
 | 
				
			||||||
 | 
					    /// <param name="source">要从中分页的源序列。</param>
 | 
				
			||||||
 | 
					    /// <param name="pageIndex">请求的页码,从0开始。</param>
 | 
				
			||||||
 | 
					    /// <param name="pageSize">每页包含的元素数量。</param>
 | 
				
			||||||
 | 
					    /// <returns>返回指定页码和页大小对应的子序列。</returns>
 | 
				
			||||||
 | 
					    /// <exception cref="ArgumentNullException">如果source为null。</exception>
 | 
				
			||||||
 | 
					    /// <exception cref="ArgumentOutOfRangeException">如果pageIndex是负数或者pageSize是非正数。</exception>
 | 
				
			||||||
 | 
					    public static IEnumerable<T> Paginate<T>(this IEnumerable<T> 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);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user