gehgassi_backend/gehGassi.Web/Auth/CustomClaimsPrincipalFactory.cs

105 lines
4.8 KiB
C#

using System.Security.Claims;
using System.Threading.Tasks;
using gehGassi.Core.Interfaces;
using gehGassi.Domain.Roles;
using gehGassi.Domain.Users;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
namespace gehGassi.Web.Auth
{
public class CustomClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, ApplicationRole>
{
private readonly ICustomerService _customerService;
private readonly IAppUserService _appUserService;
public CustomClaimsPrincipalFactory(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager,
IOptions<IdentityOptions> optionsAccessor, ICustomerService customerService, IAppUserService appUserService) : base(userManager, roleManager, optionsAccessor)
{
_customerService = customerService;
_appUserService = appUserService;
}
public override async Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
{
var principal = await base.CreateAsync(user);
//var roles = await UserManager.GetRolesAsync(user);
var customerName = "";
var customerUniqueId = "";
var selectedCustomerId = "";
var selectedCustomerUniqueId = "";
var selectedCustomerName = "";
var customer = await _customerService.GetAsync(user.CustomerId);
if (customer != null)
{
customerName = customer.Name;
customerUniqueId = customer.UniqueId.ToString();
selectedCustomerId = customer.Id.ToString();
selectedCustomerUniqueId = customer.UniqueId.ToString();
selectedCustomerName = customer.Name;
}
if (user.Settings.SelectedCustomerId.HasValue)
{
if (await UserManager.IsInRoleAsync(user, "Administrator") || await UserManager.IsInRoleAsync(user, "PowerUser"))
{
var selectedCustomer = await _customerService.GetAsync(user.Settings.SelectedCustomerId.Value);
if (selectedCustomer != null)
{
selectedCustomerId = selectedCustomer.Id.ToString();
selectedCustomerUniqueId = selectedCustomer.UniqueId.ToString();
selectedCustomerName = selectedCustomer.Name;
}
}
}
var appUserName = "";
var selectedAppUserId = "";
var selectedAppUserName = "";
var appUser = await _appUserService.GetAsync(user.AppUserId);
if (appUser != null)
{
appUserName = $"{appUser.FirstName} {appUser.LastName}";
selectedAppUserId = appUser.Id ?? "";
selectedAppUserName = $"{appUser.FirstName} {appUser.LastName}";
}
if (!string.IsNullOrWhiteSpace(user.Settings.SelectedAppUserId))
{
if (await UserManager.IsInRoleAsync(user, "Administrator") || await UserManager.IsInRoleAsync(user, "PowerUser"))
{
var selectedAppUser = await _appUserService.GetAsync(user.Settings.SelectedAppUserId);
if (selectedAppUser != null)
{
selectedAppUserId = selectedAppUser.Id ?? "";
selectedAppUserName = $"{selectedAppUser.FirstName} {selectedAppUser.LastName}";
}
}
}
// Add your claims here
((ClaimsIdentity)principal.Identity).
AddClaims(new[] {
new Claim(ClaimTypes.Name,user.LastName.ToString()),
new Claim(ClaimTypes.GivenName,user.FirstName.ToString()),
new Claim(ClaimConstants.PackedPermissionClaimType,user.Permissions.ToString()),
new Claim(ClaimConstants.CustomerIdClaimType, user.CustomerId.ToString()),
new Claim(ClaimConstants.CustomerUniqueIdClaimType, customerUniqueId),
new Claim(ClaimConstants.CustomerNameClaimType, customerName),
new Claim(ClaimConstants.SelectedCustomerIdClaimType, selectedCustomerId),
new Claim(ClaimConstants.SelectedCustomerUniqueIdClaimType, selectedCustomerUniqueId),
new Claim(ClaimConstants.SelectedCustomerNameClaimType, selectedCustomerName),
new Claim(ClaimConstants.AppUserIdClaimType, user.AppUserId ?? ""),
new Claim(ClaimConstants.AppUserNameClaimType, appUserName),
new Claim(ClaimConstants.SelectedAppUserIdClaimType, selectedAppUserId ?? ""),
new Claim(ClaimConstants.SelectedAppUserNameClaimType, selectedAppUserName),
});
return principal;
}
}
}