public IActionResult SetRole([FromBody] SetUserRoleModel model) { Guard.AgainstNull(model, nameof(model)); using (_databaseContextFactory.Create()) { if (model.RoleName.Equals("Administrator", StringComparison.InvariantCultureIgnoreCase) && !model.Active && _systemUserQuery.AdministratorCount() == 1) { return(Ok(new { Success = false, FailureReason = "LastAdministrator" })); } } _bus.Send(new SetUserRoleCommand { UserId = model.UserId, RoleName = model.RoleName, Active = model.Active }); return(Ok(new { Success = true })); }
public void ProcessMessage(IHandlerContext <SetUserRoleCommand> context) { var message = context.Message; using (_databaseContextFactory.Create()) { if (!message.Active && message.RoleName.Equals("administrator") && _systemUserQuery.AdministratorCount() == 1) { return; } var user = new User(message.UserId); var stream = _eventStore.Get(message.UserId); stream.Apply(user); if (message.Active && !user.IsInRole(message.RoleName)) { stream.AddEvent(user.AddRole(message.RoleName)); } if (!message.Active && user.IsInRole(message.RoleName)) { stream.AddEvent(user.RemoveRole(message.RoleName)); } _eventStore.Save(stream); } }