source: branches/TaskRewrite/src/grails-app/controllers/LoginController.groovy @ 58

Last change on this file since 58 was 58, checked in by gav, 15 years ago

Configure BootStrap? with latest concepts.
Install and setup Acegi plugin with custom views.
Test Fixture plugin in a test app but couldn't get it to work with Acegi encodePassword() so gave up.

File size: 4.4 KB
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.RedirectUtils
2import org.grails.plugins.springsecurity.service.AuthenticateService
3
4import org.springframework.security.AuthenticationTrustResolverImpl
5import org.springframework.security.DisabledException
6import org.springframework.security.context.SecurityContextHolder as SCH
7import org.springframework.security.ui.AbstractProcessingFilter
8import org.springframework.security.ui.webapp.AuthenticationProcessingFilter
9
10/**
11 * Login Controller (Example).
12 */
13class LoginController {
14
15        /**
16         * Dependency injection for the authentication service.
17         */
18        def authenticateService
19
20        /**
21         * Dependency injection for OpenIDConsumer.
22         */
23        def openIDConsumer
24
25        /**
26         * Dependency injection for OpenIDAuthenticationProcessingFilter.
27         */
28        def openIDAuthenticationProcessingFilter
29
30        private final authenticationTrustResolver = new AuthenticationTrustResolverImpl()
31
32        def index = {
33                if (isLoggedIn()) {
34                        redirect uri: '/'
35                }
36                else {
37                        redirect action: auth, params: params
38                }
39        }
40
41        /**
42         * Show the login page.
43         */
44        def auth = {
45
46                nocache response
47
48                if (isLoggedIn()) {
49                        redirect uri: '/'
50                        return
51                }
52
53                String view
54                String postUrl
55                def config = authenticateService.securityConfig.security
56                if (config.useOpenId) {
57                        view = 'openIdAuth'
58                        postUrl = "${request.contextPath}/login/openIdAuthenticate"
59                }
60                else if (config.useFacebook) {
61                        view = 'facebookAuth'
62                        postUrl = "${request.contextPath}${config.facebook.filterProcessesUrl}"
63                }
64                else {
65                        view = 'auth'
66                        postUrl = "${request.contextPath}${config.filterProcessesUrl}"
67                }
68
69                render view: view, model: [postUrl: postUrl]
70        }
71
72        /**
73         * Form submit action to start an OpenID authentication.
74         */
75        def openIdAuthenticate = {
76                String openID = params['j_username']
77                try {
78                        String returnToURL = RedirectUtils.buildRedirectUrl(
79                                        request, response, openIDAuthenticationProcessingFilter.filterProcessesUrl)
80                        String redirectUrl = openIDConsumer.beginConsumption(request, openID, returnToURL)
81                        redirect url: redirectUrl
82                }
83                catch (org.springframework.security.ui.openid.OpenIDConsumerException e) {
84                        log.error "Consumer error: $e.message", e
85                        redirect url: openIDAuthenticationProcessingFilter.authenticationFailureUrl
86                }
87        }
88
89        // Login page (function|json) for Ajax access.
90        def authAjax = {
91                nocache(response)
92                //this is example:
93                render """
94                <script type='text/javascript'>
95                (function() {
96                        loginForm();
97                })();
98                </script>
99                """
100        }
101
102        /**
103         * The Ajax success redirect url.
104         */
105        def ajaxSuccess = {
106                nocache(response)
107                render '{success: true}'
108        }
109
110        /**
111         * Show denied page.
112         */
113        def denied = {
114                if (isLoggedIn() && authenticationTrustResolver.isRememberMe(SCH.context?.authentication)) {
115                        // have cookie but the page is guarded with IS_AUTHENTICATED_FULLY
116                        redirect action: full, params: params
117                }
118        }
119
120        /**
121         * Login page for users with a remember-me cookie but accessing a IS_AUTHENTICATED_FULLY page.
122         */
123        def full = {
124                render view: 'auth', params: params,
125                        model: [hasCookie: authenticationTrustResolver.isRememberMe(SCH.context?.authentication)]
126        }
127
128        // Denial page (data|view|json) for Ajax access.
129        def deniedAjax = {
130                //this is example:
131                render "{error: 'access denied'}"
132        }
133
134        /**
135         * login failed
136         */
137        def authfail = {
138
139                def username = session[AuthenticationProcessingFilter.SPRING_SECURITY_LAST_USERNAME_KEY]
140                def msg = ''
141                def exception = session[AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY]
142                if (exception) {
143                        if (exception instanceof DisabledException) {
144                                msg = "[$username] is disabled."
145                        }
146                        else {
147                                msg = "[$username] wrong username/password."
148                        }
149                }
150
151                if (isAjax()) {
152                        render "{error: '${msg}'}"
153                }
154                else {
155                        flash.message = msg
156                        redirect action: auth, params: params
157                }
158        }
159
160        /**
161         * Check if logged in.
162         */
163        private boolean isLoggedIn() {
164                return authenticateService.isLoggedIn()
165        }
166
167        private boolean isAjax() {
168                return authenticateService.isAjax(request)
169        }
170
171        /** cache controls */
172        private void nocache(response) {
173                response.setHeader('Cache-Control', 'no-cache') // HTTP 1.1
174                response.addDateHeader('Expires', 0)
175                response.setDateHeader('max-age', 0)
176                response.setIntHeader ('Expires', -1) //prevents caching at the proxy server
177                response.addHeader('cache-Control', 'private') //IE5.x only
178        }
179}
Note: See TracBrowser for help on using the repository browser.