From 6f4a2aa3d4096556fba9dac2d49a8a8f3b9954ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20M=C3=A4kinen?= Date: Fri, 15 Sep 2023 15:40:23 +0300 Subject: [PATCH] Revert "User created and last login" --- ...3_3_2__migrate_created_and_last_login.java | 96 ------------------- ...OskariPreAuthenticationSuccessHandler.java | 16 +--- 2 files changed, 4 insertions(+), 108 deletions(-) delete mode 100644 app-resources/src/main/java/flyway/pti/V3_3_2__migrate_created_and_last_login.java diff --git a/app-resources/src/main/java/flyway/pti/V3_3_2__migrate_created_and_last_login.java b/app-resources/src/main/java/flyway/pti/V3_3_2__migrate_created_and_last_login.java deleted file mode 100644 index 5767a5f0..00000000 --- a/app-resources/src/main/java/flyway/pti/V3_3_2__migrate_created_and_last_login.java +++ /dev/null @@ -1,96 +0,0 @@ -package flyway.pti; - -import fi.nls.oskari.log.LogFactory; -import fi.nls.oskari.log.Logger; -import fi.nls.oskari.util.JSONHelper; -import org.flywaydb.core.api.migration.BaseJavaMigration; -import org.flywaydb.core.api.migration.Context; -import org.json.JSONObject; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.time.LocalDateTime; -import java.time.OffsetDateTime; -import java.time.ZoneOffset; -import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.List; - -public class V3_3_2__migrate_created_and_last_login extends BaseJavaMigration { - class User { - Long id; - String attributes; - String userName; - OffsetDateTime created; - OffsetDateTime lastLogin; - } - - private Logger log = LogFactory.getLogger(V3_3_2__migrate_created_and_last_login.class); - - @Override - public void migrate(Context context) throws Exception { - Connection connection = context.getConnection(); - List users = getUsers(connection); - users.forEach(user -> { - JSONObject attributes = JSONHelper.createJSONObject(user.attributes); - try { - String created = attributes.getString("created"); - String lastLogin = attributes.getString("lastLogin"); - attributes.remove("created"); - attributes.remove("lastLogin"); - user.attributes = attributes.toString(); - user.created = this.formatDateTime(created); - user.lastLogin = this.formatDateTime(lastLogin); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); - - saveChanges(connection, users); - } - - public List getUsers(Connection conn) throws Exception { - List users = new ArrayList<>(); - final String sql = "SELECT id, user_name, attributes FROM oskari_users;"; - try(PreparedStatement statement = conn.prepareStatement(sql)) { - try (ResultSet rs = statement.executeQuery()) { - while(rs.next()) { - User user = new User(); - user.id = rs.getLong("id"); - user.attributes = rs.getString("attributes"); - users.add(user); - } - } - } - return users; - } - - public OffsetDateTime formatDateTime(String stringDate) { - if (stringDate == null || stringDate.equals("null")) { - return null; - } - - String format = stringDate.indexOf('T') > -1 ? "yyyy-MM-dd'T'HH:mm:ss" : "yyyy-MM-dd HH:mm:ss.SSS"; - OffsetDateTime offsetDateTime = - OffsetDateTime.of(LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern(format)), ZoneOffset.UTC); - - return offsetDateTime; - } - - private void saveChanges(Connection conn, List users) throws SQLException { -// final String sql = "UPDATE oskari_users_migration_test SET attributes = ?, created = ? , last_login = ? WHERE id = ?"; - final String sql = "UPDATE oskari_users SET attributes = ?, created = ? , last_login = ? WHERE id = ?"; - try (PreparedStatement ps = conn.prepareStatement(sql)) { - for (User user : users) { - ps.setString(1, user.attributes); - ps.setObject(2, user.created); - ps.setObject(3, user.lastLogin); - ps.setLong(4, user.id); - ps.addBatch(); - } - ps.executeBatch(); - } - } -} diff --git a/server-extension/src/main/java/fi/nls/oskari/spring/security/preauth/OskariPreAuthenticationSuccessHandler.java b/server-extension/src/main/java/fi/nls/oskari/spring/security/preauth/OskariPreAuthenticationSuccessHandler.java index a9f66529..38be823e 100755 --- a/server-extension/src/main/java/fi/nls/oskari/spring/security/preauth/OskariPreAuthenticationSuccessHandler.java +++ b/server-extension/src/main/java/fi/nls/oskari/spring/security/preauth/OskariPreAuthenticationSuccessHandler.java @@ -18,7 +18,6 @@ import javax.servlet.http.HttpSession; import java.io.IOException; import java.text.SimpleDateFormat; -import java.time.OffsetDateTime; import java.util.Date; public class OskariPreAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { @@ -64,9 +63,12 @@ public void onAuthenticationSuccess(HttpServletRequest request, private User getUser(OskariUserDetails oud) throws ServiceException { User user = userService.getUserByEmail(oud.getUser().getEmail()); + // sdf is not threadsafe so create new for each login + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); if(user == null) { user = oud.getUser(); user.addRole(Role.getDefaultUserRole()); + user.setAttribute("created", format.format(new Date())); } else { // copy data that we got from headers user.setFirstname(oud.getUser().getFirstname()); @@ -75,6 +77,7 @@ private User getUser(OskariUserDetails oud) throws ServiceException { // merge attributes JSONHelper.merge(user.getAttributesJSON(), oud.getUser().getAttributesJSON()); } + user.setAttribute("lastLogin", format.format(new Date())); return user; } @@ -87,17 +90,6 @@ protected void setupSession(User authenticatedUser, HttpServletRequest request) AuditLog.user(ActionParameters.getClientIp(request), authenticatedUser.getEmail()) .withMsg("Login") .updated(AuditLog.ResourceType.USER); - - // update last login - try { - User userToUpdate = userService.getUser(authenticatedUser.getId()); - userToUpdate.setLastLogin(OffsetDateTime.now()); - userService.modifyUser(userToUpdate); - } catch (Exception e) { - - } - - } protected DatabaseUserService getUserService() {