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