NonInventPurchasingSystem/CPRNIMS.Domain/Services/Account/UserClaimsManager.cs
2026-05-20 16:50:48 +08:00

106 lines
5.1 KiB
C#

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<ApplicationUser> _userManager;
private readonly NonInventoryDbContext _dbContext;
RoleManager<IdentityRole> _roleManager;
public UserClaimsManager(UserManager<ApplicationUser> userManager,
NonInventoryDbContext dbContext, RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_dbContext = dbContext;
_roleManager = roleManager;
}
public async Task<List<object>> GetAllUsersProfile()
{
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 ?? "404userImage" : "404userImage",
FileName = a != null ? a.FileName ?? "404userImage.jpg" : "404userImage.jpg",
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<object>().ToList();
}
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);
}
}
}
}