public async Task ResetUsername(SystemUserResetUsernameModel model)
        {
            var systemUser = await GetItem(new Guid(model.Id));

            if (systemUser == null)
            {
                throw new UserNotFoundException();
            }

            if (string.Compare(_hashingService.DecryptString(systemUser.Password), model.ConfirmPassword, false) != 0)
            {
                throw new PasswordsDoNotMatchException();
            }
            if (await UsernameAlreadyExists(model.ResetUsername))
            {
                throw new UsernameAlreadyExistsException(model.ResetUsername);
            }

            //  TODO: All or nothing!
            //  NoSQL databases do not support updating the partition key value of an existing item. Need to delete entire document and create new.
            //  Transactions using the same partition key (but this process uses 2 different partition keys) ~ https://devblogs.microsoft.com/cosmosdb/introducing-transactionalbatch-in-the-net-sdk/
            await _systemUsersManager.DeleteItemAsync(systemUser);

            systemUser.Username = model.ResetUsername;

            var systemUserModel = new SystemUserAuthenticateModel(systemUser);

            await CreateItem(systemUserModel);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> ResetUsername(SystemUserResetUsernameModel model)
        {
            try
            {
                await _systemUserService.ResetUsername(model);

                response = new ApiResponse(HttpStatusCode.OK, string.Format("System user with username '{0}' updated successfully.", model.ResetUsername), null);
                return(Ok(new { response }));
            }
            catch (UserNotFoundException exception)
            {
                response = new ApiResponse(HttpStatusCode.NotFound, exception.Message, null);
                return(Ok(new { response }));
            }
            catch (PasswordsDoNotMatchException exception)
            {
                response = new ApiResponse(HttpStatusCode.Conflict, exception.Message, null);
                return(Ok(new { response }));
            }
            catch (UsernameAlreadyExistsException exception)
            {
                response = new ApiResponse(HttpStatusCode.Conflict, string.Format("Username '{0}' already exists in the system database.", model.ResetUsername), null);
                return(Ok(new { response }));
            }
            catch (Exception exception)
            {
                return(BadRequest("System user update failed. Error: " + exception.Message));
            }
        }
 public SystemUserResetUsername(SystemUserResetUsernameModel entity)
 {
     ResetUsername   = entity.ResetUsername;
     ConfirmPassword = entity.ConfirmPassword;
 }