root/trunk/grails-app/services/dbnp/authentication/AuthenticationService.groovy @ 1455

Revision 1455, 4.3 KB (checked in by s.h.sikkema@…, 3 years ago)

Using moduleCommunicationService now to fetch measurement data from module

  • Property svn:keywords set to Rev Author Date
Line 
1/**
2 * AuthenticationService
3 *
4 * Is used for keeping track of the logged in user
5 *
6 * @author      robert@isdat.nl (Robert Horlings
7 * @since       20101021
8 * @package     dbnp.authentication
9 *
10 * Revision information:
11 * $Rev$
12 * $Author$
13 * $Date$
14 */
15package dbnp.authentication
16
17import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser
18
19class AuthenticationService {
20    def SpringSecurityService
21    static final int expiryTime = 60; // Number of minutes a remotely logged in user remains active
22
23    boolean transactional = true
24
25    public boolean isLoggedIn() {
26        return SpringSecurityService.isLoggedIn();
27    }
28
29    public SecUser getLoggedInUser() {
30      def principal = SpringSecurityService.getPrincipal()
31
32      // If the user is logged in, the principal should be a GrailsUser object.
33      // If the user is not logged in, the principal is the 'anonymous username'
34      // i.e. a string
35      if( principal instanceof GrailsUser ) {
36          return SecUser.findByUsername( principal.username );
37      }
38
39      return null;
40    }
41
42    /**
43     * Logs a user in for a remote session
44     */
45    public boolean logInRemotely( String consumer, String token, SecUser user ) {
46        // Make sure there is no other logged in user anymore
47        logOffRemotely( consumer, token )
48
49        def SAUser = new SessionAuthenticatedUser( consumer: consumer, token: token, secUser: user, expiryDate: createExpiryDate() )
50
51        return SAUser.save(flush: true)
52    }
53   
54    public boolean logOffRemotely( String consumer, String token ) {
55        def user = getSessionAuthenticatedUser(consumer, token)
56
57        if( user ) {
58            user.refresh()
59            user.delete()
60        }
61       
62        return true
63    }
64
65    /**
66     * Checks whether a user is logged in from a remote consumer with the
67     * given token
68     */
69    public boolean isRemotelyLoggedIn( String consumer, String token ) {
70        // Remove expired users, otherwise they will be kept in the database forever
71        removeExpiredTokens()
72
73        // Check whether a user exists
74        def user = getSessionAuthenticatedUser(consumer, token)
75
76        // Check whether the user is logged in. Since we don't want to return a
77        // user, we explicitly return true or false
78        if( user ) {
79                        // The expiry date should be reset
80                        updateExpiryDate( user )
81
82            return true
83                } else {
84            return false
85                }
86    }
87
88    /**
89     * Returns the user that is logged in remotely
90     */
91    public SecUser getRemotelyLoggedInUser( String consumer, String token ) {
92        // Remove expired users, otherwise they will be kept in the database forever
93        removeExpiredTokens()
94
95        // Check whether a user exists
96        def user = getSessionAuthenticatedUser(consumer, token)
97
98        return user ? user.secUser : null
99    }
100
101    /**
102     * Removes all tokens for remote logins that have expired
103     */
104    protected boolean removeExpiredTokens() {
105        SessionAuthenticatedUser.executeUpdate("delete SessionAuthenticatedUser u where u.expiryDate < :expiryDate", [ expiryDate: new Date() ])
106    }
107
108    /**
109         * Returns the currently logged in user from the database or null if no user is logged in
110         */
111        protected SessionAuthenticatedUser getSessionAuthenticatedUser( String consumer, String token ) {
112        def c = SessionAuthenticatedUser.createCriteria()
113        def result = c.get {
114                and {
115                        eq( "consumer", consumer)
116                        eq( "token", token)
117                        gt( "expiryDate", new Date())
118                }
119        }
120
121        if( result )
122            return result
123        else
124            return null
125    }
126
127        /**
128         * Returns the expiry date for a user that is active now.
129         */
130        protected Date createExpiryDate() {
131                // Compute expiryDate
132                long now = new Date().getTime();
133                return new Date( now + AuthenticationService.expiryTime * 60 * 1000 );
134
135        }
136
137        /**
138         * Resets the expiry date of the given user. This should be called every time
139         * an action occurs with this user. That way, if (in case of a timeout of 60 minutes)
140         * he logs in and returns 50 minutes later, he will keep a timeout value of
141         * 60 minutes, instead of only 10 minutes.
142         */
143        protected boolean updateExpiryDate( SessionAuthenticatedUser user ) {
144                user.expiryDate = createExpiryDate()
145                return user.save( flush: true )
146        }
147}
Note: See TracBrowser for help on using the browser.