KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > acl > Policy


1 package de.webman.acl;
2
3 import com.teamkonzept.lib.TKException;
4 import com.teamkonzept.lib.TKVector;
5 import de.webman.acl.db.PolicyDBData;
6
7 /**
8  * A policy applies a role to a login and an object reference. Thus, it
9  * constitutes a concrete access control right.
10  *
11  * @version 1.0
12  * @since 1.0
13  * @author © 2001 Webman AG
14  */

15 public class Policy
16     extends WMObject
17 {
18
19     // $Header: /cvsroot/webman-cms/source/webman/de/webman/acl/Policy.java,v 1.1 2001/08/20 08:25:07 mischa Exp $
20

21     // Constants
22

23     /**
24      * Type constant.
25      */

26     public static final Integer JavaDoc ROLE_ID = new Integer JavaDoc(0);
27
28     /**
29      * Type constant.
30      */

31     public static final Integer JavaDoc USER_ID = new Integer JavaDoc(1);
32
33     /**
34      * Type constant.
35      */

36     public static final Integer JavaDoc POLICY_ID = new Integer JavaDoc(2);
37
38     /**
39      * Type constant.
40      */

41     public static final Integer JavaDoc ATTRIBUTE_ID = new Integer JavaDoc(3);
42
43     /**
44      * Type constant.
45      */

46     public static final Integer JavaDoc CLASS_REGISTRY_ID = new Integer JavaDoc(4);
47
48     /**
49      * Type constant.
50      */

51     public static final Integer JavaDoc CONTENT_TREE_ID = new Integer JavaDoc(5);
52
53     /**
54      * Type constant.
55      */

56     public static final Integer JavaDoc FORM_ID = new Integer JavaDoc(6);
57
58     /**
59      * Type constant.
60      */

61     public static final Integer JavaDoc PRESENTATION_ID = new Integer JavaDoc(7);
62
63     /**
64      * Type constant.
65      */

66     public static final Integer JavaDoc PROPERTY_ID = new Integer JavaDoc(8);
67
68     /**
69      * Type constant.
70      */

71     public static final Integer JavaDoc PROPERTY_GROUP_ID = new Integer JavaDoc(9);
72
73     /**
74      * Type constant.
75      */

76     public static final Integer JavaDoc SITE_TREE_ID = new Integer JavaDoc(10);
77
78     /**
79      * Type constant.
80      */

81     public static final Integer JavaDoc TEMPLATE_ID = new Integer JavaDoc(11);
82
83     /**
84      * Type constant.
85      */

86     public static final Integer JavaDoc TEXT_ATTRIBUTE_ID = new Integer JavaDoc(12);
87
88
89     // Attributes
90

91     /**
92      * The access mode of the policy.
93      */

94     private boolean access = false;
95
96     /**
97      * The login asscoiated with this policy.
98      */

99     private Integer JavaDoc login = null;
100
101     /**
102      * The role asscoiated with this policy.
103      */

104     private Integer JavaDoc role = null;
105
106     /**
107      * The context asscoiated with this policy.
108      */

109     private Integer JavaDoc context = null;
110
111     /**
112      * The object reference.
113      */

114     private Integer JavaDoc reference = null;
115
116     /**
117      * The object type.
118      */

119     private Integer JavaDoc type = null;
120
121
122     // Constructors
123

124     /**
125      * Provide instantion only to package classes or subclasses.
126      *
127      * @param data the initial policy data.
128      */

129     protected Policy (PolicyDBData data)
130     {
131         super(data);
132
133         this.access = data.isAllowed();
134         this.login = data.getLogin();
135         this.role = data.getRole();
136         this.context = data.getContext();
137         this.reference = data.getObjectReference();
138         this.type = data.getObjectType();
139     }
140
141
142     // Method implementations
143

144     /**
145      * Returns the factory of the object.
146      *
147      * @return the factory of the object.
148      * @exception com.teamkonzept.lib.TKException if an error occured during factory retrieval.
149      */

150     public final ObjectFactory getFactory ()
151         throws TKException
152     {
153         return PolicyFactory.getInstance();
154     }
155
156     /**
157      * Checks wether the login is allowed to perform the role's tasks on the
158      * controlled object.
159      *
160      * @return <CODE>true</CODE> if the login is allowed to perform the role's
161      * tasks on the controlled object, otherwise <CODE>false</CODE>.
162      */

163     public final boolean isAllowed ()
164     {
165         return access;
166     }
167
168     /**
169      * Checks wether the execution of the role's tasks on the controlled object by
170      * the login is denied.
171      *
172      * @return <CODE>true</CODE> the execution of the role's tasks on the
173      * controlled object by the login is denied, otherwise <CODE>false</CODE>.
174      */

175     public final boolean isDenied ()
176     {
177         return ! access;
178     }
179
180     /**
181      * Allows the login is allowed to perform the role's tasks on the controlled
182      * object.
183      */

184     public final void allow ()
185     {
186         super.modifyAttribute(this.access ? Boolean.TRUE : Boolean.FALSE, Boolean.TRUE);
187         this.access = true;
188     }
189
190     /**
191      * Denies the execution of the role's tasks on the controlled object by the
192      * login.
193      */

194     public final void deny ()
195     {
196         super.modifyAttribute(this.access ? Boolean.TRUE : Boolean.FALSE, Boolean.FALSE);
197         this.access = false;
198     }
199
200     /**
201      * Returns the ID of the login of the policy.
202      *
203      * @return the ID of the login of the policy.
204      */

205     public final Integer JavaDoc getLoginID ()
206     {
207         return login;
208     }
209
210     /**
211      * Returns the login of the policy.
212      *
213      * @return the login of the policy.
214      * @exception com.teamkonzept.lib.TKException if an error occured during login retrieval.
215      */

216     public final Login getLogin ()
217         throws TKException
218     {
219         return login != null
220                      ? (Login) LoginFactory.getInstance().getObject(login)
221                      : null;
222     }
223
224     /**
225      * Assigns the login of the policy.
226      *
227      * @param login the login of the policy.
228      */

229     public final void setLogin (Login login)
230     {
231         Integer JavaDoc id = login != null
232                            ? login.getID()
233                            : null;
234
235         super.modifyAttribute(this.login, id);
236         this.login = id;
237     }
238
239     /**
240      * Returns the ID of the role of the policy.
241      *
242      * @return the ID of the role of the policy.
243      */

244     public final Integer JavaDoc getRoleID ()
245     {
246         return role;
247     }
248
249     /**
250      * Returns the role of the policy.
251      *
252      * @return the role of the policy.
253      * @exception com.teamkonzept.lib.TKException if an error occured during role retrieval.
254      */

255     public final Role getRole ()
256         throws TKException
257     {
258         return role != null
259                     ? (Role) RoleFactory.getInstance().getObject(role)
260                     : null;
261     }
262
263     /**
264      * Assigns the role of the policy.
265      *
266      * @param role the role of the policy.
267      */

268     public final void setRole (Role role)
269     {
270         Integer JavaDoc id = role != null
271                           ? role.getID()
272                           : null;
273
274         super.modifyAttribute(this.role, id);
275         this.role = id;
276     }
277
278     /**
279      * Returns the ID of the context of the policy.
280      *
281      * @return the ID of the context of the policy.
282      */

283     public final Integer JavaDoc getContextID ()
284     {
285         return context;
286     }
287
288     /**
289      * Returns the context of the policy.
290      *
291      * @return the context of the policy.
292      * @exception com.teamkonzept.lib.TKException if an error occured during context retrieval.
293      */

294     public final Context getContext ()
295         throws TKException
296     {
297         return context != null
298                        ? (Context) ContextFactory.getInstance().getObject(context)
299                        : null;
300     }
301
302     /**
303      * Assigns the context of the policy.
304      *
305      * @param context the context of the policy.
306      */

307     public final void setContext (Context context)
308     {
309         Integer JavaDoc id = context != null
310                              ? context.getID()
311                              : null;
312
313         super.modifyAttribute(this.context, id);
314         this.context = id;
315     }
316
317     /**
318      * Returns the object reference of the policy.
319      *
320      * @return the object reference of the policy.
321      */

322     public final Integer JavaDoc getObjectReference ()
323     {
324         return this.reference;
325     }
326
327     /**
328      * Returns the object type of the policy.
329      *
330      * @return the object type of the policy.
331      */

332     public final Integer JavaDoc getObjectType ()
333     {
334         return this.type;
335     }
336
337     /**
338      * Returns all events referencing the policy.
339      *
340      * @return all events referencing the policy.
341      * @exception com.teamkonzept.lib.TKException if an error occured during event retrieval.
342      */

343     public final TKVector getEvents ()
344         throws TKException
345     {
346         return EventFactory.getInstance()
347                              .getObjects(EventFactory.getInstance()
348                                                        .getEventProxies(this.getID()));
349     }
350
351
352     // Convenience methods
353

354     /**
355      * Claims control for the whole context.
356      */

357     public final void setWholeContextControl ()
358     {
359         setObjectControl(null, null);
360     }
361
362     /**
363      * Assigns the reference of the controlled attribute.
364      *
365      * @param reference the reference of the controlled attribute.
366      */

367     public final void setAttributeControl (Integer JavaDoc reference)
368     {
369         setObjectControl(reference, ATTRIBUTE_ID);
370     }
371
372     /**
373      * Assigns the reference of the controlled class registry.
374      *
375      * @param reference the reference of the controlled class registry.
376      */

377     public final void setClassRegistryControl (Integer JavaDoc reference)
378     {
379         setObjectControl(reference, CLASS_REGISTRY_ID);
380     }
381
382     /**
383      * Assigns the reference of the controlled content tree.
384      *
385      * @param reference the reference of the controlled content tree.
386      */

387     public final void setContentTreeControl (Integer JavaDoc reference)
388     {
389         setObjectControl(reference, CONTENT_TREE_ID);
390     }
391
392     /**
393      * Assigns the reference of the controlled form.
394      *
395      * @param reference the reference of the controlled form.
396      */

397     public final void setFormControl (Integer JavaDoc reference)
398     {
399         setObjectControl(reference, FORM_ID);
400     }
401
402     /**
403      * Assigns the reference of the controlled policy.
404      *
405      * @param reference the reference of the controlled policy.
406      */

407     public final void setPolicyControl (Integer JavaDoc reference)
408     {
409         setObjectControl(reference, POLICY_ID);
410     }
411
412     /**
413      * Assigns the reference of the controlled presentation.
414      *
415      * @param reference the reference of the controlled presentation.
416      */

417     public final void setPresentationControl (Integer JavaDoc reference)
418     {
419         setObjectControl(reference, PRESENTATION_ID);
420     }
421
422     /**
423      * Assigns the reference of the controlled property.
424      *
425      * @param reference the reference of the controlled property.
426      */

427     public final void setPropertyControl (Integer JavaDoc reference)
428     {
429         setObjectControl(reference, PROPERTY_ID);
430     }
431
432     /**
433      * Assigns the reference of the controlled property group.
434      *
435      * @param reference the reference of the controlled property group.
436      */

437     public final void setPropertyGroupControl (Integer JavaDoc reference)
438     {
439         setObjectControl(reference, PROPERTY_GROUP_ID);
440     }
441
442     /**
443      * Assigns the reference of the controlled role.
444      *
445      * @param reference the reference of the controlled role.
446      */

447     public final void setRoleControl (Integer JavaDoc reference)
448     {
449         setObjectControl(reference, ROLE_ID);
450     }
451
452     /**
453      * Assigns the reference of the controlled site tree.
454      *
455      * @param reference the reference of the controlled site tree.
456      */

457     public final void setSiteTreeControl (Integer JavaDoc reference)
458     {
459         setObjectControl(reference, SITE_TREE_ID);
460     }
461
462     /**
463      * Assigns the reference of the controlled template.
464      *
465      * @param reference the reference of the controlled template.
466      */

467     public final void setTemplateControl (Integer JavaDoc reference)
468     {
469         setObjectControl(reference, TEMPLATE_ID);
470     }
471
472     /**
473      * Assigns the reference of the controlled text attribute.
474      *
475      * @param reference the reference of the controlled text attribute.
476      */

477     public final void setTextAttributeControl (Integer JavaDoc reference)
478     {
479         setObjectControl(reference, TEXT_ATTRIBUTE_ID);
480     }
481
482     /**
483      * Assigns the reference of the controlled user.
484      *
485      * @param reference the reference of the controlled user.
486      */

487     public final void setUserControl (Integer JavaDoc reference)
488     {
489         setObjectControl(reference, USER_ID);
490     }
491
492     /**
493      * Assigns the reference and the type of the controlled object.
494      *
495      * @param reference the reference of the controlled object.
496      * @param type the type of the controlled object.
497      */

498     private final void setObjectControl (Integer JavaDoc reference, Integer JavaDoc type)
499     {
500         super.modifyAttribute(this.reference, reference);
501         this.reference = reference;
502
503         super.modifyAttribute(this.type, type);
504         this.type = type;
505     }
506
507     /**
508      * Checks wether the given type represents atomic access rights.
509      * <P>
510      * A type represents atomic access rights, if it does not represent
511      * generic access rights.
512      *
513      * @return <CODE>true</CODE> if the given type does not represent
514      * generic access rights, otherwise <CODE>false</CODE>.
515      */

516     public static final boolean isAtomic (Integer JavaDoc type)
517     {
518         return ! Policy.isGeneric(type);
519     }
520
521     /**
522      * Checks wether the given type represents generic access rights.
523      * <P>
524      * A type represents generic access rights, if it is set to content
525      * tree or site tree.
526      *
527      * @return <CODE>true</CODE> if the given type represents
528      * generic access rights, otherwise <CODE>false</CODE>.
529      */

530     public static final boolean isGeneric (Integer JavaDoc type)
531     {
532         return Policy.CONTENT_TREE_ID.equals(type) ||
533                Policy.SITE_TREE_ID.equals(type);
534     }
535
536 }
537
Popular Tags