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