Skip to content

Commit

Permalink
move updating the last login after the user has been updated to session
Browse files Browse the repository at this point in the history
  • Loading branch information
DenverCoder544 committed Sep 6, 2023
1 parent 2376396 commit c3f1898
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.security.crypto.bcrypt.BCrypt;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -67,16 +64,7 @@ public User login(final String user, final String pass) throws ServiceException
return null;
}

User fetchedUser = getUser(username);
OffsetDateTime previousLastLogin = fetchedUser.getLastLogin();
OffsetDateTime now = LocalDateTime.now().atOffset(ZoneOffset.UTC);
fetchedUser.setLastLogin(now);
userService.updateUser(fetchedUser);

// return the previous login to front
fetchedUser.setLastLogin(previousLastLogin);
return fetchedUser;

return getUser(username);
} catch (Exception ex) {
throw new ServiceException("Unable to handle login", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.service.UserService;
import fi.nls.oskari.user.MybatisUserService;
import org.oskari.log.AuditLog;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
Expand All @@ -15,6 +16,7 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
Expand All @@ -25,6 +27,7 @@
public class OskariUserHelper {

private static Logger log = LogFactory.getLogger(OskariUserHelper.class);
private MybatisUserService userService = new MybatisUserService();

/**
* Common code done for SAML and DB authentication on successful login
Expand Down Expand Up @@ -77,6 +80,11 @@ private void setupSession(final HttpServletRequest httpRequest, final String use
AuditLog.user(ActionParameters.getClientIp(httpRequest), loadedUser)
.withMsg("Login")
.updated(AuditLog.ResourceType.USER);

// update last login
User userToUpdate = UserService.getInstance().getUser(username);
userToUpdate.setLastLogin(OffsetDateTime.now());
userService.updateUser(userToUpdate);
}
else {
log.error("Login user check failed! Got user from principal, but can't find it in Oskari db:", username);
Expand Down

0 comments on commit c3f1898

Please sign in to comment.