using Hutopy.Application.Common.Interfaces; using Hutopy.Application.Common.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; namespace Hutopy.Infrastructure.Identity; public class IdentityService( UserManager userManager, IUserClaimsPrincipalFactory userClaimsPrincipalFactory, IAuthorizationService authorizationService) : IIdentityService { public async Task GetUserNameAsync(string userId) { var user = await userManager.FindByIdAsync(userId); return user?.UserName; } public async Task<(Result Result, string UserId)> CreateUserAsync(string userName, string password) { var user = new ApplicationUser { UserName = userName, Email = userName, }; var result = await userManager.CreateAsync(user, password); return (result.ToApplicationResult(), user.Id); } public async Task IsInRoleAsync(string userId, string role) { var user = await userManager.FindByIdAsync(userId); return user != null && await userManager.IsInRoleAsync(user, role); } public async Task AuthorizeAsync(string userId, string policyName) { var user = await userManager.FindByIdAsync(userId); if (user == null) { return false; } var principal = await userClaimsPrincipalFactory.CreateAsync(user); var result = await authorizationService.AuthorizeAsync(principal, policyName); return result.Succeeded; } public async Task DeleteUserAsync(string userId) { var user = await userManager.FindByIdAsync(userId); return user != null ? await DeleteUserAsync(user) : Result.Success(); } public async Task DeleteUserAsync(ApplicationUser user) { var result = await userManager.DeleteAsync(user); return result.ToApplicationResult(); } }