]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
363365e2a0371e2a64a94974b131b3328175cc5f
[tmp/jakarta-migration.git] /
1 package org.collectionspace.authentication;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7
8 import javax.security.auth.login.AccountException;
9 import javax.security.auth.login.AccountNotFoundException;
10
11 import org.collectionspace.authentication.realm.db.CSpaceDbRealm;
12 import org.postgresql.util.PSQLState;
13 import org.springframework.context.ApplicationListener;
14 import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
15 import org.springframework.security.core.Authentication;
16
17 public class CSpaceAuthenticationSuccessEvent implements ApplicationListener<AuthenticationSuccessEvent> {
18
19         private static final String UPDATE_USER_SQL =
20                         "UPDATE users SET lastlogin = now() WHERE username = ?";
21
22         @Override
23         public void onApplicationEvent(AuthenticationSuccessEvent event) {
24                 if (event.getSource() instanceof Authentication) {
25                         Authentication eventSource = (Authentication) event.getSource();
26
27                         if (eventSource.getPrincipal() instanceof CSpaceUser) {
28                                 CSpaceDbRealm cspaceDbRealm = new CSpaceDbRealm();
29                                 CSpaceUser cspaceUser = (CSpaceUser) eventSource.getPrincipal();
30                                 String username = cspaceUser.getUsername();
31
32                                 try {
33                                         setLastLogin(cspaceDbRealm, username);
34                                 } catch (Exception e) {
35                                         e.printStackTrace();
36                                 }
37                         }
38                 }
39         }
40
41         private void setLastLogin(CSpaceDbRealm cspaceDbRealm, String username) throws AccountException {
42         Connection conn = null;
43         PreparedStatement ps = null;
44         ResultSet rs = null;
45
46         try {
47             conn = cspaceDbRealm.getConnection();
48             ps = conn.prepareStatement(UPDATE_USER_SQL);
49             ps.setString(1, username);
50             int affected = ps.executeUpdate();
51             if (affected < 1) {
52                 String errMsg = String.format("No matching username '%s' found.", username);
53                 throw new AccountException(errMsg);
54             }
55         } catch (SQLException ex) {
56                 // Assuming PostgreSQL
57             if (PSQLState.UNDEFINED_COLUMN.getState().equals(ex.getSQLState())) {
58                 System.err.println("'users' table is missing 'lastlogin' column.");
59             } else {
60                 AccountException ae = new AccountException("Authentication query failed: " + ex.getLocalizedMessage());
61                 ae.initCause(ex);
62                 throw ae;
63             }
64         } catch (AccountNotFoundException ex) {
65             throw ex;
66         } catch (Exception ex) {
67             AccountException ae = new AccountException("Unknown Exception");
68             ae.initCause(ex);
69             throw ae;
70         } finally {
71             if (rs != null) {
72                 try {
73                     rs.close();
74                 } catch (SQLException e) {
75                 }
76             }
77             if (ps != null) {
78                 try {
79                     ps.close();
80                 } catch (SQLException e) {
81                 }
82             }
83             if (conn != null) {
84                 try {
85                     conn.close();
86                 } catch (SQLException ex) {
87                 }
88             }
89         }
90     }
91 }