|
@@ -0,0 +1,358 @@
|
|
|
+using Electric.Core;
|
|
|
+using Electric.Domain.Entities.Identity;
|
|
|
+using Electric.Domain.Repositories.Identity;
|
|
|
+
|
|
|
+using Microsoft.AspNetCore.Identity;
|
|
|
+
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace Electric.Domain.Manager.Identity
|
|
|
+{
|
|
|
+ public class UserStore : IUserStore<EleUser>, IUserPasswordStore<EleUser>, IUserRoleStore<EleUser>
|
|
|
+ {
|
|
|
+ private IUserRepository _userRepository;
|
|
|
+ private IRoleRepository _roleRepository;
|
|
|
+
|
|
|
+ public bool AutoSaveChanges { get; set; } = true;
|
|
|
+
|
|
|
+ public UserStore(IUserRepository userRepository, IRoleRepository roleRepository)
|
|
|
+ {
|
|
|
+ _userRepository = userRepository;
|
|
|
+ _roleRepository = roleRepository;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 新增用户
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<IdentityResult> CreateAsync(EleUser user, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+
|
|
|
+ await _userRepository.InsertAsync(user, AutoSaveChanges, cancellationToken);
|
|
|
+
|
|
|
+ return IdentityResult.Success;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 删除用户
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<IdentityResult> DeleteAsync(EleUser user, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+
|
|
|
+ await _userRepository.DeleteAsync(user, AutoSaveChanges, cancellationToken);
|
|
|
+
|
|
|
+ return IdentityResult.Success;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Dispose()
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据用户Id获取用户
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="userId"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+#pragma warning disable CS8613 // 返回类型中引用类型的为 Null 性与隐式实现的成员不匹配。
|
|
|
+
|
|
|
+ public async Task<EleUser> FindByIdAsync(string userId, CancellationToken cancellationToken)
|
|
|
+#pragma warning restore CS8613 // 返回类型中引用类型的为 Null 性与隐式实现的成员不匹配。
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(userId, nameof(userId));
|
|
|
+
|
|
|
+ return await _userRepository.FindAsync(Guid.Parse(userId), AutoSaveChanges, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据用户名获取用户
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="normalizedUserName"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+#pragma warning disable CS8613 // 返回类型中引用类型的为 Null 性与隐式实现的成员不匹配。
|
|
|
+
|
|
|
+ public async Task<EleUser> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken)
|
|
|
+#pragma warning restore CS8613 // 返回类型中引用类型的为 Null 性与隐式实现的成员不匹配。
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(normalizedUserName, nameof(normalizedUserName));
|
|
|
+
|
|
|
+ return await _userRepository.FindByNameAsync(normalizedUserName, AutoSaveChanges, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据格式的用户名获取用户
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+#pragma warning disable CS8613 // 返回类型中引用类型的为 Null 性与隐式实现的成员不匹配。
|
|
|
+
|
|
|
+ public Task<string> GetNormalizedUserNameAsync(EleUser user, CancellationToken cancellationToken)
|
|
|
+#pragma warning restore CS8613 // 返回类型中引用类型的为 Null 性与隐式实现的成员不匹配。
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+
|
|
|
+ return Task.FromResult(user.NormalizedUserName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取密码哈希值
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public Task<string?> GetPasswordHashAsync(EleUser user, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+
|
|
|
+#pragma warning disable CS8619 // 值中的引用类型的为 Null 性与目标类型不匹配。
|
|
|
+ return Task.FromResult(user.PasswordHash);
|
|
|
+#pragma warning restore CS8619 // 值中的引用类型的为 Null 性与目标类型不匹配。
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取用户Id
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public Task<string> GetUserIdAsync(EleUser user, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+
|
|
|
+ return Task.FromResult(user.Id.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取用户名
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+#pragma warning disable CS8613 // 返回类型中引用类型的为 Null 性与隐式实现的成员不匹配。
|
|
|
+
|
|
|
+ public Task<string> GetUserNameAsync(EleUser user, CancellationToken cancellationToken)
|
|
|
+#pragma warning restore CS8613 // 返回类型中引用类型的为 Null 性与隐式实现的成员不匹配。
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+
|
|
|
+ return Task.FromResult(user.UserName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 判断用户是否有密码
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public Task<bool> HasPasswordAsync(EleUser user, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+
|
|
|
+ return Task.FromResult(string.IsNullOrEmpty(user.PasswordHash));
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 格式化用户名
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="normalizedName"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public Task SetNormalizedUserNameAsync(EleUser user, string? normalizedName, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+
|
|
|
+ user.NormalizedUserName = normalizedName;
|
|
|
+
|
|
|
+ return Task.CompletedTask;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 设置用户密码
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="passwordHash"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public Task SetPasswordHashAsync(EleUser user, string? passwordHash, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+#pragma warning disable CS8604 // 引用类型参数可能为 null。
|
|
|
+ user.SetPasswordHash(passwordHash);
|
|
|
+#pragma warning restore CS8604 // 引用类型参数可能为 null。
|
|
|
+
|
|
|
+ return Task.CompletedTask;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 设置用户名
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="userName"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public Task SetUserNameAsync(EleUser user, string? userName, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+
|
|
|
+#pragma warning disable CS8604 // 引用类型参数可能为 null。
|
|
|
+ user.SetUserName(userName);
|
|
|
+#pragma warning restore CS8604 // 引用类型参数可能为 null。
|
|
|
+
|
|
|
+ return Task.CompletedTask;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 更新用户
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<IdentityResult> UpdateAsync(EleUser user, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+
|
|
|
+ user.LastModificationTime = DateTime.Now;
|
|
|
+ await _userRepository.UpdateAsync(user, AutoSaveChanges, cancellationToken);
|
|
|
+
|
|
|
+ return IdentityResult.Success;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 给用户分配角色
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="roleName"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ /// <exception cref="Exception"></exception>
|
|
|
+ public async Task AddToRoleAsync(EleUser user, string roleName, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+ Check.NotNull(roleName, nameof(roleName));
|
|
|
+
|
|
|
+ var role = await _roleRepository.FindByNameAsync(roleName, cancellationToken: cancellationToken);
|
|
|
+ if (role == null)
|
|
|
+ {
|
|
|
+ throw new Exception("角色不存在,角色名称:" + roleName);
|
|
|
+ }
|
|
|
+
|
|
|
+ user.AddRole(role.Id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 移除用户的角色
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="roleName"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task RemoveFromRoleAsync(EleUser user, string roleName, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+ Check.NotNull(roleName, nameof(roleName));
|
|
|
+
|
|
|
+ var role = await _roleRepository.FindByNameAsync(roleName, cancellationToken: cancellationToken);
|
|
|
+ if (role == null)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ user.RemoveRole(role.Id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取角色列表
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<IList<string>> GetRolesAsync(EleUser user, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+
|
|
|
+ var roleNames = await _userRepository.GetRoleNamesAsync(user.Id, cancellationToken);
|
|
|
+
|
|
|
+ return roleNames;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 判断用户是否包含某角色
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="user"></param>
|
|
|
+ /// <param name="roleName"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<bool> IsInRoleAsync(EleUser user, string roleName, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(user, nameof(user));
|
|
|
+ Check.NotNull(roleName, nameof(roleName));
|
|
|
+
|
|
|
+ var roles = await GetRolesAsync(user, cancellationToken);
|
|
|
+
|
|
|
+ return roles.Contains(roleName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据角色名获取用户列表
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="roleName"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<IList<EleUser>> GetUsersInRoleAsync(string roleName, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ cancellationToken.ThrowIfCancellationRequested();
|
|
|
+
|
|
|
+ Check.NotNull(roleName, nameof(roleName));
|
|
|
+
|
|
|
+ return await _userRepository.GetListByRoleNameAsync(roleName, cancellationToken: cancellationToken);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|