private FormResult PostUpdateUserForm(Form form) { // Get web and user details Web web = _authenticationService.Web; long userId = _authenticationService.GetCurrentUser().User.UserId; // Get new profile details from form values UpdateUserModel model = new UpdateUserModel { Alias = ((TextField)form.Fields["alias"]).Value, Email = ((TextField)form.Fields["email"]).Value, TenantId = web.TenantId, UserId = userId }; // Get upload identifiers for thumbnail, preview and source images if (web.UserHasImage) { string uploadIds = ((UploadField)form.Fields["upload"]).Value; if (!string.IsNullOrWhiteSpace(uploadIds)) { string[] uploadParts = uploadIds.Split('|'); model.ImageTenantId = web.TenantId; model.ThumbnailImageUploadId = Convert.ToInt64(uploadParts[0]); model.PreviewImageUploadId = Convert.ToInt64(uploadParts[1]); model.ImageUploadId = Convert.ToInt64(uploadParts[2]); } } // Update user profile string status = null; bool confirmRequired = _authenticationService.UpdateUser(model); if (confirmRequired) { _authenticationService.Logoff(); status = _authenticationUrlService.GetUpdateProfileLogonUrl(); } // Return form result with no errors return(_formHelperService.GetFormResult(status)); }
public bool UpdateUser(UpdateUserModel model, IUnitOfWork unitOfWork = null) { // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null; // Do the user update try { // Validate supplied details (including checking that all or none of the image upload properties are specified) _authenticationValidator.ValidateUpdateUser(model); // Get existing user details User user = _userRepository.ReadUser(model.TenantId, model.UserId); string currentAlias = user.Alias; string currentEmail = user.Email; // Get new details string newAlias = model.Alias.Trim(); string newEmail = model.Email.Trim().ToLower(); user.Alias = newAlias; user.Email = newEmail; // Has email address changed? bool reconfirm = (currentEmail != newEmail); if (reconfirm) { // Get confirm verification token TimeSpan expiryTimeSpan = _authenticationConfigurationService.GetUpdateUserExpiryTimeSpan(model.TenantId); Token confirmToken = _securityService.CreateToken(expiryTimeSpan); // Unconfirm account user.Confirmed = false; user.ConfirmTokenValue = confirmToken.Value.ToString(); user.ConfirmTokenExpiry = confirmToken.Expiry; // Commit user images? if (model.ImageUploadId.HasValue && user.ImageUploadId != model.ImageUploadId) { _uploadService.Commit(model.ImageTenantId.Value, model.ThumbnailImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork); _uploadService.Commit(model.ImageTenantId.Value, model.PreviewImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork); _uploadService.Commit(model.ImageTenantId.Value, model.ImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork); user.ImageTenantId = model.ImageTenantId; user.ThumbnailImageUploadId = model.ThumbnailImageUploadId; user.PreviewImageUploadId = model.PreviewImageUploadId; user.ImageUploadId = model.ImageUploadId; } // Update user details _userRepository.UpdateUser(user, unitOfWork ?? localUnitOfWork); // Get details of email that will be sent to user who must re-confirm their account Email email = _authenticationConfigurationService.GetUpdateUserEmail(Web, Domain, newEmail, newAlias, confirmToken); // Send email to newly created user _emailService.SendEmail(email); } // If email address not changed if (!reconfirm) { // Commit user images? if (model.ImageUploadId.HasValue && user.ImageUploadId != model.ImageUploadId) { _uploadService.Commit(model.ImageTenantId.Value, model.ThumbnailImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork); _uploadService.Commit(model.ImageTenantId.Value, model.PreviewImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork); _uploadService.Commit(model.ImageTenantId.Value, model.ImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork); user.ImageTenantId = model.ImageTenantId; user.ThumbnailImageUploadId = model.ThumbnailImageUploadId; user.PreviewImageUploadId = model.PreviewImageUploadId; user.ImageUploadId = model.ImageUploadId; } // Update user details _userRepository.UpdateUser(user, unitOfWork ?? localUnitOfWork); // If email address not changed, update authenticated user details AuthenticatedUserInfo userInfo = GetCurrentUser(); userInfo.User.Alias = user.Alias; _authenticationProviderService.LogonAuthenticatedUser(userInfo); } // Commit work if local unit of work in place if (localUnitOfWork != null) { localUnitOfWork.Commit(); } // Return whether or not user must reconfirm address return(reconfirm); } catch (ValidationErrorException) { if (localUnitOfWork != null) { localUnitOfWork.Rollback(); } throw; } catch (Exception ex) { if (localUnitOfWork != null) { localUnitOfWork.Rollback(); } throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex); } }