Skip to content

Commit

Permalink
[Fix] Fixed storing first login on LDAP login
Browse files Browse the repository at this point in the history
  • Loading branch information
eitch committed Jun 11, 2024
1 parent abe4151 commit caeb904
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,17 @@ private Certificate internalAuthenticate(String username, char[] password, Strin
persistSessionsAsync();

// save last login
UserHistory history = user.getHistory();
UserHistory history = user.getHistory().withLastLogin(ZonedDateTime.now());
if (history.isFirstLoginEmpty())
history = history.withFirstLogin(ZonedDateTime.now());
history = history.withLastLogin(ZonedDateTime.now());
this.persistenceHandler.replaceUser(user.withHistory(history));

if (!this.persistenceHandler.hasUser(user.getUsername())) {
// for remote, the user won't exist, if it is the user's first login
this.persistenceHandler.addUser(user.withHistory(history));
} else {
// otherwise we replace the user
this.persistenceHandler.replaceUser(user.withHistory(history));
}
persistModelAsync();

// log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,11 @@ protected User checkCredentialsAndUserState(String username, char[] password) th

// build user
try {
User user = this.queryContext.buildUserFromSearchResult(username, searchResult);

// persist this user
User user = this.queryContext.buildUserFromSearchResult(username, searchResult);
if (internalUser == null)
this.persistenceHandler.addUser(user);
else
this.persistenceHandler.replaceUser(user);

if (this.autoPersistOnUserChangesData)
persistModelAsync();

return user;
return user;
return user.withHistory(internalUser.history());

} catch (AccessDeniedException e) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public interface PersistenceHandler {
*/
User getUser(String username);

/**
* Returns true if the user exists with the give name, false otherwise
*
* @param username the name of the user to check for
*
* @return true if the user exists with the give name, false otherwise
*/
boolean hasUser(String username);

/**
* Returns a {@link Group} object from the underlying database
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,14 @@ public List<Role> getAllRoles() {
}
}

@Override
public boolean hasUser(String username) {
return this.userMap.containsKey(evaluateUsername(username));
}

@Override
public User getUser(String username) {
return this.userMap.get(this.caseInsensitiveUsername ? username.toLowerCase() : username);
return this.userMap.get(evaluateUsername(username));
}

@Override
Expand All @@ -116,7 +121,7 @@ public Role getRole(String roleName) {

@Override
public User removeUser(String username) {
User user = this.userMap.remove(this.caseInsensitiveUsername ? username.toLowerCase() : username);
User user = this.userMap.remove(evaluateUsername(username));
this.userMapDirty = user != null;
return user;
}
Expand All @@ -137,7 +142,7 @@ public Role removeRole(String roleName) {

@Override
public void addUser(User user) {
String username = this.caseInsensitiveUsername ? user.getUsername().toLowerCase() : user.getUsername();
String username = evaluateUsername(user.getUsername());
if (this.userMap.containsKey(username))
throw new IllegalStateException(format("The user {0} already exists!", user.getUsername()));
this.userMap.put(username, user);
Expand All @@ -146,7 +151,7 @@ public void addUser(User user) {

@Override
public void replaceUser(User user) {
String username = this.caseInsensitiveUsername ? user.getUsername().toLowerCase() : user.getUsername();
String username = evaluateUsername(user.getUsername());
if (!this.userMap.containsKey(username))
throw new IllegalStateException(
format("The user {0} can not be replaced as it does not exist!", user.getUsername()));
Expand Down Expand Up @@ -219,8 +224,8 @@ public void initialize(Map<String, String> paramsMap) {
this.groupsPath = groupsPath;
this.rolesPath = rolesPath;

this.caseInsensitiveUsername = !this.parameterMap.containsKey(PARAM_CASE_INSENSITIVE_USERNAME) || parseBoolean(
this.parameterMap.get(PARAM_CASE_INSENSITIVE_USERNAME));
this.caseInsensitiveUsername = parseBoolean(
this.parameterMap.getOrDefault(PARAM_CASE_INSENSITIVE_USERNAME, "true"));

if (reload())
logger.info("Privilege Data loaded.");
Expand Down Expand Up @@ -353,4 +358,8 @@ public boolean persist() throws XMLStreamException, IOException {
logger.warn("Persist took {}", formatNanoDuration(tookNanos));
return saved;
}

protected String evaluateUsername(String username) {
return this.caseInsensitiveUsername ? username.toLowerCase() : username;
}
}

0 comments on commit caeb904

Please sign in to comment.