Skip to content

Commit

Permalink
#76 - Polishing.
Browse files Browse the repository at this point in the history
Some cleanups in PersistentUserAccountManager, use of lambdas in SpringSecurityAuthenticationManager. Introduced UserAccount.getUsername() to avoid leaking internals via the identifier.
  • Loading branch information
odrotbohm committed Dec 18, 2014
1 parent 56eddeb commit 1bd3fef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ public Optional<UserAccount> get(UserAccountIdentifier userAccountIdentifier) {
@Transactional
public UserAccount save(UserAccount userAccount) {

Assert.notNull(userAccount, "userAccount must not be null");
Assert.notNull(userAccount, "UserAccount must not be null!");

Password password = userAccount.getPassword();
Password passwordToSet = password.isEncrypted() ? password : new Password(passwordEncoder.encode(password
.toString()), true);
userAccount.setPassword(passwordToSet);

userAccount.setPassword(password.isEncrypted() ? password : new Password(
passwordEncoder.encode(password.toString()), true));

return repository.save(userAccount);
}

Expand All @@ -91,9 +92,10 @@ public UserAccount save(UserAccount userAccount) {
* @see org.salespointframework.useraccount.UserAccountManager#enable(org.salespointframework.useraccount.UserAccountIdentifier)
*/
@Override
@Transactional
public void enable(UserAccountIdentifier userAccountIdentifier) {

Assert.notNull(userAccountIdentifier, "userAccountIdentifier must not be null");
Assert.notNull(userAccountIdentifier, "UserAccountIdentifier must not be null!");
get(userAccountIdentifier).ifPresent(account -> account.setEnabled(true));
}

Expand All @@ -102,9 +104,10 @@ public void enable(UserAccountIdentifier userAccountIdentifier) {
* @see org.salespointframework.useraccount.UserAccountManager#disable(org.salespointframework.useraccount.UserAccountIdentifier)
*/
@Override
@Transactional
public void disable(UserAccountIdentifier userAccountIdentifier) {

Assert.notNull(userAccountIdentifier, "userAccountIdentifier must not be null");
Assert.notNull(userAccountIdentifier, "UserAccountIdentifier must not be null!");
get(userAccountIdentifier).ifPresent(account -> account.setEnabled(false));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ public Optional<UserAccount> getCurrentUser() {
@Override
public boolean matches(Password candidate, Password existing) {

Assert.notNull(existing);
Assert.notNull(existing, "Existing password must not be null!");

if (candidate == null) {
return false;
}

return passwordEncoder.matches(candidate.toString(), existing.toString());
return Optional.ofNullable(candidate).//
map(c -> passwordEncoder.matches(c.toString(), existing.toString())).//
orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public final UserAccountIdentifier getIdentifier() {
return userAccountIdentifier;
}

/**
* Returns the user's username.
*
* @return will never be {@literal null}.
*/
public String getUsername() {
return userAccountIdentifier.getIdentifier();
}

/**
* Adds a {@link Role} to the {@link UserAccount}.
*
Expand Down

0 comments on commit 1bd3fef

Please sign in to comment.