Beispiel #1
0
        public async Task <Content <Boolean> > UpdateUserPasswordAsync(Int32 id, String newPassword)
        {
            var resultContent = new Content <Boolean>();

            try
            {
                if (id <= 0)
                {
                    var message = $"Unable to {nameof(UpdateUserPasswordAsync)} for {nameof(user)} {id}";
                    resultContent.AppendError(new ArgumentOutOfRangeException(), message);
                    _logger_.LogError(message);
                }
                else if (String.IsNullOrEmpty(newPassword))
                {
                    var message = $"Unable to {nameof(UpdateUserPasswordAsync)} for {nameof(user)} id {id} - password cant be empty.";
                    resultContent.AppendError(new ArgumentException(), message);
                    _logger_.LogError(message);
                }
                else if (!PasswordFollowsComplexityRules(resultContent, newPassword))
                {
                    _logger_.LogError($"Password not complex enough.");
                }
                else
                {
                    var userContent = await GetUserWithSensitiveDataAsync(id);

                    if (userContent.HasError)
                    {
                        resultContent.AppendError(userContent.Errors);
                    }
                    else
                    {
                        var newPasswordHashed = BCrypt.Net.BCrypt.HashPassword(newPassword, userContent.Data.Salt);
                        if (String.Equals(newPasswordHashed, userContent.Data.Password, StringComparison.InvariantCulture))
                        {
                            var message = $"Unable to {nameof(UpdateUserPasswordAsync)} of {nameof(user)} - new value same as the old one";
                            resultContent.AppendError(new ArgumentException(), message);
                            _logger_.LogError(message);
                        }
                        else
                        {
                            await _endUserRepository_.UpdateUserPasswordAsync(id, newPasswordHashed);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                var message = $"Unable to {nameof(UpdateUserPasswordAsync)} of {nameof(user)} with id {id}";
                resultContent.AppendError(e, message);
                _logger_.LogError(e, message);
            }
            return(resultContent);
        }