2024-10-13 17:04:47 +08:00
|
|
|
|
using LMS.Repository.Models.DB;
|
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2024-11-13 14:00:39 +08:00
|
|
|
|
using OneOf.Types;
|
2024-10-13 17:04:47 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace LMS.DAO.UserDAO
|
|
|
|
|
|
{
|
|
|
|
|
|
public class UserBasicDao(UserManager<User> userManager)
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly UserManager<User> _userManager = userManager;
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
}
|
2024-11-13 14:00:39 +08:00
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
}
|
2024-10-13 17:04:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|