source: trunk/grails-app/conf/BaseFilters.groovy @ 1939

Last change on this file since 1939 was 1939, checked in by robert@…, 12 years ago

Updated module notification so that it also sends authentication to modules.

  • Property svn:keywords set to Rev Author Date
File size: 3.5 KB
Line 
1/**
2 * Base Filters
3 * @Author Jeroen Wesbeek
4 * @Since 20091026
5 * @see main.gsp
6 * @see http://grails.org/Filters
7 * @Description
8 *
9 * These filters contain generic logic for -every- page request.
10 *
11 * Revision information:
12 * $Rev: 1939 $
13 * $Author: robert@isdat.nl $
14 * $Date: 2011-06-24 10:56:22 +0000 (vr, 24 jun 2011) $
15 */
16import org.codehaus.groovy.grails.commons.GrailsApplication
17
18class BaseFilters {
19        def authenticationService
20
21        // define filters
22        def filters = {
23                defineStyle(controller: '*', action: '*') {
24                        // before every execution
25                        before = {
26                                // set the default style in the session
27                                if (!session.style) {
28                                        def hostname = InetAddress.getLocalHost().getHostName()
29                                        if (hostname =~ 'nmcdsp.org') {
30                                                session.style = 'nmcdsp_style'
31                                        } else if (hostname =~ 'nbx') {
32                                                session.style = 'dbnp_style'
33                                        } else {
34                                                session.style = 'default_style'
35                                        }
36                                }
37
38                                // set session lifetime to 1 week
39                                session.setMaxInactiveInterval(604800)
40                        }
41                }
42               
43                // Save a reference to the logged in user in the session,
44                // in order to use it later on. This is needed, because webflows are not capable of retrieving
45                // the logged in user from the authenticationService, since that service (more specific: spring security)
46                // is not serializable.
47                saveUser(controller: '*', action: '*' ) {
48                        before = { 
49                                // set the secUser in the session
50                                def secUser = authenticationService.getLoggedInUser()
51                                if (secUser) {
52                                        session.gscfUser = secUser
53                                } else {
54                                        // remove session variable
55                                        if( session?.gscfUser )
56                                                session.removeAttribute('gscfUser')
57                                }
58                        }
59                }
60
61                // we need secUser in GDT::Template*, but we do not want GDT
62                // to rely on authentication. Therefore we handle it through
63                // a filter and store the loggedInUser in the session instead
64                templateEditor(controller: 'templateEditor', action: '*') {
65                        // before every execution
66                        before = {
67                                // set the secUser in the session
68                                def secUser = authenticationService.getLoggedInUser()
69                                if (secUser) {
70                                        session.loggedInUser = secUser
71                                } else {
72                                        // remove session variable
73                                        session.removeAttribute('loggedInUser')
74
75                                        def returnURI = request.requestURL.toString().replace(".dispatch","").replace("/grails/","/") + '?' + request.queryString
76
77                                        // and redirect to login page
78                                        redirect(controller: 'login', action: 'auth', params: [returnURI: returnURI, referer: request.getHeader('referer')] )
79                                }
80                        }
81                }
82
83                // disable all access to the query controller as this allows
84                // full access to the database
85                query(controller: 'query', action: '*') {
86                        // before every execution
87                        before = {
88                                // only allow development
89                                if (grails.util.GrailsUtil.environment != GrailsApplication.ENV_DEVELOPMENT) {
90                                        redirect(controller: 'home')
91                                }
92                        }
93                }
94               
95                profiler(controller: '*', action: '*') {
96                        before = {
97                                request._timeBeforeRequest = System.currentTimeMillis()
98                        }
99
100                        after = {
101                                request._timeAfterRequest = System.currentTimeMillis()
102                        }
103
104                        afterView = {
105                                def actionDuration = request._timeAfterRequest ? request._timeAfterRequest - request._timeBeforeRequest : 0
106                                def viewDuration = request._timeAfterRequest ? System.currentTimeMillis() - request._timeAfterRequest : 0
107                                log.info("Timer: ${controllerName}(${actionDuration}ms)::${actionName}(${viewDuration}ms)")
108                        }
109                }
110
111        // Mapping filter for the gdtImporter-plugin
112        gdtImporter(controller:'gdtImporter', action:'*') {
113             before = {
114                if(!authenticationService.getLoggedInUser()) {
115                      redirect(controller:'home')
116                    return false
117                }
118             }
119
120        }
121
122        }
123}
124
Note: See TracBrowser for help on using the repository browser.