Rev 1326 | Rev 1603 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1312 | jmachado | 1 | package pt.estgp.estgweb.domain; |
2 | |||
3 | import com.owlike.genson.Genson; |
||
4 | import com.owlike.genson.TransformationException; |
||
5 | import org.apache.log4j.Logger; |
||
6 | import org.json.JSONArray; |
||
7 | import org.json.JSONException; |
||
8 | import pt.estgp.estgweb.services.pageContent.PageContentAccessPolicyAcl; |
||
1326 | jmachado | 9 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; |
1312 | jmachado | 10 | |
11 | import java.io.IOException; |
||
12 | import java.util.ArrayList; |
||
13 | import java.util.HashMap; |
||
14 | import java.util.Map; |
||
15 | |||
16 | |||
17 | /** |
||
18 | * Created by jorgemachado on 03/02/16. |
||
19 | */ |
||
20 | public abstract class PageContentImpl extends PageContent implements ClearanceRuled |
||
21 | { |
||
22 | |||
23 | private static 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(PageContentImpl.class); |
||
24 | |||
25 | private UserSession sess = null; |
||
26 | private Map<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation,Boolean> userSessionClearences = null; |
||
27 | private boolean childsInitialized = false; |
||
28 | private boolean parentInitialized = false; |
||
29 | |||
30 | public static class 1.5.0/docs/api/java/security/acl/Acl.html">Acl |
||
31 | { |
||
32 | public static enum Access |
||
33 | { |
||
34 | PERMIT, |
||
35 | DENY //default |
||
36 | } |
||
37 | public static enum 1.5.0/docs/api/java/lang/reflect/Type.html">Type |
||
38 | { |
||
39 | ALL, |
||
40 | ANONYMOUS, //ONLY SUPER USER SHOULD ADD ANONYMOUS PERMISSIONS |
||
41 | AUTHENTICATED, |
||
42 | ROLE, |
||
43 | USER, |
||
44 | COURSEUNIT //SPECIFIC PERMISSIONS RELATIVE TO COURSE UNITS |
||
45 | } |
||
46 | public static enum 1.5.0/docs/api/java/rmi/server/Operation.html">Operation |
||
47 | { |
||
48 | ALL, //SPECIAL OPERATION FOR DENY AND PERMIT REPLACE FULL CONTROL |
||
49 | |||
50 | READ_CONTENTS, |
||
51 | |||
52 | READ_VERSIONS, |
||
53 | READ_INVISIBLES, |
||
54 | |||
55 | |||
56 | ORDER_CONTENTS, |
||
57 | |||
1561 | jmachado | 58 | ADD_SECTIONS, //ADD TEXT USA ESTA PERMISSAO |
1312 | jmachado | 59 | ADD_FILES, |
60 | ADD_URLS, |
||
61 | ADD_MODULES, |
||
62 | |||
63 | REMOVE_SECTIONS, |
||
64 | REMOVE_FILES, |
||
65 | REMOVE_URLS, |
||
66 | REMOVE_MODULES, |
||
67 | |||
68 | EDIT_SECTIONS, |
||
69 | EDIT_FILES, |
||
70 | EDIT_URLS, |
||
71 | EDIT_MODULES; |
||
72 | |||
73 | public static 1.5.0/docs/api/java/rmi/server/Operation.html">Operation parse(1.5.0/docs/api/java/lang/String.html">String clearance) |
||
74 | { |
||
75 | if(clearance == null) |
||
76 | return null; |
||
77 | for(1.5.0/docs/api/java/rmi/server/Operation.html">Operation op: 1.5.0/docs/api/java/rmi/server/Operation.html">Operation.values()) |
||
78 | { |
||
79 | if(op.name().equals(clearance)) |
||
80 | return op; |
||
81 | } |
||
82 | return null; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | public static enum CourseUnitsId |
||
87 | { |
||
88 | COURSE_UNIT_TEACHER("page.content.course.unit.teacher"), |
||
89 | COURSE_UNIT_STUDENT("page.content.course.unit.student"), |
||
90 | COURSE_UNIT_RESP_TEACHER("page.content.course.unit.resp.teacher"), |
||
91 | COURSE_UNIT_COURSE_COMISSION("page.content.course.unit.course.comission"); |
||
92 | |||
93 | 1.5.0/docs/api/java/lang/String.html">String msgKey; |
||
94 | |||
95 | |||
96 | CourseUnitsId(1.5.0/docs/api/java/lang/String.html">String msgKey) { |
||
97 | this.msgKey = msgKey; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | private 1.5.0/docs/api/java/lang/String.html">String access; |
||
102 | private 1.5.0/docs/api/java/lang/String.html">String type; |
||
103 | private 1.5.0/docs/api/java/lang/String.html">String id; |
||
104 | private 1.5.0/docs/api/java/lang/String.html">String operation; |
||
105 | |||
106 | public 1.5.0/docs/api/java/lang/String.html">String getAccess() { |
||
107 | return access; |
||
108 | } |
||
109 | |||
110 | public void setAccess(1.5.0/docs/api/java/lang/String.html">String access) { |
||
111 | this.access = access; |
||
112 | } |
||
113 | |||
114 | public void setAccessEnum(Access access) { |
||
115 | this.access = access.name(); |
||
116 | } |
||
117 | |||
118 | public 1.5.0/docs/api/java/lang/String.html">String getType() { |
||
119 | return type; |
||
120 | } |
||
121 | |||
122 | public void setType(1.5.0/docs/api/java/lang/String.html">String type) { |
||
123 | this.type = type; |
||
124 | } |
||
125 | public void setTypeEnum(1.5.0/docs/api/java/lang/reflect/Type.html">Type type) { |
||
126 | this.type = type.name(); |
||
127 | } |
||
128 | |||
129 | public 1.5.0/docs/api/java/lang/String.html">String getId() { |
||
130 | return id; |
||
131 | } |
||
132 | |||
133 | public void setId(1.5.0/docs/api/java/lang/String.html">String id) { |
||
134 | this.id = id; |
||
135 | } |
||
136 | |||
137 | public 1.5.0/docs/api/java/lang/String.html">String getOperation() { |
||
138 | return operation; |
||
139 | } |
||
140 | |||
141 | public void setOperation(1.5.0/docs/api/java/lang/String.html">String operation) { |
||
142 | this.operation = operation; |
||
143 | } |
||
144 | |||
145 | public void setOperationEnum(1.5.0/docs/api/java/rmi/server/Operation.html">Operation operation) { |
||
146 | this.operation = operation.name(); |
||
147 | } |
||
148 | |||
149 | public void setCourseUnitIdEnum(CourseUnitsId courseUnitIdEnum) { |
||
150 | this.id = courseUnitIdEnum.name(); |
||
151 | } |
||
152 | |||
153 | public Access getAccessEnum() |
||
154 | { |
||
155 | for(Access a : Access.values()) |
||
156 | { |
||
157 | if(a.name().equals(getAccess())) |
||
158 | return a; |
||
159 | } |
||
160 | return Access.DENY; |
||
161 | } |
||
162 | |||
163 | public 1.5.0/docs/api/java/lang/reflect/Type.html">Type getTypeEnum() |
||
164 | { |
||
165 | for(1.5.0/docs/api/java/lang/reflect/Type.html">Type a : 1.5.0/docs/api/java/lang/reflect/Type.html">Type.values()) |
||
166 | { |
||
167 | if(a.name().equals(getType())) |
||
168 | return a; |
||
169 | } |
||
170 | return 1.5.0/docs/api/java/lang/reflect/Type.html">Type.ROLE; |
||
171 | } |
||
172 | |||
173 | public 1.5.0/docs/api/java/rmi/server/Operation.html">Operation getOperationEnum() |
||
174 | { |
||
175 | for(1.5.0/docs/api/java/rmi/server/Operation.html">Operation a : 1.5.0/docs/api/java/rmi/server/Operation.html">Operation.values()) |
||
176 | { |
||
177 | if(a.name().equals(getOperation())) |
||
178 | return a; |
||
179 | } |
||
180 | return null; |
||
181 | } |
||
182 | |||
183 | public CourseUnitsId getCourseUnitIdEnum() |
||
184 | { |
||
185 | for(CourseUnitsId a : CourseUnitsId.values()) |
||
186 | { |
||
187 | if(a.name().equals(getId())) |
||
188 | return a; |
||
189 | } |
||
190 | return null; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Empty Array Means no ACL's defined |
||
196 | * build a json array form ACL's |
||
197 | * @return example [{"access":"PERMIT","operation":"FULL_CONTROL","id":"services","type":"ROLE"}] |
||
198 | * @throws JSONException |
||
199 | */ |
||
200 | public JSONArray getAclJson() throws JSONException |
||
201 | { |
||
202 | if(getAcl() == null || getAcl().trim().length() == 0) |
||
203 | return new JSONArray(); |
||
204 | return new JSONArray(getAcl()); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Sets the string ACL with the proper json |
||
209 | * @param json |
||
210 | * @throws JSONException |
||
211 | */ |
||
212 | public void setAclJson(JSONArray json) throws JSONException |
||
213 | { |
||
214 | if(json == null) |
||
215 | setAcl(null); |
||
216 | else |
||
217 | setAcl(json.toString()); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * sets the string Acl with the json built from given List |
||
222 | * @param acls |
||
223 | * @throws JSONException |
||
224 | */ |
||
225 | public void setAclList(ArrayList<Acl> acls) throws JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException { |
||
226 | if(acls == null || acls.size() == 0) |
||
227 | { |
||
228 | setAcl(null); |
||
229 | return; |
||
230 | } |
||
231 | setAcl(getAclJson(acls).toString()); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Empty Array Means no ACL's defined allays |
||
236 | * @return allays a list if no acls return empty list |
||
237 | * @throws JSONException |
||
238 | */ |
||
239 | public ArrayList<Acl> getAclList() throws JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException { |
||
240 | ArrayList<Acl> acls = new ArrayList<Acl>(); |
||
241 | if(getAcl() == null || getAcl().trim().length() == 0) |
||
242 | return acls; |
||
243 | Genson genson = getGenson(); |
||
244 | ArrayList<HashMap<String,String>> aclsDes = genson.deserialize(getAcl(),1.5.0/docs/api/java/util/ArrayList.html">ArrayList.class); |
||
245 | for(HashMap<String,String> acl: aclsDes) |
||
246 | { |
||
247 | 1.5.0/docs/api/java/security/acl/Acl.html">Acl a = new 1.5.0/docs/api/java/security/acl/Acl.html">Acl(); |
||
248 | a.setType(acl.get("type")); |
||
249 | a.setOperation(acl.get("operation")); |
||
250 | a.setAccess(acl.get("access")); |
||
251 | a.setId(acl.get("id")); |
||
252 | acls.add(a); |
||
253 | } |
||
254 | return acls; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Add a acl to String json at the end |
||
259 | * @param acl |
||
260 | * @throws IOException |
||
261 | * @throws TransformationException |
||
262 | * @throws JSONException |
||
263 | */ |
||
264 | public void addAcl(1.5.0/docs/api/java/security/acl/Acl.html">Acl acl) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException { |
||
265 | if(acl == null) |
||
266 | return; |
||
267 | ArrayList<Acl> acls = getAclList(); |
||
268 | acls.add(acl); |
||
269 | setAclList(acls); |
||
270 | } |
||
271 | |||
272 | private static Genson getGenson(){ |
||
273 | Genson genson = new Genson.Builder() |
||
274 | .exclude(5+0%2Fdocs%2Fapi+Object">Object.class) |
||
275 | .include("access", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class) |
||
276 | .include("type", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class) |
||
277 | .include("id", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class) |
||
278 | .include("operation", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class) |
||
279 | .create(); |
||
280 | return genson; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Utilitário |
||
285 | * @param aclList |
||
286 | * @return |
||
287 | * @throws IOException |
||
288 | * @throws TransformationException |
||
289 | * @throws JSONException |
||
290 | */ |
||
291 | public static JSONArray getAclJson(ArrayList<Acl> aclList) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException { |
||
292 | if(aclList == null) |
||
293 | return new JSONArray(); |
||
294 | Genson genson = getGenson(); |
||
295 | 1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(aclList); |
||
296 | //System.out.println(json); |
||
297 | return new JSONArray(json); |
||
298 | } |
||
299 | |||
300 | |||
301 | public boolean isChildsInitialized() { |
||
302 | return childsInitialized; |
||
303 | } |
||
304 | |||
305 | public void setChildsInitialized(boolean childsInitialized) { |
||
306 | this.childsInitialized = childsInitialized; |
||
307 | } |
||
308 | |||
309 | public boolean isParentInitialized() { |
||
310 | return parentInitialized; |
||
311 | } |
||
312 | |||
313 | public void setParentInitialized(boolean parentInitialized) { |
||
314 | this.parentInitialized = parentInitialized; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Metodo de testes |
||
319 | * @param args |
||
320 | * @throws IOException |
||
321 | * @throws TransformationException |
||
322 | * @throws JSONException |
||
323 | */ |
||
324 | public static void main(1.5.0/docs/api/java/lang/String.html">String [] args) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JSONException { |
||
325 | |||
326 | |||
327 | |||
328 | } |
||
329 | |||
330 | public boolean canAdmin(UserSessionImpl u) |
||
331 | { |
||
332 | if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie())) |
||
333 | { |
||
334 | logger.warn("Clearances checked for Mr:" + sess.getCookie() + " but clearing for Mr:" + u.getCookie()); |
||
335 | return false; |
||
336 | } |
||
337 | else if(userSessionClearences != null) |
||
338 | { |
||
339 | for(1.5.0/docs/api/java/util/Map.html">Map.Entry<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation,Boolean> entry: userSessionClearences.entrySet()) |
||
340 | { |
||
341 | if(entry.getValue() == true && |
||
342 | !entry.getKey().name().startsWith("READ")) |
||
343 | { |
||
344 | return true; |
||
345 | } |
||
346 | } |
||
347 | return false; |
||
348 | } |
||
349 | else |
||
350 | { |
||
351 | logger.warn("Checking clearance without sessionClearances initialized, not allowed"); |
||
352 | return false; |
||
353 | } |
||
354 | } |
||
355 | |||
356 | |||
357 | |||
358 | |||
359 | public boolean isClear(UserSessionImpl u, 1.5.0/docs/api/java/lang/String.html">String OP) |
||
360 | { |
||
361 | 1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation o = 1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation.parse(OP); |
||
362 | return isClear(u,o); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Same logic in isClearanceInitialized Method |
||
367 | * @param u |
||
368 | * @return |
||
369 | */ |
||
370 | public boolean isClear(UserSessionImpl u, PageContentImpl.1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation op) |
||
371 | { |
||
372 | if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie())) |
||
373 | { |
||
374 | logger.warn("Clearances checked for Mr:" + sess.getCookie() + " but clearing for Mr:" + u.getCookie()); |
||
375 | return false; |
||
376 | } |
||
377 | else if(userSessionClearences != null) |
||
378 | { |
||
379 | return userSessionClearences.get(op); |
||
380 | } |
||
381 | else |
||
382 | { |
||
383 | logger.warn("Checking clearance without sessionClearances initialized, not allowed"); |
||
384 | return false; |
||
385 | } |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Same logic in isClear Method |
||
390 | * @param u |
||
391 | * @return |
||
392 | */ |
||
393 | public boolean isClearanceInitialized(UserSessionImpl u) |
||
394 | { |
||
395 | if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie())) |
||
396 | { |
||
397 | return false; |
||
398 | } |
||
399 | else if(userSessionClearences == null) |
||
400 | { |
||
401 | return false; |
||
402 | } |
||
403 | return true; |
||
404 | } |
||
405 | |||
1326 | jmachado | 406 | @1.5.0/docs/api/java/lang/Override.html">Override |
407 | public void initClearances(UserSessionImpl u, boolean force) { |
||
408 | throw new NotImplementedException(); |
||
409 | } |
||
1312 | jmachado | 410 | |
411 | |||
1326 | jmachado | 412 | |
1312 | jmachado | 413 | public void setUserSessionClearences(Map<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation, Boolean> userSessionClearences) { |
414 | this.userSessionClearences = userSessionClearences; |
||
415 | } |
||
416 | |||
417 | public void setSess(UserSession sess) { |
||
418 | this.sess = sess; |
||
419 | } |
||
420 | |||
421 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
422 | public 1.5.0/docs/api/java/lang/String.html">String getAccessPolicyClass() { |
||
423 | if(super.getAccessPolicyClass() == null) |
||
424 | { |
||
425 | logger.debug("No AccessPolicyClass in PageContent " + getId() + ":" + getTitle() + " using default:" + PageContentAccessPolicyAcl.class.getName()); |
||
426 | return PageContentAccessPolicyAcl.class.getName(); |
||
427 | } |
||
428 | return super.getAccessPolicyClass(); |
||
429 | } |
||
430 | } |