Exemple #1
0
 public async Task <bool> PermissionValidator(ActionExecutingContext context)
 {
     if (context.HttpContext.User.Claims.FirstOrDefault() != null)
     {
         string oId = context.HttpContext.User.Claims.FirstOrDefault(c => c.Type == azureOptions.UserClaimsUrl).Value;
         if (!(string.IsNullOrEmpty(oId)))
         {
             oId = EncryptionUtilities.GenerateSHA512String(oId);
             string requestPath = context.HttpContext.Request.Path.ToString();
             if (requestPath.Contains("get-user-profile"))
             {
                 if (context.ActionArguments.Count > 0)
                 {
                     Dictionary <string, object> parameters = new Dictionary <string, object>();
                     foreach (var param in context.ActionArguments)
                     {
                         parameters.Add(param.Key, param.Value);
                         if (param.Value.ToString() == oId)
                         {
                             return(await CheckPermissions(oId));
                         }
                     }
                 }
                 return(false);
             }
             return(await CheckPermissions(oId));
         }
     }
     return(false);
 }
        private string GetOId()
        {
            string oId = string.Empty;

            if (httpContextAccessor.HttpContext.User.Claims.FirstOrDefault() != null)
            {
                oId = httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == azureOptions.UserClaimsUrl).Value;
                oId = EncryptionUtilities.GenerateSHA512String(oId);
            }
            return(oId);
        }
        public async Task <UserProfileViewModel> UpsertUserProfileAsync(UserProfile userProfile)
        {
            if (userProfile == null || string.IsNullOrEmpty(userProfile?.OId))
            {
                throw new Exception("Please login into Application");
            }

            userProfile.OId = EncryptionUtilities.GenerateSHA512String(userProfile?.OId);
            UserProfileViewModel resultUP = await GetUserProfileDataAsync(userProfile?.OId, true);

            if (string.IsNullOrEmpty(resultUP?.OId))
            {
                userProfile.RoleInformationId.Add(await GetDefaultUserRole());
                List <dynamic> profile = new List <dynamic>();
                var            result  = await dbService.CreateItemAsync(userProfile, dbSettings.ProfilesCollectionId);

                profile.Add(result);
                resultUP = ConvertUserProfileViewModel(profile);
                resultUP.RoleInformation.Add(new RoleViewModel {
                    RoleName = Permissions.Role.Authenticated.ToString(), OrganizationalUnit = string.Empty
                });
            }
            else
            {
                List <Role> userRoles = await GetRoleDetailsAsync(resultUP.RoleInformationId);

                if (userRoles?.Count() > 0)
                {
                    List <RoleViewModel> roleViewModels = new List <RoleViewModel>();
                    foreach (var userRole in userRoles)
                    {
                        roleViewModels.Add(new RoleViewModel {
                            RoleName = userRole.RoleName, OrganizationalUnit = userRole.OrganizationalUnit
                        });
                    }
                    resultUP.RoleInformation = roleViewModels;
                }
            }
            resultUP.RoleInformationId = null;
            return(resultUP);
        }