1 | /* Copyright 2006-2009 the original author or authors. |
---|
2 | * |
---|
3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | * you may not use this file except in compliance with the License. |
---|
5 | * You may obtain a copy of the License at |
---|
6 | * |
---|
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | * |
---|
9 | * Unless required by applicable law or agreed to in writing, software |
---|
10 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | * See the License for the specific language governing permissions and |
---|
13 | * limitations under the License. |
---|
14 | */ |
---|
15 | package org.grails.plugins.springsecurity.taglib |
---|
16 | |
---|
17 | import org.springframework.security.context.SecurityContextHolder as SCH |
---|
18 | |
---|
19 | import org.codehaus.groovy.grails.plugins.springsecurity.AuthorizeTools |
---|
20 | |
---|
21 | /** |
---|
22 | * Authorize Taglibs. |
---|
23 | * Rewritten in Groovy from Java source of org.acegisecurity.taglibs.authz.AuthorizeTag. |
---|
24 | * |
---|
25 | * @author T.Yamamoto |
---|
26 | */ |
---|
27 | class AuthorizeTagLib { |
---|
28 | |
---|
29 | /** |
---|
30 | * <g:ifAllGranted role="ROLE_USER,ROLE_ADMIN,ROLE_SUPERVISOR"> |
---|
31 | * All the listed roles must be granted for the tag to output its body. |
---|
32 | * </g:ifAllGranted> |
---|
33 | */ |
---|
34 | def ifAllGranted = { attrs, body -> |
---|
35 | if (AuthorizeTools.ifAllGranted(attrs.role)) { |
---|
36 | out << body() |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * <g:ifNotGranted role="ROLE_USER,ROLE_ADMIN,ROLE_SUPERVISOR"> |
---|
42 | * None of the listed roles must be granted for the tag to output its body. |
---|
43 | * </g:ifNotGranted> |
---|
44 | */ |
---|
45 | def ifNotGranted = { attrs, body -> |
---|
46 | if (AuthorizeTools.ifNotGranted(attrs.role)) { |
---|
47 | out << body() |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * <g:ifAnyGranted role="ROLE_USER,ROLE_ADMIN,ROLE_SUPERVISOR"> |
---|
53 | * Any of the listed roles must be granted for the tag to output its body. |
---|
54 | * </g:ifAnyGranted> |
---|
55 | */ |
---|
56 | def ifAnyGranted = { attrs, body -> |
---|
57 | if (AuthorizeTools.ifAnyGranted(attrs.role)) { |
---|
58 | out << body() |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * <g:loggedInUserInfo field="userRealName">Guest User</g:loggedInUserInfo> |
---|
64 | */ |
---|
65 | def loggedInUserInfo = { attrs, body -> |
---|
66 | if (isAuthenticated()) { |
---|
67 | def source = determineSource() |
---|
68 | out << source."$attrs.field" |
---|
69 | } |
---|
70 | else { |
---|
71 | out << body() |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | private def determineSource() { |
---|
76 | def principal = SCH.context.authentication.principal |
---|
77 | def source |
---|
78 | |
---|
79 | // check to see if it's a GrailsUser/GrailsUserImpl/subclass, |
---|
80 | // or otherwise has a 'domainClass' property |
---|
81 | if (principal.metaClass.respondsTo(principal, 'getDomainClass')) { |
---|
82 | source = principal.domainClass |
---|
83 | } |
---|
84 | if (!source) { |
---|
85 | source = principal |
---|
86 | } |
---|
87 | |
---|
88 | return source |
---|
89 | } |
---|
90 | |
---|
91 | def isLoggedIn = { attrs, body -> |
---|
92 | if (isAuthenticated()) { |
---|
93 | out << body() |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | def isNotLoggedIn = {attrs, body -> |
---|
98 | if (!isAuthenticated()) { |
---|
99 | out << body() |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | def loggedInUsername = { attrs -> |
---|
104 | if (isAuthenticated()) { |
---|
105 | out << SCH.context.authentication.principal.username |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | private boolean isAuthenticated() { |
---|
110 | def authPrincipal = SCH?.context?.authentication?.principal |
---|
111 | return authPrincipal != null && authPrincipal != 'anonymousUser' |
---|
112 | } |
---|
113 | } |
---|