ApplicationUserManager.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.Logging;
  5. using Microsoft.Extensions.Options;
  6. using MTWorkHR.Core.Global;
  7. using MTWorkHR.Infrastructure.DBContext;
  8. namespace MTWorkHR.Infrastructure.Entities
  9. {
  10. public class ApplicationUserManager : UserManager<ApplicationUser>
  11. {
  12. private UserStore<ApplicationUser, ApplicationRole, HRDataContext, string, IdentityUserClaim<string>
  13. , IdentityUserRole<string>, IdentityUserLogin<string>, IdentityUserToken<string>
  14. , IdentityRoleClaim<string>>
  15. _store;
  16. private readonly GlobalInfo _globalInfo;
  17. public ApplicationUserManager(IUserStore<ApplicationUser> store, GlobalInfo globalInfo, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators, IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<ApplicationUser>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
  18. {
  19. _globalInfo = globalInfo;
  20. }
  21. private HRDataContext GetContext()
  22. {
  23. _store = (UserStore<ApplicationUser, ApplicationRole, HRDataContext, string, IdentityUserClaim<string>,
  24. IdentityUserRole<string>, IdentityUserLogin<string>, IdentityUserToken<string>, IdentityRoleClaim<string>>)this.Store;
  25. var context = _store.Context;
  26. return context;
  27. }
  28. public async Task<bool> UserHasAccess(string userId, string permmisions)
  29. {
  30. var context = GetContext();
  31. //check if curr user has admin role
  32. bool isAdminUser = await IsAdminAsync(userId);
  33. if (isAdminUser)
  34. return true;
  35. var permissionsArr = permmisions.Split(",").ToArray();
  36. var count = await context.UserRoles
  37. .Join(context.RolePermissions,
  38. userRole => userRole.RoleId,
  39. rolePermission => rolePermission.RoleId,
  40. (userRole, rolePermission) => new { userRole, rolePermission })
  41. .Join(context.Permissions,
  42. rp => rp.rolePermission.PermissionId,
  43. permission => permission.Id,
  44. (rp, permission) => new { rp, permission })
  45. .Where(x => permissionsArr.Any(a => a == x.permission.Name) && x.rp.userRole.UserId == userId)
  46. .CountAsync();
  47. return count > 0;
  48. }
  49. public async Task<string[]> GetUserPermission(string userId)
  50. {
  51. var context = GetContext();
  52. string[] permissions = Array.Empty<string>();
  53. //check if curr user has admin role
  54. bool isAdminUser = await IsAdminAsync(userId);
  55. //if curr user has admin role, return all available permissions for curr tenant
  56. if (isAdminUser)
  57. {
  58. permissions = context.Permissions
  59. .Select(xx => xx.Name).ToArray();
  60. }
  61. //if not admin then return only assigned permissions
  62. else
  63. {
  64. permissions = await context.UserRoles
  65. .Join(context.RolePermissions,
  66. userRole => userRole.RoleId,
  67. rolePermission => rolePermission.RoleId,
  68. (userRole, rolePermission) => new { userRole, rolePermission })
  69. .Join(context.Permissions,
  70. rp => rp.rolePermission.PermissionId,
  71. permission => permission.Id,
  72. (rp, permission) => new { rp, permission })
  73. .Where(x => x.rp.userRole.UserId == userId)
  74. .Select(x => x.permission.Name)
  75. .AsNoTracking().ToArrayAsync();
  76. }
  77. return permissions;
  78. }
  79. private async Task<bool> IsAdminAsync(string userId)
  80. {
  81. var context = GetContext();
  82. return await context.Roles
  83. .Join(context.UserRoles,
  84. role => role.Id,
  85. userRole => userRole.RoleId,
  86. (role, userRole) => new { role, userRole })
  87. .Where(x => x.userRole.UserId == userId && x.role.IsAdmin == true)
  88. .CountAsync() > 0;
  89. }
  90. public async Task<ApplicationUser> FindByAnyAsync(string name)
  91. {
  92. var context = GetContext();
  93. var res = await context.Users.FirstOrDefaultAsync(x => x.NormalizedUserName == name.ToUpper() || x.NormalizedEmail == name.ToUpper() || x.PhoneNumber.ToUpper() == name.ToUpper());
  94. return res;
  95. }
  96. public async Task<bool> IsStopped(string userId)
  97. {
  98. var context = GetContext();
  99. var user = await context.Users
  100. .FirstOrDefaultAsync(x => x.Id == userId);
  101. return user.IsStopped;
  102. }
  103. }
  104. }