using LMS.Repository.Models.DB; using Microsoft.AspNetCore.Identity; namespace LMS.DAO.UserDAO { public class UserBasicDao(UserManager userManager) { private readonly UserManager _userManager = userManager; /// /// 检查用户是否存在,通过用户ID /// /// /// public async Task CheckUserExistsByID(long? userId) { if (userId == null) { return false; } return await _userManager.FindByIdAsync(userId.ToString()) != null; } /// /// 判断传入的数据是不是有管理员权限或者是超级管理员权限 /// /// /// /// public async Task 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; } /// /// 检查用户是不是超级管理员 /// /// /// /// public async Task 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; } /// /// 检查用户是不是管理员 /// /// /// /// public async Task 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; } /// /// 检查用户是不是代理 /// /// /// /// public async Task 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; } /// /// 判断用户是不是指定用户的上级 /// /// 用户ID /// 上级用户ID /// public async Task 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; } } }