Exemplo n.º 1
0
        public async Task <UserProfile> CreateUserProfileAsync(Guid userId, UserProfileSubmit userProfileSubmit, Guid createdById)
        {
            InitSharedTransaction();

            try
            {
                var existingUser = await userRepository.GetByIdAsync(userId, true);

                if (existingUser == null)
                {
                    throw new ItemNotFoundException($"User with ID '{userId}' not found.");
                }

                var existingUserProfile = existingUser.Profiles.FirstOrDefault(up => up.Name == userProfileSubmit.Name);

                if (existingUserProfile != null)
                {
                    throw new ItemNotProcessableException($"User profile with name '{userProfileSubmit.Name}' already exists for user.");
                }

                await AddNewProfileToUser(existingUser, userProfileSubmit, createdById);

                await userRepository.UpdateAsync(existingUser);

                CommitTransaction();

                return(mapper.Map <UserProfile>(existingUser.Profiles.FirstOrDefault(up => up.Name == userProfileSubmit.Name)));
            }
            catch
            {
                RollbackTransaction();
                throw;
            }
        }
Exemplo n.º 2
0
        private async Task AddNewProfileToUser(UserModel user, UserProfileSubmit userProfileSubmit, Guid createdById)
        {
            ProfileModel newUserProfile = new ProfileModel
            {
                Name        = userProfileSubmit.Name,
                Description = userProfileSubmit.Description,
                ChangedBy   = createdById
            };

            await CheckForSubRealmAndAssignToProfileIfExists(newUserProfile, userProfileSubmit);

            // Ensure we have a defacto blank ProfileRoles and ProfileTeams associations.
            newUserProfile.ProfileRoles = new List <ProfileRoleModel>();
            newUserProfile.ProfileTeams = new List <ProfileTeamModel>();
            await AssignRolesToUserProfileFromRoleIdList(newUserProfile, userProfileSubmit.RoleIds, createdById);
            await AssignTeamsToUserProfileFromTeamIdList(newUserProfile, userProfileSubmit.TeamIds, createdById);

            user.Profiles.Add(newUserProfile);
        }
Exemplo n.º 3
0
 public abstract Task <IActionResult> UpdateUserProfileAsync([FromRoute][Required] Guid userId, [FromRoute][Required] Guid profileId, [FromBody] UserProfileSubmit userProfileSubmit);
Exemplo n.º 4
0
        public async Task <UserProfile> UpdateUserProfileAsync(Guid userId, Guid userProfileId, UserProfileSubmit userProfileSubmit, Guid updatedById)
        {
            InitSharedTransaction();

            try
            {
                var existingUser = await userRepository.GetByIdAsync(userId, false);

                if (existingUser == null)
                {
                    throw new ItemNotFoundException($"User with ID '{userId}' not found.");
                }

                var existingUserProfile = await profileRepository.GetByIdAsync(userProfileId, true);

                if (existingUserProfile == null || existingUserProfile.User.Id != userId.ToString())
                {
                    throw new ItemNotFoundException($"User profile with ID '{userProfileId}' not found for user with ID '{userId}'.");
                }

                await UpdateExistingUserProfile(existingUser, existingUserProfile, userProfileSubmit, updatedById);

                await userRepository.UpdateAsync(existingUser);

                CommitTransaction();

                return(mapper.Map <UserProfile>(existingUser.Profiles.FirstOrDefault(up => up.Id == userProfileId)));
            }
            catch
            {
                RollbackTransaction();
                throw;
            }
        }
Exemplo n.º 5
0
        private async Task CheckForSubRealmAndAssignToProfileIfExists(ProfileModel profile, UserProfileSubmit userProfileSubmit)
        {
            // Recall that submit models with empty GUIDs will not be null but rather Guid.Empty.
            if (userProfileSubmit.SubRealmId == null || userProfileSubmit.SubRealmId == Guid.Empty)
            {
                throw new InvalidFormatException("Profiles must contain a 'sub_realm_id'.");
            }

            var existingSubRealm = await subRealmRepository.GetByIdAsync(userProfileSubmit.SubRealmId, false);

            profile.SubRealm = existingSubRealm ?? throw new ItemNotFoundException($"Sub-realm with ID '{userProfileSubmit.SubRealmId}' does not exist.");
        }
Exemplo n.º 6
0
        private async Task UpdateExistingUserProfile(UserModel user, ProfileModel userProfile, UserProfileSubmit userProfileSubmit, Guid updatedById)
        {
            // If the name is being updated, ensure that the does not have a profile with the new name.
            if (userProfile.Name != userProfileSubmit.Name)
            {
                var existingUserProfileWithName = user.Profiles.FirstOrDefault(up => up.Name == userProfileSubmit.Name);

                if (existingUserProfileWithName != null)
                {
                    throw new ItemNotProcessableException($"User with ID '{user.Id}' already has a profile with name '{userProfileSubmit.Name}'! Cannot update current profile name to that name.");
                }

                userProfile.Name      = userProfileSubmit.Name;
                userProfile.ChangedBy = updatedById;
            }

            if (userProfile.Description != userProfileSubmit.Description)
            {
                userProfile.Description = userProfileSubmit.Description;
                userProfile.ChangedBy   = updatedById;
            }

            await AssignRolesToUserProfileFromRoleIdList(userProfile, userProfileSubmit.RoleIds, updatedById);
            await AssignTeamsToUserProfileFromTeamIdList(userProfile, userProfileSubmit.TeamIds, updatedById);
        }
Exemplo n.º 7
0
 public async override Task <IActionResult> UpdateUserProfileAsync([FromRoute, Required] Guid userId, [FromRoute, Required] Guid profileId, [FromBody] UserProfileSubmit userProfileSubmit)
 {
     return(Ok(await profileService.UpdateUserProfileAsync(userId, profileId, userProfileSubmit, ClaimsHelper.GetUserId(User))));
 }