1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.taglibs.velocity;
17
18 import org.acegisecurity.acl.AclManager;
19
20 import org.acegisecurity.taglibs.authz.AclTag;
21 import org.acegisecurity.taglibs.authz.AuthenticationTag;
22 import org.acegisecurity.taglibs.authz.AuthorizeTag;
23
24 import org.springframework.context.ApplicationContext;
25
26 import javax.servlet.jsp.JspException;
27 import javax.servlet.jsp.PageContext;
28 import javax.servlet.jsp.tagext.Tag;
29
30
31
32
33
34
35
36 public class AuthzImpl implements Authz {
37
38
39 static final int ALL_GRANTED = 1;
40 static final int ANY_GRANTED = 2;
41 static final int NONE_GRANTED = 3;
42
43
44
45 private ApplicationContext appCtx;
46
47
48
49 public boolean allGranted(String roles) {
50 return ifGranted(roles, ALL_GRANTED);
51 }
52
53 public boolean anyGranted(String roles) {
54 return ifGranted(roles, ANY_GRANTED);
55 }
56
57 public ApplicationContext getAppCtx() {
58 return appCtx;
59 }
60
61
62
63
64
65
66
67
68 public String getPrincipal() {
69 MyAuthenticationTag authenticationTag = new MyAuthenticationTag();
70
71 authenticationTag.setOperation("username");
72
73 try {
74 authenticationTag.doStartTag();
75 } catch (JspException je) {
76 je.printStackTrace();
77 throw new IllegalArgumentException(je.getMessage());
78 }
79
80 return authenticationTag.getLastMessage();
81 }
82
83
84
85
86
87
88
89
90
91
92
93 public boolean hasPermission(Object domainObject, String permissions) {
94 MyAclTag aclTag = new MyAclTag();
95 aclTag.setPageContext(null);
96 aclTag.setContext(getAppCtx());
97 aclTag.setDomainObject(domainObject);
98 aclTag.setHasPermission(permissions);
99
100 int result = -1;
101
102 try {
103 result = aclTag.doStartTag();
104 } catch (JspException je) {
105 throw new IllegalArgumentException(je.getMessage());
106 }
107
108 if (Tag.EVAL_BODY_INCLUDE == result) {
109 return true;
110 } else {
111 return false;
112 }
113 }
114
115
116
117
118
119
120
121
122
123
124
125 private boolean ifGranted(String roles, int grantType) {
126 AuthorizeTag authorizeTag = new AuthorizeTag();
127
128 int result = -1;
129
130 try {
131 switch (grantType) {
132 case ALL_GRANTED:
133 authorizeTag.setIfAllGranted(roles);
134
135 break;
136
137 case ANY_GRANTED:
138 authorizeTag.setIfAnyGranted(roles);
139
140 break;
141
142 case NONE_GRANTED:
143 authorizeTag.setIfNotGranted(roles);
144
145 break;
146
147 default:
148 throw new IllegalArgumentException("invalid granted type : " + grantType + " role=" + roles);
149 }
150
151 result = authorizeTag.doStartTag();
152 } catch (JspException je) {
153 throw new IllegalArgumentException(je.getMessage());
154 }
155
156 if (Tag.EVAL_BODY_INCLUDE == result) {
157 return true;
158 } else {
159 return false;
160 }
161 }
162
163 public boolean noneGranted(String roles) {
164 return ifGranted(roles, NONE_GRANTED);
165 }
166
167
168
169
170
171
172 public void setAppCtx(ApplicationContext appCtx) {
173 this.appCtx = appCtx;
174 }
175
176
177
178
179
180
181
182
183 private class MyAclTag extends AclTag {
184 private static final long serialVersionUID = 6752340622125924108L;
185 ApplicationContext context;
186
187 protected ApplicationContext getContext(PageContext pageContext) {
188 return context;
189 }
190
191 protected void setContext(ApplicationContext context) {
192 this.context = context;
193 }
194 }
195
196
197
198
199
200 private class MyAuthenticationTag extends AuthenticationTag {
201 private static final long serialVersionUID = -1094246833893599161L;
202 String lastMessage = null;
203
204 public String getLastMessage() {
205 return lastMessage;
206 }
207
208 protected void writeMessage(String msg) throws JspException {
209 lastMessage = msg;
210 }
211 }
212 }