Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User created and last login timestamps #1000

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE oskari_users ADD COLUMN created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW();
ALTER TABLE oskari_users ADD COLUMN last_login TIMESTAMP WITH TIME ZONE DEFAULT null;
23 changes: 23 additions & 0 deletions service-base/src/main/java/fi/nls/oskari/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.json.JSONObject;

import java.io.Serializable;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -27,13 +28,17 @@ public class User implements Serializable {
private static final String KEY_USERID = "userID";
private final static String KEY_ROLES = "roles";
private final static String KEY_ADMIN = "admin";
private final static String KEY_CREATED = "created";
private final static String KEY_LAST_LOGIN = "lastLogin";

private long id = -1;
private String lastname = "";
private String firstname = "";
private String screenname = "";
private String email = "";
private JSONObject attributes = new JSONObject();
private OffsetDateTime created;
private OffsetDateTime lastLogin;

private String uuid = "";
private Set<Role> roles = new LinkedHashSet<>();
Expand Down Expand Up @@ -210,6 +215,22 @@ public String getFirstname() {
return firstname;
}

public OffsetDateTime getCreated() {
return created;
}

public void setCreated(OffsetDateTime created) {
this.created = created;
}

public OffsetDateTime getLastLogin() {
return lastLogin;
}

public void setLastLogin(OffsetDateTime lastLogin) {
this.lastLogin = lastLogin;
}

public JSONObject toJSON() {
try {
JSONObject userData = new JSONObject();
Expand All @@ -219,6 +240,8 @@ public JSONObject toJSON() {
userData.put(KEY_NICKNAME, getScreenname());
userData.put(KEY_USERUUID, getUuid());
userData.put(KEY_USERID, getId());
userData.put(KEY_CREATED, getCreated());
userData.put(KEY_LAST_LOGIN, getLastLogin());
if (isAdmin()){
userData.put(KEY_ADMIN, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.security.crypto.bcrypt.BCrypt;

import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;



public class DatabaseUserService extends UserService {
Expand Down
17 changes: 10 additions & 7 deletions service-users/src/main/java/fi/nls/oskari/user/UsersMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ public interface UsersMapper {
@Result(property="email", column="email"),
@Result(property="uuid", column="uuid"),
@Result(property="screenname", column="user_name"),
@Result(property="attributes", column="attributes")
@Result(property="attributes", column="attributes"),
@Result(property="created", column="created"),
@Result(property="lastLogin", column="last_login")
})
@Select("SELECT id, first_name, last_name, user_name, email, uuid, attributes" +
@Select("SELECT id, first_name, last_name, user_name, email, uuid, attributes, created, last_login" +
" FROM oskari_users" +
" ORDER BY user_name")
List<User> findAll();

@ResultMap("UsersResult")
@Select("SELECT id, first_name, last_name, user_name, email, uuid, attributes" +
@Select("SELECT id, first_name, last_name, user_name, email, uuid, attributes, created, last_login" +
" FROM oskari_users" +
" ORDER BY user_name" +
" LIMIT #{limit} OFFSET #{offset}")
List<User> findAllPaginated(@Param("limit") int limit, @Param("offset") int offset);

@ResultMap("UsersResult")
@Select("SELECT id, first_name, last_name, user_name, email, uuid, attributes" +
@Select("SELECT id, first_name, last_name, user_name, email, uuid, attributes, created, last_login" +
" FROM oskari_users" +
" WHERE " +
" user_name ilike '%' || #{query} || '%'" +
Expand Down Expand Up @@ -74,12 +76,13 @@ public interface UsersMapper {
" last_name = #{lastname}, " +
" user_name = #{screenname}, " +
" email = #{email}, " +
" attributes = #{attributes} " +
" attributes = #{attributes}, " +
" last_login = #{lastLogin} " +
" WHERE id = #{id}")
void updateUser(User user);

@ResultMap("UsersResult")
@Select("SELECT id, first_name, last_name, user_name, email, uuid, attributes" +
@Select("SELECT id, first_name, last_name, user_name, email, uuid, attributes, created, last_login" +
" FROM oskari_users" +
" WHERE id = #{id}")
User find(long id);
Expand All @@ -95,7 +98,7 @@ public interface UsersMapper {
String getPassword(final String username);

@ResultMap("UsersResult")
@Select("SELECT id, first_name, last_name, user_name, email, uuid, attributes" +
@Select("SELECT id, first_name, last_name, user_name, email, uuid, attributes, created, last_login" +
" FROM oskari_users" +
" WHERE user_name = #{username}")
User findByUserName(String username);
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
Loading