public async Task <IActionResult> UpdateUserAsync(string id, [FromBody] UpdateUserAc updatedUserAc) { if (string.IsNullOrEmpty(updatedUserAc.Name)) { return(Ok(new { Message = "User's Name can't be null or empty", HasError = true })); } else if (updatedUserAc.UserGroupIdList.Count == 0) { return(Ok(new { Message = "Please select user role", HasError = true })); } else if (updatedUserAc.InstituteId == 0) { return(Ok(new { Message = "User's Institute can't be null or empty", HasError = true })); } else { if ((await _userManagementRepository.GetUserByIdAsync(id)) == null) { return(Ok(new { Message = "Please make sure the user exists", HasError = true })); } ApplicationUser currentUser = await _userManager.FindByNameAsync(User.Identity.Name); await _userManagementRepository.UpdateUserAsync(id, updatedUserAc, currentUser); return(Ok(new { Message = "User has been updated successfully" })); } }
/// <summary> /// Method for updating the details of logged in user /// </summary> /// <param name="id"></param> /// <param name="updatedUserAc"></param> /// <returns></returns> public async Task UpdateLoggedInUserProfileDetails(UpdateUserAc updatedUserAc, ApplicationUser currentUser) { ApplicationUser existingUser = _iMSDbContext.Users.First(x => x.Id.Equals(currentUser.Id)); existingUser.UpdatedBy = currentUser.Id; existingUser.UpdatedOn = DateTime.UtcNow; existingUser.Name = updatedUserAc.Name; await _userManager.UpdateAsync(existingUser); #region Set bell notification if (!await _userManager.IsInRoleAsync(currentUser, "SuperAdmin")) { // To the recipient NotificationAc notificationAc = new NotificationAc { NotificationMessage = "Profile Updated", NotificationTo = null, NotificationDetails = "You have successfully updated your profile", NotificationUserMappingsList = new List <NotificationUserMappingAc> { new NotificationUserMappingAc { UserId = currentUser.Id } } }; int currentUserInstituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(currentUser.Id, true); await _notificationManagementRepository.AddNotificationAsync(notificationAc, currentUserInstituteId, currentUser); notificationAc.NotificationUserMappingsList = new List <NotificationUserMappingAc>(); // To the admin if (!await _userManager.IsInRoleAsync(currentUser, "Admin")) { string instituteAdminId = (await _iMSDbContext.Institutes.FirstAsync(x => x.Id == currentUserInstituteId)).AdminId; notificationAc.NotificationDetails = string.Format("{0} has updated profile", currentUser.Name); notificationAc.NotificationUserMappingsList.Add(new NotificationUserMappingAc { UserId = instituteAdminId }); await _notificationManagementRepository.AddNotificationAsync(notificationAc, currentUserInstituteId, currentUser); } } #endregion }
public async Task <IActionResult> UpdateLoggedInUserProfileDetails([FromBody] UpdateUserAc updatedUserAc) { if (string.IsNullOrEmpty(updatedUserAc.Name)) { return(Ok(new { Message = "Name can't be null or empty", HasError = true })); } else { ApplicationUser currentUser = await _userManager.FindByNameAsync(User.Identity.Name); await _userManagementRepository.UpdateLoggedInUserProfileDetails(updatedUserAc, currentUser); return(Ok(new { Message = "Profile has been updated successfully" })); } }
/// <summary> /// Method for updating an existing user /// </summary> /// <param name="id"></param> /// <param name="updatedUserAc"></param> /// <returns></returns> public async Task UpdateUserAsync(string id, UpdateUserAc updatedUserAc, ApplicationUser currentUser) { // Update user details ApplicationUser existingUser = _iMSDbContext.Users.First(x => x.Id.Equals(id)); existingUser.UpdatedBy = currentUser.Id; existingUser.UpdatedOn = DateTime.UtcNow; existingUser.Name = updatedUserAc.Name; await _userManager.UpdateAsync(existingUser); // Add user institute UserInstituteMapping existingUserInstituteMapping = await _iMSDbContext.UserInstituteMappings.FirstOrDefaultAsync(x => x.UserId == id && x.InstituteId == updatedUserAc.InstituteId); if (existingUserInstituteMapping == null) { UserInstituteMapping userInstituteMapping = new UserInstituteMapping { InstituteId = updatedUserAc.InstituteId, UserId = existingUser.Id, CreatedOn = DateTime.UtcNow }; _iMSDbContext.UserInstituteMappings.Add(userInstituteMapping); await _iMSDbContext.SaveChangesAsync(); } // Update user role, if updated List <UserGroupMapping> userRolesMappingsList = await _iMSDbContext.UserGroupMapping.Where(x => x.UserId.Equals(existingUser.Id)).ToListAsync(); _iMSDbContext.UserGroupMapping.RemoveRange(userRolesMappingsList); await _iMSDbContext.SaveChangesAsync(); foreach (int userGroupId in updatedUserAc.UserGroupIdList) { UserGroup userGroup = await _iMSDbContext.UserGroups.FirstOrDefaultAsync(x => x.Id == userGroupId); if (userGroup != null) { UserGroupMapping userRolesMapping = new UserGroupMapping { UserGroupId = userGroup.Id, UserId = existingUser.Id, CreatedOn = DateTime.UtcNow }; _iMSDbContext.UserGroupMapping.Add(userRolesMapping); await _iMSDbContext.SaveChangesAsync(); } } }