source: trunk/grails-app/controllers/dbnp/authentication/LoginController.groovy @ 1430

Last change on this file since 1430 was 1430, checked in by work@…, 12 years ago
  • set keyword expansion
  • Property svn:keywords set to Rev Author Date
File size: 5.6 KB
Line 
1package dbnp.authentication
2
3import grails.converters.JSON
4
5import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
6
7import org.springframework.security.authentication.AccountExpiredException
8import org.springframework.security.authentication.CredentialsExpiredException
9import org.springframework.security.authentication.DisabledException
10import org.springframework.security.authentication.LockedException
11import org.springframework.security.core.context.SecurityContextHolder as SCH
12import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter
13import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
14
15class LoginController {
16
17        /**
18         * Dependency injection for the authenticationTrustResolver.
19         */
20        def authenticationTrustResolver
21
22        /**
23         * Dependency injection for the springSecurityService.
24         */
25        def springSecurityService
26
27        /**
28         * Dependency injection for the GSCF authentication service
29         */
30        def AuthenticationService
31
32        /**
33         * Default action; redirects to 'defaultTargetUrl' if logged in, /login/auth otherwise.
34         */
35        def index = {
36                if (springSecurityService.isLoggedIn()) {
37                        redirect uri: SpringSecurityUtils.securityConfig.successHandler.defaultTargetUrl
38                }
39                else {
40                        redirect action: auth, params: params
41                }
42        }
43
44        /**
45         * Show the login page.
46         */
47        def auth = {
48
49                def config = SpringSecurityUtils.securityConfig
50
51                if (springSecurityService.isLoggedIn()) {
52                        redirect uri: config.successHandler.defaultTargetUrl
53                        return
54                }
55
56                String view = 'auth'
57                String postUrl = "${request.contextPath}${config.apf.filterProcessesUrl}"
58                render view: view, model: [postUrl: postUrl,
59                                           rememberMeParameter: config.rememberMe.parameter]
60        }
61
62        /**
63         * Shows the login page for users from a module
64         */
65        def auth_remote = {
66            def consumer    = params.consumer
67            def token       = params.token
68
69                        if( consumer == null || token == null ) {
70                                throw new Exception( "Consumer and Token must be given!" );
71                        }
72
73            def returnUrl   = params.returnUrl
74
75            // If the user is already authenticated with this session_id, redirect
76            // him
77            if( AuthenticationService.isRemotelyLoggedIn( consumer, token ) ) {
78                if( returnUrl ) {
79                                        redirect url: returnUrl
80                } else {
81                    redirect controller: 'home'
82                }
83            }
84
85            // If the user is already logged in locally, we log him in and
86            // immediately redirect him
87            if (AuthenticationService.isLoggedIn()) {
88                                AuthenticationService.logInRemotely( consumer, token, AuthenticationService.getLoggedInUser() )
89
90                                if( returnUrl ) {
91                    redirect url: returnUrl
92                } else {
93                    redirect controller: 'home'
94                }
95            }
96
97            // Otherwise we show the login screen
98                        def config = SpringSecurityUtils.securityConfig
99            String view = 'auth'
100            String postUrl = "${request.contextPath}${config.apf.filterProcessesUrl}"
101            String redirectUrl = g.createLink( controller: 'login', action: 'auth_remote', params: [ consumer: params.consumer, token: params.token, returnUrl: params.returnUrl ], absolute: true )
102            render view: view, model: [postUrl: postUrl,
103                                       rememberMeParameter: config.rememberMe.parameter, redirectUrl: redirectUrl ]
104        }
105       
106        /**
107         * Show denied page.
108         */
109        def denied = {
110                if (springSecurityService.isLoggedIn() &&
111                                authenticationTrustResolver.isRememberMe(SCH.context?.authentication)) {
112                        // have cookie but the page is guarded with IS_AUTHENTICATED_FULLY
113                        redirect action: full, params: params
114                }
115        }
116
117        /**
118         * Login page for users with a remember-me cookie but accessing a IS_AUTHENTICATED_FULLY page.
119         */
120        def full = {
121                def config = SpringSecurityUtils.securityConfig
122                render view: 'auth', params: params,
123                        model: [hasCookie: authenticationTrustResolver.isRememberMe(SCH.context?.authentication),
124                                postUrl: "${request.contextPath}${config.apf.filterProcessesUrl}"]
125        }
126
127        /**
128         * Callback after a failed login. Redirects to the auth page with a warning message.
129         */
130        def authfail = {
131
132                def username = session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY]
133                String msg = ''
134                def exception = session[AbstractAuthenticationProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY]
135                if (exception) {
136                        if (exception instanceof AccountExpiredException) {
137                                msg = SpringSecurityUtils.securityConfig.errors.login.expired
138                        }
139                        else if (exception instanceof CredentialsExpiredException) {
140                                msg = SpringSecurityUtils.securityConfig.errors.login.passwordExpired
141                        }
142                        else if (exception instanceof DisabledException) {
143                                msg = SpringSecurityUtils.securityConfig.errors.login.disabled
144                        }
145                        else if (exception instanceof LockedException) {
146                                msg = SpringSecurityUtils.securityConfig.errors.login.locked
147                        }
148                        else {
149                                msg = SpringSecurityUtils.securityConfig.errors.login.fail
150                        }
151                }
152
153                if (springSecurityService.isAjax(request)) {
154                        render([error: msg] as JSON)
155                }
156                else {
157                        flash.message = msg
158                        redirect action: auth, params: params
159                }
160        }
161
162        /**
163         * The Ajax success redirect url.
164         */
165        def ajaxSuccess = {
166                render([success: true, username: springSecurityService.authentication.name] as JSON)
167        }
168
169        /**
170         * The Ajax denied redirect url.
171         */
172        def ajaxDenied = {
173                render([error: 'access denied'] as JSON)
174        }
175}
Note: See TracBrowser for help on using the repository browser.