1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.serializer.objects;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21
22 import javolution.xml.XMLFormat;
23 import javolution.xml.stream.XMLStreamException;
24
25 import org.apache.commons.lang.StringEscapeUtils;
26 import org.apache.jetspeed.security.FolderPermission;
27 import org.apache.jetspeed.security.FragmentPermission;
28 import org.apache.jetspeed.security.PagePermission;
29 import org.apache.jetspeed.security.PortalResourcePermission;
30 import org.apache.jetspeed.security.PortletPermission;
31
32 /***
33 * Serialized Permission <permission type='folder' resource='/' actions='view,
34 * edit'> <roles>admin, user</roles> <groups>dev</groups> <users>joe</users>
35 * </permission>
36 *
37 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
38 * @version $Id: $
39 */
40 public class JSPermission
41 {
42
43 private String type;
44
45 private String resource;
46
47 private String actions;
48
49 private long id;
50
51 private ArrayList roles = null;
52
53 private ArrayList groups = null;
54
55 private ArrayList users = null;
56
57 private JSUserRoles roleString;
58
59 private JSUserGroups groupString;
60
61 private JSUserUsers userString;
62
63 public static final String TYPE_FOLDER = "folder".intern();
64
65 public static final String TYPE_FRAGMENT = "fragment".intern();
66
67 public static final String TYPE_PAGE = "page".intern();
68
69 public static final String TYPE_PORTALRESOURCE = "portalResource".intern();
70
71 public static final String TYPE_PORTALRESOURCECOLLECTION = "portalResource"
72 .intern();
73
74 public static final String TYPE_PORTAL = "portal".intern();
75
76 public static final String TYPE_UNKNOWN = "unknown".intern();
77
78 public String getClassForType(String type)
79 {
80 if ((type == null) || (type.length() == 0) || (type.equals(TYPE_UNKNOWN)))
81 return "";
82 if (type.equals(TYPE_FOLDER))
83 return "org.apache.jetspeed.security.FolderPermission";
84 if (type.equals(TYPE_FRAGMENT))
85 return "org.apache.jetspeed.security.FragmentPermission";
86 if (type.equals(TYPE_PAGE))
87 return "org.apache.jetspeed.security.PagePermission";
88 if (type.equals(TYPE_PORTALRESOURCE))
89 return "org.apache.jetspeed.security.PortalResourcePermission";
90 if (type.equals(TYPE_PORTALRESOURCECOLLECTION))
91 return "org.apache.jetspeed.security.PortalResourcePermissionCollection";
92 if (type.equals(TYPE_PORTAL))
93 return "org.apache.jetspeed.security.PortletPermission";
94 return "";
95 }
96
97 public String getTypeForClass(String className)
98 {
99 if ((className == null) || (className.length() == 0))
100 return TYPE_UNKNOWN;
101 if (className.equals("org.apache.jetspeed.security.FolderPermission"))
102 return TYPE_FOLDER;
103
104 if (className.equals("org.apache.jetspeed.security.FragmentPermission"))
105 return TYPE_FRAGMENT;
106 if (className.equals("org.apache.jetspeed.security.PagePermission"))
107 return TYPE_PAGE;
108 if (className.equals("org.apache.jetspeed.security.PortletPermission"))
109 return TYPE_PORTAL;
110
111 if (className
112 .equals("org.apache.jetspeed.security.PortalResourcePermission"))
113 return TYPE_PORTALRESOURCE;
114 if (className
115 .equals("org.apache.jetspeed.security.PortalResourcePermissionCollection"))
116 return TYPE_PORTALRESOURCECOLLECTION;
117 return TYPE_UNKNOWN;
118
119 }
120
121 public PortalResourcePermission getPermissionForType()
122 {
123 PortalResourcePermission newPermission = null;
124 if ((this.type == null) || (this.type.equals(TYPE_UNKNOWN)))
125 return null;
126 try
127 {
128 if (type.equals(TYPE_FOLDER))
129 newPermission = new FolderPermission(this.resource,this.actions);
130 else if (type.equals(TYPE_FRAGMENT))
131 newPermission = new FragmentPermission(this.resource,this.actions);
132 else if (type.equals(TYPE_PAGE))
133 newPermission = new PagePermission(this.resource,this.actions);
134 else if (type.equals(TYPE_PORTAL))
135 newPermission = new PortletPermission(this.resource,this.actions);
136 else return null;
137 return newPermission;
138 }
139 catch (Exception e)
140 {
141 e.printStackTrace();
142 return null;
143 }
144
145 }
146 public JSPermission()
147 {
148 }
149
150 private String append(JSRole rule)
151 {
152 return rule.getName();
153 }
154
155 private String append(JSGroup group)
156 {
157 return group.getName();
158 }
159
160 private String append(JSUser user)
161 {
162 return user.getName();
163 }
164
165 private String append(Object s)
166 {
167 if (s instanceof JSRole)
168 return append((JSRole) s);
169 if (s instanceof JSGroup)
170 return append((JSGroup) s);
171 if (s instanceof JSUser)
172 return append((JSUser) s);
173
174 return s.toString();
175 }
176
177 private String putTokens(ArrayList _list)
178 {
179 if ((_list == null) || (_list.size() == 0))
180 return "";
181 boolean _start = true;
182 Iterator _it = _list.iterator();
183 StringBuffer _sb = new StringBuffer();
184 while (_it.hasNext())
185 {
186 if (!_start)
187 _sb.append(',');
188 else
189 _start = false;
190
191 _sb.append(append(_it.next()));
192 }
193 return _sb.toString();
194 }
195
196 /***
197 * @return Returns the actions.
198 */
199 public String getActions()
200 {
201 return actions;
202 }
203
204 /***
205 * @param actions
206 * The actions to set.
207 */
208 public void setActions(String actions)
209 {
210 this.actions = actions;
211 }
212
213 /***
214 * @return Returns the groups.
215 */
216 public ArrayList getGroups()
217 {
218 return groups;
219 }
220
221 /***
222 * @param groups
223 * The groups to set.
224 */
225 public void setGroups(ArrayList groups)
226 {
227 this.groups = groups;
228 }
229
230 /***
231 * @return Returns the resource.
232 */
233 public String getResource()
234 {
235 return resource;
236 }
237
238 /***
239 * @param resource
240 * The resource to set.
241 */
242 public void setResource(String resource)
243 {
244 this.resource = resource;
245 }
246
247 /***
248 * @return Returns the roles.
249 */
250 public ArrayList getRoles()
251 {
252 return roles;
253 }
254
255 /***
256 * @param roles
257 * The roles to set.
258 */
259 public void setRoles(ArrayList roles)
260 {
261 this.roles = roles;
262 }
263
264 /***
265 * @return Returns the type.
266 */
267 public String getType()
268 {
269 return type;
270 }
271
272 /***
273 * @param type
274 * The type to set.
275 */
276 public void setType(String type)
277 {
278 this.type = type;
279 }
280
281 /***
282 * @return Returns the users.
283 */
284 public ArrayList getUsers()
285 {
286 return users;
287 }
288
289 /***
290 * @param users
291 * The users to set.
292 */
293 public void setUsers(ArrayList users)
294 {
295 this.users = users;
296 }
297
298
299 /***
300 * @return Returns the id.
301 */
302 public long getId()
303 {
304 return id;
305 }
306
307 /***
308 * @param id
309 * The id to set.
310 */
311 public void setId(long id)
312 {
313 this.id = id;
314 }
315
316
317
318 public void addGroup(JSGroup group)
319 {
320 if (groups == null)
321 groups = new ArrayList();
322 groups.add(group);
323 }
324
325 public void addRole(JSRole role)
326 {
327 if (roles == null)
328 roles = new ArrayList();
329 roles.add(role);
330 }
331
332
333 public void addUser(JSUser user)
334 {
335 if (users == null)
336 users = new ArrayList();
337 users.add(user);
338 }
339
340
341 /****************************************************************************
342 * SERIALIZER
343 */
344 private static final XMLFormat XML = new XMLFormat(JSPermission.class)
345 {
346 public void write(Object o, OutputElement xml)
347 throws XMLStreamException
348 {
349 try
350 {
351 JSPermission g = (JSPermission) o;
352 xml.setAttribute("type", g.getType());
353 xml.setAttribute("resource",g.getResource());
354 xml.setAttribute("actions",g.getActions());
355 g.groupString = new JSUserGroups(g.putTokens(g.getGroups()));
356 g.roleString = new JSUserRoles(g.putTokens(g.getRoles()));
357 g.userString = new JSUserUsers(g.putTokens(g.getUsers()));
358 xml.add(g.roleString);
359 xml.add(g.groupString);
360 xml.add(g.userString);
361
362 } catch (Exception e)
363 {
364 e.printStackTrace();
365 }
366 }
367
368 public void read(InputElement xml, Object o)
369 {
370 try
371 {
372 JSPermission g = (JSPermission) o;
373 g.type = StringEscapeUtils.unescapeHtml(xml.getAttribute("type", "type_unknown"));
374 g.resource = StringEscapeUtils.unescapeHtml(xml.getAttribute("resource", "resource_unknown"));
375 g.actions = StringEscapeUtils.unescapeHtml(xml.getAttribute("actions", "unknown_actions"));
376
377 while (xml.hasNext())
378 {
379 Object o1 = xml.getNext();
380
381 if (o1 instanceof JSUserGroups)
382 g.groupString = (JSUserGroups) o1;
383 else if (o1 instanceof JSUserUsers)
384 g.userString = (JSUserUsers) o1;
385 else if (o1 instanceof JSUserRoles)
386 g.roleString = (JSUserRoles) o1;
387 }
388 } catch (Exception e)
389 {
390 e.printStackTrace();
391 }
392 }
393
394 };
395
396 public JSUserGroups getGroupString()
397 {
398 return groupString;
399 }
400
401 public JSUserRoles getRoleString()
402 {
403 return roleString;
404 }
405
406 public JSUserUsers getUserString()
407 {
408 return userString;
409 }
410
411
412
413 }