1
2
3
4 package org.chenillekit.access.internal;
5
6 import org.apache.tapestry5.EventContext;
7 import org.apache.tapestry5.annotations.OnEvent;
8 import org.apache.tapestry5.services.ContextValueEncoder;
9 import org.apache.tapestry5.services.TransformMethodSignature;
10 import org.chenillekit.access.ChenilleKitAccessConstants;
11
12
13
14
15 public class ChenillekitAccessInternalUtils
16 {
17 private static final String[] NO_GROUPS = new String[0];
18
19 private ChenillekitAccessInternalUtils() {}
20
21
22
23
24
25
26
27 public static final String getContextAsString(EventContext context)
28 {
29 String res = "";
30
31 for (int i = 0; i < context.getCount(); i++)
32 {
33 res = res + context.get(String.class, i) + "####";
34 }
35
36 return res;
37 }
38
39
40
41
42
43
44
45 public static final EventContext getContextFromString(ContextValueEncoder valueEncoder,
46 String contextString)
47 {
48 String[] contextElements = contextString.split("####");
49
50 return new CKUrlEventContext(valueEncoder, contextElements);
51 }
52
53
54
55
56
57
58
59
60
61 public static final boolean hasUserRequiredRole(int userRoleWeigh, int requiredRoleWeigh)
62 {
63 return userRoleWeigh >= requiredRoleWeigh;
64 }
65
66
67
68
69
70
71
72
73
74 public static final boolean hasUserRequiredGroup(String[] userGroups, String[] requiredGroups)
75 {
76 boolean hasGroup = false;
77
78
79
80
81 if (requiredGroups.length == 0)
82 return true;
83
84 for (String requiredGroup : requiredGroups)
85 {
86 for (String userGroup : userGroups)
87 {
88 if (userGroup.equalsIgnoreCase(requiredGroup))
89 {
90 hasGroup = true;
91 break;
92 }
93 }
94
95 if (hasGroup)
96 break;
97 }
98
99 return hasGroup;
100 }
101
102
103
104
105
106
107
108
109 public static final String buildMetaForHandlerMethod(String componentId, String eventType, String suffix)
110 {
111 return ChenilleKitAccessConstants.RESTRICTED_EVENT_HANDLER_PREFIX
112 + "-" + componentId + "-" + eventType + "-" + suffix;
113
114 }
115
116
117
118
119
120
121 public static final String getStringArrayAsString(String[] groups)
122 {
123 String csvString = "";
124
125 for (int i = 0; i < groups.length; i++)
126 {
127 if (i == 0)
128 csvString += groups[i];
129 else
130 csvString += "," + groups[i];
131 }
132
133 return csvString;
134 }
135
136
137
138
139
140
141 public static final String[] getStringAsStringArray(String groups)
142 {
143 if ( ChenilleKitAccessConstants.NO_RESTRICTION.equals(groups)) return NO_GROUPS;
144 return groups.split(",");
145 }
146
147
148
149
150
151 public static final String extractComponentId(TransformMethodSignature method, OnEvent annotation)
152 {
153 if (annotation != null) return annotation.component();
154
155
156
157 String name = method.getMethodName();
158
159 int fromx = name.indexOf("From");
160
161 if (fromx < 0) return "";
162
163 return name.substring(fromx + 4);
164 }
165
166
167
168
169
170 public static final String extractEventType(TransformMethodSignature method, OnEvent annotation)
171 {
172 if (annotation != null) return annotation.value();
173
174
175
176 String name = method.getMethodName();
177
178 int fromx = name.indexOf("From");
179
180 return fromx == -1 ? name.substring(2) : name.substring(2, fromx);
181 }
182
183 }