public async Task ConfirmNewUserAsync(string userId, UserAndOrganizationDto userAndOrg) { var applicationUser = await _usersDbSet.FirstAsync(user => user.Id == userId); _userAdministrationValidator.CheckIfEmploymentDateIsSet(applicationUser.EmploymentDate); var hasRole = await _userManager.IsInRoleAsync(userId, Contracts.Constants.Roles.FirstLogin); _userAdministrationValidator.CheckIfUserHasFirstLoginRole(hasRole); var addRoleResult = await _userManager.AddToRoleAsync(userId, Contracts.Constants.Roles.User); var removeRoleResult = await _userManager.RemoveFromRoleAsync(userId, Contracts.Constants.Roles.NewUser); _userAdministrationValidator.CheckForAddingRemovingRoleErrors(addRoleResult.Errors.ToList(), removeRoleResult.Errors.ToList()); await _notificationService.SendConfirmedNotificationEmailAsync(applicationUser.Email, userAndOrg); SetTutorialStatus(applicationUser, false); await SetWelcomeKudosAsync(applicationUser); await AddUserToWallsForNewUsers(userAndOrg); await _uow.SaveChangesAsync(userAndOrg.UserId); }
public async Task <HttpResponseMessage> PutPersonalInfo(ApplicationUserPutPersonalInfoViewModel model) { var validatedModel = await ValidateModelAsync(model); if (!validatedModel.IsSuccessStatusCode) { return(validatedModel); } var userOrg = GetUserAndOrganization(); var user = await _applicationUserRepository.GetByIdAsync(model.Id); if (user == null) { return(Request.CreateResponse(HttpStatusCode.NotFound, new[] { string.Format(Resources.Common.DoesNotExist, Resources.Models.ApplicationUser.ApplicationUser.EntityName) })); } if ((user.FirstName != model.FirstName || user.LastName != model.LastName) && !await HasPermissionAsync(userOrg, AdministrationPermissions.ApplicationUser)) { return(Request.CreateResponse(HttpStatusCode.Forbidden)); } if (await _applicationUserRepository.Get(u => u.Email == model.Email && u.Id != user.Id).AnyAsync()) { return(Request.CreateResponse(HttpStatusCode.BadRequest, new[] { string.Format(Resources.Models.ApplicationUser.ApplicationUser.EmailAlreadyExsists) })); } if (user.PictureId != model.PictureId && !string.IsNullOrEmpty(user.PictureId)) { await _pictureService.RemoveImageAsync(user.PictureId, userOrg.OrganizationId); } _mapper.Map(model, user); _applicationUserRepository.Update(user); await _unitOfWork.SaveAsync(); if (!User.IsInRole(Roles.NewUser) || !await _userManager.IsInRoleAsync(user.Id, Roles.FirstLogin)) { return(Request.CreateResponse(HttpStatusCode.OK)); } await _userManager.RemoveFromRoleAsync(User.Identity.GetUserId(), Roles.FirstLogin); await _administrationUsersService.NotifyAboutNewUserAsync(user, userOrg.OrganizationId); var requiresConfirmation = await _organizationService.RequiresUserConfirmationAsync(userOrg.OrganizationId); if (!requiresConfirmation) { await _administrationUsersService.ConfirmNewUserAsync(userOrg.UserId, userOrg); } var response = new { requiresConfirmation }; return(Request.CreateResponse(HttpStatusCode.OK, response)); }