public async Task <IdentityResult> Edit(ModificationRoleViewModel model) { using (var transaction = _entityContext.Database.BeginTransaction()) { try { IdentityResult result = null; foreach (string userId in model.IdsToAdd ?? new string[] { }) { User user = await _userManager.FindByIdAsync(userId); if (user != null) { result = await _userManager.AddToRoleAsync(user, model.RoleName); } } foreach (string userId in model.IdsToDelete ?? new string[] { }) { User user = await _userManager.FindByIdAsync(userId); if (user != null) { result = await _userManager.RemoveFromRoleAsync(user, model.RoleName); } } if (result == null) { throw new OperationException("The result could not be completed"); } if (!result.Succeeded) { transaction.Rollback(); } else { transaction.Commit(); } return(result); } catch (Exception) { transaction.Rollback(); throw; } } }
public async Task <IActionResult> Edit(ModificationRoleViewModel model) { if (ModelState.IsValid) { try { IdentityResult changePasswordResult = await _roleLogic.Edit(model); if (changePasswordResult != null) { if (!changePasswordResult.Succeeded) { AddErrorsFromResult(changePasswordResult); } else { ViewBag.Message = "The user roles were successfully modified."; } return(await Edit(model.RoleId)); } } catch (NotFoundException ex) { _logger.Log(LogLevel.Error, $"The requested resource was not found with the following message: {ex.Message} @ {GetType().Name}"); } catch (ArgumentException ex) { _logger.Log(LogLevel.Error, $"The requested resource was not found with the following message: {ex.Message} @ {GetType().Name}"); } catch (ConnectionException ex) { _logger.Log(LogLevel.Error, $"The following connection error occurred: {ex.Message} @ {GetType().Name}"); } catch (Exception ex) { _logger.Log(LogLevel.Error, $"A general exception occurred: {ex.Message} @ {GetType().Name}"); } } return(await Edit(model.RoleId)); }
public async Task <IdentityResult> Edit(ModificationRoleViewModel model) { try { return(await _roleRepository.Edit(model)); } catch (NullReferenceException) { throw new NotFoundException("Role was not found"); } catch (MySqlException) { throw new ConnectionException(); } catch (Exception) { throw new OperationException("An error occured while updating Role!"); } }