LMS.service/LMS.DAO/UserDAO/UserBasicDAO.cs

150 lines
5.2 KiB
C#
Raw Normal View History

2024-10-13 17:04:47 +08:00
using LMS.Repository.Models.DB;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
2024-10-13 17:04:47 +08:00
namespace LMS.DAO.UserDAO
{
public class UserBasicDao(UserManager<User> userManager, ApplicationDbContext dbContext)
2024-10-13 17:04:47 +08:00
{
private readonly UserManager<User> _userManager = userManager;
private readonly ApplicationDbContext _dbContext = dbContext;
2024-10-13 17:04:47 +08:00
/// <summary>
/// 检查用户是否存在,通过用户ID
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public async Task<bool> CheckUserExistsByID(long? userId)
{
if (userId == null)
{
return false;
}
return await _userManager.FindByIdAsync(userId.ToString()) != null;
}
/// <summary>
/// 判断传入的数据是不是有管理员权限或者是超级管理员权限
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<bool> CheckUserIsAdminOrSuperAdmin(long? userId)
{
if (userId == null)
{
return false;
}
User? user = await _userManager.FindByIdAsync(userId.ToString() ?? "0") ?? throw new Exception("用户不存在");
bool isAdminOrSuperAdmin = await _userManager.IsInRoleAsync(user, "Admin") || await _userManager.IsInRoleAsync(user, "Super Admin");
return isAdminOrSuperAdmin || userId == 4;
}
/// <summary>
/// 检查用户是不是超级管理员
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<bool> CheckUserIsSuperAdmin(long? userId)
{
if (userId == null)
{
return false;
}
User? user = await _userManager.FindByIdAsync(userId.ToString() ?? "0") ?? throw new Exception("用户不存在");
bool isSuperAdmin = await _userManager.IsInRoleAsync(user, "Super Admin");
return isSuperAdmin || userId == 4;
}
/// <summary>
/// 检查用户是不是管理员
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<bool> CheckUserIsAdmin(long? userId)
{
if (userId == null)
{
return false;
}
User? user = await _userManager.FindByIdAsync(userId.ToString() ?? "0") ?? throw new Exception("用户不存在");
bool isSuperAdmin = await _userManager.IsInRoleAsync(user, "Admin");
return isSuperAdmin || userId == 4;
}
/// <summary>
/// 检查用户是不是代理
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<bool> CheckUserIsAgent(long? userId)
{
if (userId == null)
{
return false;
}
User? user = await _userManager.FindByIdAsync(userId.ToString() ?? "0") ?? throw new Exception("用户不存在");
bool isSuperAdmin = await _userManager.IsInRoleAsync(user, "Agent User");
return isSuperAdmin || userId == 4;
}
/// <summary>
/// 判断用户是不是指定用户的上级
/// </summary>
/// <param name="userId">用户ID</param>
/// <param name="agentUserId">上级用户ID</param>
/// <returns></returns>
public async Task<bool> CheckAgentAndUserMatch(long? userId, long? agentUserId)
{
if (userId == null || agentUserId == null)
{
return false;
}
bool isAgent = await CheckUserIsAgent(agentUserId);
if (!isAgent)
{
return false;
}
User? user = await _userManager.FindByIdAsync(userId.ToString() ?? "0") ?? throw new Exception("用户不存在");
if (user == null)
{
return false;
}
if (user.ParentId != agentUserId)
{
return false;
}
return true;
}
/// <summary>
/// 获取用户的所有角色ID
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<List<long>> GetUserRoleIds(long userId)
{
// 查找用户,若不存在直接抛异常
User user = await _userManager.FindByIdAsync(userId.ToString())
?? throw new Exception("用户不存在");
// 直接查询 UserRoles 表获取所有关联的 RoleId
var roleIds = await _dbContext.UserRoles
.Where(ur => ur.UserId == user.Id)
.Select(ur => ur.RoleId)
.ToListAsync();
return roleIds;
}
2024-10-13 17:04:47 +08:00
}
}