using CPRNIMS.Infrastructure.Database; using CPRNIMS.Infrastructure.Entities.Account; using CPRNIMS.Infrastructure.Models; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Text; using System.Threading.Tasks; namespace CPRNIMS.Domain.Services.Account { public class UserClaimsManager { private readonly UserManager _userManager; private readonly NonInventoryDbContext _dbContext; RoleManager _roleManager; public UserClaimsManager(UserManager userManager, NonInventoryDbContext dbContext, RoleManager roleManager) { _userManager = userManager; _dbContext = dbContext; _roleManager = roleManager; } public async Task> GetAllUsersProfile() { try { var usersWithRolesAndAttachments = await (from user in _userManager.Users join userRole in _dbContext.IdentityUserRoles on user.Id equals userRole.UserId into userRoles from ur in userRoles.DefaultIfEmpty() join role in _dbContext.Roles on ur.RoleId equals role.Id into roles from r in roles.DefaultIfEmpty() join attachment in _dbContext.Attachments on user.Id equals attachment.AttachmentId into attachments from a in attachments.DefaultIfEmpty() select new { user.Id, Role = r != null ? r.Name ?? "N/A" : "N/A", URL = a != null ? a.URL ?? "N/A" : "N/A", FileName = a != null ? a.FileName ?? "N/A" : "N/A", user.Company, user.DepartmentId, user.UserName, user.FullName, user.Email, user.EmailConfirmed, Address = user.Address ?? "N/A", PhoneNumber = user.PhoneNumber ?? "N/A", user.LockoutEnd, user.LockoutEnabled, user.CreatedBy, user.UpdatedBy, user.CreatedDate, user.UpdatedDate }).ToListAsync(); return usersWithRolesAndAttachments.Cast().ToList(); } catch (Exception ex) { ex.ToString(); throw; } } public async Task AssignUserRole(RegisterModel registerModel) { var user = await _userManager.FindByIdAsync(registerModel.Id); if (user != null) { // Check if the role exists, if not, create it if (!await _roleManager.RoleExistsAsync(registerModel.Role)) { await _roleManager.CreateAsync(new IdentityRole(registerModel.Role)); } // Assign the user to the role await _userManager.AddToRoleAsync(user, registerModel.Role); } } public async Task AddCustomClaim(RegisterModel registerModel) { var user = await _userManager.FindByIdAsync(registerModel.Id); if (user != null) { // Define your role claim var roleClaim = new Claim(ClaimTypes.Role, registerModel.Role); // Use UserManager.AddClaimAsync to add the role claim to the user var result = await _userManager.AddClaimAsync(user, roleClaim); // Optionally, add additional custom claims here if needed } } public async Task UpdateCustomClaim(RegisterModel appUser, string currentClaim) { var user = await _userManager.FindByIdAsync(appUser.Id); if (user != null) { // Define your custom claim var claim = new Claim(appUser.Role, currentClaim); var newClaim = new Claim(appUser.Role, appUser.Role); await _userManager.ReplaceClaimAsync(user, claim, newClaim); } } } }