Skip to content

Commit

Permalink
#170 - improve feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Aug 26, 2016
1 parent 069449d commit b9d80b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/main/java/alfio/manager/user/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

Expand Down Expand Up @@ -219,8 +220,7 @@ public ValidationResult validateUser(Integer id, String username, int organizati
if(existing > 0) {
return ValidationResult.failed(new ValidationResult.ValidationError("username", "There is already another user with the same username."));
}
return ValidationResult.of(Arrays.asList(Pair.of(firstName, "firstName"), Pair.of(lastName, "lastName"), Pair.of(emailAddress, "emailAddress"))
.stream()
return ValidationResult.of(Stream.of(Pair.of(firstName, "firstName"), Pair.of(lastName, "lastName"), Pair.of(emailAddress, "emailAddress"))
.filter(p -> StringUtils.isEmpty(p.getKey()))
.map(p -> new ValidationResult.ValidationError(p.getKey(), p.getValue() + " is required"))
.collect(toList()));
Expand All @@ -234,13 +234,13 @@ public ValidationResult validateNewPassword(String username, String oldPassword,
List<ValidationResult.ValidationError> errors = new ArrayList<>();
Optional<String> password = userRepository.findPasswordByUsername(username);
if(!password.filter(p -> passwordEncoder.matches(oldPassword, p)).isPresent()) {
errors.add(new ValidationResult.ValidationError("old-password-invalid", "wrong password"));
errors.add(new ValidationResult.ValidationError("alfio.old-password-invalid", "wrong password"));
}
if(!PasswordGenerator.isValid(newPassword)) {
errors.add(new ValidationResult.ValidationError("new-password-invalid", "new password is not strong enough"));
errors.add(new ValidationResult.ValidationError("alfio.new-password-invalid", "new password is not strong enough"));
}
if(!StringUtils.equals(newPassword, newPasswordConfirm)) {
errors.add(new ValidationResult.ValidationError("new-password-does-not-match", "new password has not been confirmed"));
errors.add(new ValidationResult.ValidationError("alfio.new-password-does-not-match", "new password has not been confirmed"));
}
return ValidationResult.of(errors);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ <h4>Personal information</h4>
</div>
</form>
<div class="clearfix"></div>
<div uib-alert type="success" close="editCurrentUserCtrl.dismissMessage()" dismiss-on-timeout="1500" data-ng-if="editCurrentUserCtrl.showMessage">Success!</div>
<div class="page-header">
<h4>Change Password</h4>
</div>
Expand All @@ -54,8 +55,9 @@ <h4>Change Password</h4>
<button type="reset" class="btn btn-default">Cancel</button>
</div>

<div data-ng-messages="editCurrentUserCtrl.changePassword.$error" class="text-danger" data-ng-messages-multiple>
<div data-ng-message="new-password-invalid">

<div data-ng-messages="editCurrentUserCtrl.changePassword.$error" class="text-danger wMarginBottom" data-ng-messages-multiple>
<div data-ng-message="alfio.new-password-invalid">
<span>The new password doesn't match the required format. It must be <strong>at least</strong> 10 characters and it must: </span>
<ul>
<li>contain <strong>at least</strong> an uppercase letter <code>A-Z</code></li>
Expand All @@ -64,8 +66,8 @@ <h4>Change Password</h4>
<li><strong>not</strong> contain spaces</li>
</ul>
</div>
<div data-ng-message="new-password-does-not-match">"New password" and "Confirm new password" don't match</div>
<div data-ng-message="old-password-invalid">Current password is not valid</div>
<div data-ng-message="alfio.new-password-does-not-match">"New password" and "Confirm new password" don't match</div>
<div data-ng-message="alfio.old-password-invalid">Current password is not valid</div>
<div data-ng-message="pattern">The value should match the following pattern: {{requiredPattern}}</div>
</div>

Expand Down
17 changes: 14 additions & 3 deletions src/main/webapp/resources/js/admin/feature/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@

EditUserController.$inject = ['$state', '$stateParams', '$rootScope', '$q', 'OrganizationService', 'UserService', 'ValidationService', '$q'];

function EditCurrentUserController($state, user, UserService, ValidationService) {
function EditCurrentUserController($state, user, UserService, $timeout) {
var self = this;
self.user = user;
self.original = user;
Expand All @@ -159,6 +159,7 @@
return UserService.editUser(self.user).then(function() {
self.original = angular.copy(self.user);
self.loading = false;
self.showMessage = true;
});
});
promise.then(function() {}, function(err) {
Expand All @@ -175,14 +176,21 @@
};

self.updatePassword = function() {
['alfio.new-password-invalid', 'alfio.new-password-does-not-match', 'alfio.old-password-invalid'].forEach(function(e) {
self.changePassword.$setValidity(e, true);
});
if(self.changePassword.$valid) {
self.loading = true;
UserService.updatePassword(self.passwordContainer).then(function(result) {
var validationResult = result.data;
if(validationResult.success) {
self.passwordContainer = {};
self.changePasswordErrors = {};
alert('succeeded');
self.showMessage = true;
$timeout(function() {
self.changePassword.$setPristine();
self.changePassword.$setUntouched();
});
} else {
angular.forEach(validationResult.validationErrors, function(e) {
self.changePassword.$setValidity(e.fieldName, false);
Expand All @@ -191,11 +199,14 @@
self.loading = false;
});
}
self.dismissMessage = function() {
self.showMessage = false;
};
};

}

EditCurrentUserController.$inject = ['$state', 'user', 'UserService', 'ValidationService'];
EditCurrentUserController.$inject = ['$state', 'user', 'UserService', '$timeout'];

function OrganizationService($http, HttpErrorHandler) {
return {
Expand Down

0 comments on commit b9d80b7

Please sign in to comment.