添加项目文件。
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
using IdentityService.Domain;
|
||||
using IdentityService.Domain.Entities;
|
||||
using IM.Commons;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
namespace IdentityService.Infrastructure
|
||||
{
|
||||
public class IdReposity(UserDbContext userDbContext, IdUserManager userManager, RoleManager<Role> roleManager) : IIdRepository
|
||||
{
|
||||
private readonly UserDbContext userDbContext = userDbContext;
|
||||
private readonly IdUserManager userManager = userManager;
|
||||
private readonly RoleManager<Role> roleManager = roleManager;
|
||||
|
||||
public async Task<IdentityResult> AccessFailedAsync(User user)
|
||||
{
|
||||
return await userManager.AccessFailedAsync(user);
|
||||
}
|
||||
|
||||
public async Task<IdentityResult> AddRoleAsync(User user, string role)
|
||||
{
|
||||
return await userManager.AddToRoleAsync(user, role);
|
||||
}
|
||||
|
||||
public async Task<IdentityResult> ChangePasswordAsync(User user, string password)
|
||||
{
|
||||
if (password.Length < 6)
|
||||
{
|
||||
IdentityError error = new IdentityError();
|
||||
error.Code = "Password Failure";
|
||||
error.Description = "密码长度不能小于6";
|
||||
return IdentityResult.Failed(error);
|
||||
}
|
||||
string token = await userManager.GeneratePasswordResetTokenAsync(user);
|
||||
return await userManager.ResetPasswordAsync(user, token, password);
|
||||
}
|
||||
|
||||
public async Task<SignInResult> CheckForSignInAsync(User user, string password, bool lockoutOnFailure)
|
||||
{
|
||||
if (await userManager.IsLockedOutAsync(user))
|
||||
{
|
||||
return SignInResult.LockedOut;
|
||||
}
|
||||
bool isSuccess = await userManager.CheckPasswordAsync(user, password);
|
||||
if (!isSuccess)
|
||||
{
|
||||
await userManager.AccessFailedAsync(user);
|
||||
return SignInResult.Failed;
|
||||
}
|
||||
else
|
||||
{
|
||||
return SignInResult.Success;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<IdentityResult> CreateAsync(User user, string password)
|
||||
{
|
||||
return await userManager.CreateAsync(user, password);
|
||||
}
|
||||
|
||||
public async Task<User?> FindByEmailAsync(string Email)
|
||||
{
|
||||
return await userManager.FindByEmailAsync(Email);
|
||||
}
|
||||
|
||||
public async Task<User?> FindByIdAsync(Guid id)
|
||||
{
|
||||
return await userManager.FindByIdAsync(id.ToString());
|
||||
}
|
||||
|
||||
public async Task<User?> FindByPhoneAsync(string phone)
|
||||
{
|
||||
return await userManager.Users.FirstOrDefaultAsync(x => x.PhoneNumber == phone);
|
||||
}
|
||||
|
||||
public async Task<User?> FindByUserNameAsync(string userName)
|
||||
{
|
||||
return await userManager.FindByNameAsync(userName);
|
||||
}
|
||||
|
||||
public async Task<IList<string>> GetRolesAsync(User user)
|
||||
{
|
||||
return await userManager.GetRolesAsync(user);
|
||||
}
|
||||
|
||||
public async Task<IdentityResult> RemoveAsync(User user)
|
||||
{
|
||||
var userStore = userManager.UserLoginStore;
|
||||
var cancelToken = default(CancellationToken);
|
||||
|
||||
var logins = await userStore.GetLoginsAsync(user, cancelToken);
|
||||
|
||||
foreach (var log in logins)
|
||||
{
|
||||
await userStore.RemoveLoginAsync(user, log.LoginProvider, log.ProviderKey, cancelToken);
|
||||
}
|
||||
user.SoftDelete();
|
||||
return await userManager.UpdateAsync(user);
|
||||
}
|
||||
|
||||
public Task<(IdentityResult, User?, string password)> ResetPasswordAsync(User user)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
public async Task<SignInResult> CheckEmailAsync(string email)
|
||||
{
|
||||
bool isExist = await userManager.Users.AnyAsync(x => x.Email == email);
|
||||
if (isExist)
|
||||
{
|
||||
return SignInResult.Failed;
|
||||
}
|
||||
else
|
||||
{
|
||||
return SignInResult.Success;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<SignInResult> CheckPhoneAsync(string phone)
|
||||
{
|
||||
bool isExist = await userManager.Users.AnyAsync(x => x.PhoneNumber == phone);
|
||||
if (isExist)
|
||||
{
|
||||
return SignInResult.Failed;
|
||||
}
|
||||
else
|
||||
{
|
||||
return SignInResult.Success;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<SignInResult> CheckUsernameAsync(string username)
|
||||
{
|
||||
var isExist = await userManager.Users.AnyAsync(x => x.UserName == username);
|
||||
if (isExist)
|
||||
{
|
||||
return SignInResult.Failed;
|
||||
}
|
||||
else
|
||||
{
|
||||
return SignInResult.Success;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TResult>> GetUsersAsync<TResult>(ISpecification<User, TResult> specification)
|
||||
{
|
||||
var query = userManager.Users.AsNoTracking();
|
||||
|
||||
if (specification.Criteria != null)
|
||||
{
|
||||
query = query.Where(specification.Criteria);
|
||||
}
|
||||
|
||||
if (specification.Includes != null && specification.Includes.Count() > 0)
|
||||
{
|
||||
foreach (var include in specification.Includes)
|
||||
{
|
||||
query = query.Include(include);
|
||||
}
|
||||
}
|
||||
return await query.Select(specification.Select).ToListAsync();
|
||||
|
||||
}
|
||||
public async Task<IEnumerable<User>> GetUsersAsync(ISpecification<User> specification)
|
||||
{
|
||||
var query = userManager.Users.AsNoTracking();
|
||||
|
||||
if (specification.Criteria != null)
|
||||
{
|
||||
query = query.Where(specification.Criteria);
|
||||
}
|
||||
|
||||
if (specification.Includes != null && specification.Includes.Count() > 0)
|
||||
{
|
||||
foreach (var include in specification.Includes)
|
||||
{
|
||||
query = query.Include(include);
|
||||
}
|
||||
}
|
||||
return await query.ToListAsync();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user