1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.chenillekit.access.services.impl;
20
21 import org.apache.tapestry5.annotations.OnEvent;
22 import org.apache.tapestry5.model.MutableComponentModel;
23 import org.apache.tapestry5.services.ClassTransformation;
24 import org.apache.tapestry5.services.ComponentClassTransformWorker;
25 import org.apache.tapestry5.services.TransformMethodSignature;
26 import org.chenillekit.access.ChenilleKitAccessConstants;
27 import org.chenillekit.access.ChenilleKitAccessException;
28 import org.chenillekit.access.annotations.Restricted;
29 import org.chenillekit.access.internal.ChenillekitAccessInternalUtils;
30
31
32
33
34
35 public class RestrictedWorker implements ComponentClassTransformWorker
36 {
37
38
39
40
41
42
43
44 public void transform(ClassTransformation transformation, MutableComponentModel model)
45 {
46 processPageRestriction(transformation, model);
47
48 processEventHandlerRestrictions(transformation, model);
49
50
51 }
52
53
54
55
56
57
58
59
60
61 private void processPageRestriction(ClassTransformation transformation, MutableComponentModel model)
62 {
63 Restricted pageRestricted = transformation.getAnnotation(Restricted.class);
64 if (pageRestricted != null)
65 {
66 String roleWeigh = Integer.toString(pageRestricted.role());
67 String[] groups = pageRestricted.groups();
68
69 model.setMeta(ChenilleKitAccessConstants.RESTRICTED_PAGE_ROLE, roleWeigh);
70
71 if (groups.length > 0)
72 model.setMeta(ChenilleKitAccessConstants.RESTRICTED_PAGE_GROUP,
73 ChenillekitAccessInternalUtils.getStringArrayAsString(groups));
74 }
75 }
76
77
78
79
80
81
82
83
84
85 private void processEventHandlerRestrictions(ClassTransformation transformation, MutableComponentModel model)
86 {
87 for (TransformMethodSignature method : transformation.findMethodsWithAnnotation(Restricted.class))
88 {
89 String methodName = method.getMethodName();
90 Restricted restricted = transformation.getMethodAnnotation(method, Restricted.class);
91 OnEvent event = transformation.getMethodAnnotation(method, OnEvent.class);
92 if (methodName.startsWith("on") || event != null)
93 {
94 String componentId = ChenillekitAccessInternalUtils.extractComponentId(method, event);
95 String eventType = ChenillekitAccessInternalUtils.extractEventType(method, event);
96
97 String groupMeta = ChenillekitAccessInternalUtils.buildMetaForHandlerMethod(componentId,
98 eventType,
99 ChenilleKitAccessConstants.RESTRICTED_EVENT_HANDLER_GROUPS_SUFFIX);
100
101 String roleMeta = ChenillekitAccessInternalUtils.buildMetaForHandlerMethod(componentId,
102 eventType,
103 ChenilleKitAccessConstants.RESTRICTED_EVENT_HANDLER_ROLE_SUFFIX);
104
105 model.setMeta(groupMeta, ChenillekitAccessInternalUtils.getStringArrayAsString(restricted.groups()));
106 model.setMeta(roleMeta, Integer.toString(restricted.role()));
107 }
108 else
109 {
110 throw new ChenilleKitAccessException("Cannot put Restricted annotation on a non event handler method");
111 }
112 }
113 }
114
115
116
117
118
119
120
121
122
123 private void processComponentsRestrictions(ClassTransformation transformation, MutableComponentModel model)
124 {
125 throw new RuntimeException("NOT YET IMPLEMENTED");
126 }
127 }