KickJava   Java API By Example, From Geeks To Geeks.

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


1 package de.webman.acl;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.Hashtable JavaDoc;
5 import java.util.Properties JavaDoc;
6 import com.teamkonzept.lib.ErrorCodes;
7 import com.teamkonzept.lib.TKException;
8 import com.teamkonzept.lib.TKVector;
9 import de.webman.acl.db.LoginDBData;
10 import de.webman.acl.resolver.ResolverFactory;
11 import com.teamkonzept.webman.mainint.events.TKUserException;
12 import com.teamkonzept.webman.mainint.events.UserCodes;
13
14 /**
15  * A login represents an identifiable user or user group whose access
16  * to WebMan functionality is controlled.
17  *
18  * @version 1.0
19  * @since 1.0
20  * @author © 2001 Webman AG
21  */

22 public abstract class Login
23     extends WMObject
24 {
25
26     // $Header: /cvsroot/webman-cms/source/webman/de/webman/acl/Login.java,v 1.2 2001/11/15 10:35:30 gregor Exp $
27

28     // Attributes
29

30     /**
31      * Specifies wether the ordering of the parents has changed.
32      */

33     private boolean parentsModified = false;
34
35     /**
36      * The name of the login object.
37      */

38     private String JavaDoc name = null;
39
40     /**
41      * The login of the login object.
42      */

43     private String JavaDoc login = null;
44
45     /**
46      * The priority order of parent login obects.
47      */

48     private TKVector parents = null;
49
50
51     // Constructors
52

53     /**
54      * Provide instantion only to package classes or subclasses.
55      *
56      * @param data the initial login data.
57      */

58     protected Login (LoginDBData data)
59     {
60         super(data);
61
62         this.name = data.getName();
63         this.login = data.getLogin();
64     }
65
66
67     // Method signatures
68

69     /**
70      * Checks wether this login object represents a user.
71      *
72      * @return <CODE>true</CODE> if this login object represents a user,
73      * otherwise <CODE>false</CODE>.
74      */

75     public abstract boolean isUser ();
76
77     /**
78      * Checks wether this login object represents a profile.
79      *
80      * @return <CODE>true</CODE> if this login object represents a profile,
81      * otherwise <CODE>false</CODE>.
82      */

83     public abstract boolean isProfile ();
84
85     /**
86      * Checks wether this login object is a parent of the given login object.
87      *
88      * @param login the login object.
89      * @return <CODE>true</CODE> if this login object is a parent of the
90      * given login object, otherwise <CODE>false</CODE>.
91      * @exception com.teamkonzept.lib.TKException if an error occured during login retrieval.
92      */

93     public abstract boolean isParent (Login login)
94         throws TKException;
95
96
97     // Method implementations
98

99     /**
100      * Returns the name of the login object.
101      *
102      * @return the name of the login object.
103      */

104     public final String JavaDoc getName ()
105     {
106         return name;
107     }
108
109     /**
110      * Assigns the name of the login object.
111      *
112      * @param name the name of the login object.
113      */

114     public final void setName (String JavaDoc name)
115     {
116         super.modifyAttribute(this.name, name);
117         this.name = name;
118     }
119
120     /**
121      * Returns the login of the login object.
122      *
123      * @return the login of the login object.
124      */

125     public final String JavaDoc getLogin ()
126     {
127         return login;
128     }
129
130     /**
131      * Assigns the login of the login object.
132      *
133      * @param login the login of the login object.
134      * @exception com.teamkonzept.lib.TKException if an error occured while checking the
135      * login or the login is invalid.
136      */

137     public final void setLogin (String JavaDoc login)
138         throws TKException
139     {
140         // Check login token.
141
LoginFactory.getInstance().checkLogin(login);
142
143         // Proceed if no errors occur.
144
super.modifyAttribute(this.login, login);
145         this.login = login;
146     }
147
148     /**
149      * Returns all profiles referencing the login.
150      *
151      * @return all profiles referencing the login.
152      * @exception com.teamkonzept.lib.TKException if an error occured during login object retrieval.
153      */

154     public final TKVector getParents ()
155         throws TKException
156     {
157         return LoginFactory.getInstance().getLogins(this);
158     }
159
160
161     // Parent order handling methods
162

163     /**
164      * Checks wether the current analysis order of all profiles referencing
165      * the login has been changed.
166      *
167      * @return <CODE>true</CODE> if the current analysis order of all profiles
168      * referencing the login has been changed, otherwise <CODE>false</CODE>.
169      */

170     public final boolean isModifiedParents ()
171     {
172         return this.parentsModified;
173     }
174
175     /**
176      * Informs the login about the succesful update of the current analysis order
177      * or changes in the kinship.
178      */

179     protected final void updatedParents ()
180     {
181         this.parentsModified = false;
182         this.parents = null;
183     }
184
185     /**
186      * Returns the IDs of all profiles referencing the login in the current
187      * analysis order.
188      *
189      * @return the IDs of all profiles referencing the login in the current
190      * analysis order.
191      * @exception com.teamkonzept.lib.TKException if an error occured during login retrieval.
192      */

193     public final TKVector getParentOrder ()
194         throws TKException
195     {
196         if (this.parents == null)
197         {
198             this.parents = LoginFactory.getInstance().getLoginIDs(this);
199         }
200
201         return this.parents;
202     }
203
204     /**
205      * Assigns the current analysis order of all profiles referencing the login.
206      *
207      * @param order the current analysis order of all profiles referencing the login.
208      * @exception com.teamkonzept.lib.TKException if an error occured during login retrieval
209      * or the given profiles do not match the parent profiles.
210      */

211     public final void setParentOrder (TKVector order)
212         throws TKException
213     {
214         if (order != null)
215         {
216             TKVector current = getParentOrder();
217
218             if (current != null)
219             {
220                 // Check number of parents.
221
if (current.size() != order.size())
222                 {
223                                     throw new TKUserException("The number of groups given ("
224                                                               + order.size()
225                                                               + ") does not match the number of the parent groups ("
226                                                               + current.size()
227                                                               + ").",
228                                                               UserCodes.INVALID_PARENT_COUNT,
229                                                               ErrorCodes.USER_SEVERITY,
230                                                               true,
231                                                               new Object JavaDoc[]{String.valueOf(order.size()),
232                                                                            String.valueOf(current.size())},
233                                                               null);
234                 }
235
236                 // Check membership of parents.
237
int index = 0;
238                 int size = current.size();
239
240                 this.parents = new TKVector(size);
241
242                 while (index < size)
243                 {
244                     Integer JavaDoc parent = (Integer JavaDoc) order.elementAt(index++);
245
246                     if (! current.contains(parent))
247                     {
248                         // Remove update information in error cases.
249
updatedParents();
250
251                         // Retrieve name.
252
String JavaDoc name = ProfileFactory.getInstance()
253                                                       .getProfile(parent)
254                                                       .getName();
255
256                         throw new TKUserException("The group '" +
257                                                   name +
258                                                   "' does not belong to the parent groups.",
259                                                   UserCodes.INVALID_PARENT_GROUP,
260                                                   ErrorCodes.USER_SEVERITY,
261                                                   true,
262                                                   new Object JavaDoc[]{name},
263                                                   null);
264                     }
265
266                     this.parents.addElement(parent);
267                 }
268
269                 // Set update information.
270
this.parentsModified = true;
271             }
272         }
273     }
274
275
276     // Access right handling methods
277

278     /**
279      * Retrieves all allowed events in the specified application context.
280      *
281      * @param context the ID of the current context.
282      * @return all allowed events in the specified application context.
283      * @exception com.teamkonzept.lib.TKException if an error occured during event retrieval.
284      */

285     public final boolean isAllowed (Integer JavaDoc event,
286                                     Integer JavaDoc context)
287         throws TKException
288     {
289         // Convenience.
290
return isAllowed(event, context, null, null);
291     }
292
293     /**
294      * Retrieves all allowed events in the specified application context.
295      *
296      * @param context the ID of the current context.
297      * @param type the current object type.
298      * @param reference the current object reference.
299      * @return all allowed events in the specified application context.
300      * @exception com.teamkonzept.lib.TKException if an error occured during event retrieval.
301      */

302     public final boolean isAllowed (Integer JavaDoc event,
303                                     Integer JavaDoc context,
304                                     Integer JavaDoc type,
305                                     Integer JavaDoc reference)
306         throws TKException
307     {
308         boolean allowed = false;
309         Hashtable JavaDoc checks = new Hashtable JavaDoc();
310         
311         // Resolve allowed events.
312
ResolverFactory.getInstance()
313             .getResolver(this)
314             .resolve(checks, context, type, reference);
315         
316         // Check given event.
317
allowed = checks.containsKey(event);
318         
319         return allowed;
320     }
321
322     /**
323      * Retrieves all allowed events in the specified application context.
324      *
325      * @param context the ID of the current context.
326      * @return all allowed events in the specified application context.
327      * @exception com.teamkonzept.lib.TKException if an error occured during event retrieval.
328      */

329     public final TKVector getAllowedEvents (Integer JavaDoc context)
330         throws TKException
331     {
332         // Convenience.
333
return getAllowedEvents(context, null, null);
334     }
335
336     /**
337      * Retrieves all allowed events in the specified application context.
338      *
339      * @param context the ID of the current context.
340      * @param type the current object type.
341      * @param reference the current object reference.
342      * @return all allowed events in the specified application context.
343      * @exception com.teamkonzept.lib.TKException if an error occured during event retrieval.
344      */

345     public final TKVector getAllowedEvents (Integer JavaDoc context,
346                                             Integer JavaDoc type,
347                                             Integer JavaDoc reference)
348         throws TKException
349     {
350         Hashtable JavaDoc resolutions = new Hashtable JavaDoc();
351
352         // Resolve allowed events.
353
ResolverFactory.getInstance()
354             .getResolver(this)
355             .resolve(resolutions, context, type, reference);
356         
357         // Copy result.
358
TKVector events = new TKVector(resolutions.size());
359         events.fill(resolutions.keys());
360         
361         // Get and return the event objects.
362
return EventFactory.getInstance().getObjects(events);
363     }
364
365     /**
366      * Retrieves all policies referencing the login.
367      *
368      * @return all policies referencing the login.
369      * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
370      */

371     public final TKVector getPolicies ()
372         throws TKException
373     {
374         return PolicyFactory.getInstance()
375                               .getObjects(PolicyFactory.getInstance()
376                                                          .getPolicyProxies(this.getID(),
377                                                                            null,
378                                                                            null,
379                                                                            null));
380     }
381
382     /**
383      * Retrieves all policies referencing the login.
384      *
385      * @param context the context additionally constraining the result set.
386      * @return all policies referencing the login.
387      * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
388      */

389     public final TKVector getPolicies (Context context)
390         throws TKException
391     {
392         return PolicyFactory.getInstance()
393                               .getObjects(PolicyFactory.getInstance()
394                                                          .getPolicyProxies(this.getID(),
395                                                                            context.getID(),
396                                                                            null,
397                                                                            null));
398     }
399
400     /**
401      * Retrieves all policies referencing the login.
402      *
403      * @param context the context additionally constraining the result set.
404      * @param type the object type additionally constraining the result set.
405      * @return all policies referencing the login.
406      * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
407      */

408     public final TKVector getPolicies (Context context,
409                                        Integer JavaDoc type)
410         throws TKException
411     {
412         return PolicyFactory.getInstance()
413                               .getObjects(PolicyFactory.getInstance()
414                                                          .getPolicyProxies(this.getID(),
415                                                                            context.getID(),
416                                                                            type,
417                                                                            null));
418     }
419
420     /**
421      * Retrieves all policies referencing the login.
422      *
423      * @param context the context additionally constraining the result set.
424      * @param type the object type additionally constraining the result set.
425      * @param reference the object reference additionally constraining the result set.
426      * @return all policies referencing the login.
427      * @exception com.teamkonzept.lib.TKException if an error occured during policy retrieval.
428      */

429     public final TKVector getPolicies (Context context,
430                                        Integer JavaDoc type,
431                                        Integer JavaDoc reference)
432         throws TKException
433     {
434         return PolicyFactory.getInstance()
435                               .getObjects(PolicyFactory.getInstance()
436                                                          .getPolicyProxies(this.getID(),
437                                                                            context.getID(),
438                                                                            type,
439                                                                            reference));
440     }
441
442
443     // User or user group specific properties
444

445     /**
446      * Retrieves the login specific properties.
447      *
448      * @return the login specific properties.
449      * @exception com.teamkonzept.lib.TKException if an error occured during
450      * property retrieval.
451      */

452     public final Properties JavaDoc getProperties ()
453         throws TKException
454     {
455         // Create property container.
456
Properties JavaDoc properties = new Properties JavaDoc();
457
458         // Apply own properties.
459
applyOwnProperties(properties);
460
461         // Return own properties.
462
return properties;
463     }
464
465     /**
466      * Assigns the login specific properties.
467      *
468      * @param properties the login specific properties.
469      * @exception com.teamkonzept.lib.TKException if an error occured during
470      * property assignment.
471      */

472     public final void setProperties (Properties JavaDoc properties)
473         throws TKException
474     {
475         // Remove old properties.
476
TKVector own = PropertyFactory.getInstance()
477                                         .getProperties(this);
478
479         
480         for (int i = 0; i < own.size(); i++)
481         {
482             PropertyFactory.getInstance()
483                              .deleteProperty((Property) own.elementAt(i));
484         }
485
486         // Create new properties.
487
if (properties != null)
488         {
489             Enumeration JavaDoc keys = properties.keys();
490             String JavaDoc key = null;
491
492             while (keys.hasMoreElements())
493             {
494                 key = (String JavaDoc) keys.nextElement();
495
496                 PropertyFactory.getInstance()
497                                  .createProperty(this, key, properties.getProperty(key));
498             }
499         }
500     }
501
502     /**
503      * Retrieves the inherited properties.
504      *
505      * @return the inherited properties.
506      * @exception com.teamkonzept.lib.TKException if an error occured during
507      * property retrieval.
508      */

509     public final Properties JavaDoc getInheritedProperties ()
510         throws TKException
511     {
512         // Create property container.
513
Properties JavaDoc properties = new Properties JavaDoc();
514
515         // Apply inherited properties.
516
applyInheritedProperties(properties);
517
518         // Return inherited properties.
519
return properties;
520     }
521
522     /**
523      * Resolves the inherited properties.
524      *
525      * @param properties the inherited properties.
526      * @exception com.teamkonzept.lib.TKException if an error occured during
527      * property retrieval.
528      */

529     private final void resolveInheritedProperties (Properties JavaDoc properties)
530         throws TKException
531     {
532         // Apply inherited properties.
533
applyInheritedProperties(properties);
534
535         // Apply own properties.
536
applyOwnProperties(properties);
537         }
538     
539
540     /**
541      * Fills in the inherited properties.
542      *
543      * @param properties the inherited properties.
544      * @exception com.teamkonzept.lib.TKException if an error occured during
545      * property retrieval.
546      */

547     private final void applyInheritedProperties (Properties JavaDoc properties)
548         throws TKException
549     {
550         TKVector inherited = getParentOrder();
551
552         if (inherited != null)
553         {
554                     // jtest has some problems noticing that the method
555
// resolveInheritedProperties() is really used ...
556
Login actLogin = null;
557             for (int i = 0; i < inherited.size(); i++)
558             {
559                             actLogin = LoginFactory.getInstance()
560                                 .getLogin((Integer JavaDoc) inherited.elementAt(i));
561                             actLogin.resolveInheritedProperties(properties);
562             }
563         }
564     }
565
566     /**
567      * Fills in the inherited properties.
568      *
569      * @param properties the inherited properties.
570      * @exception com.teamkonzept.lib.TKException if an error occured during
571      * property retrieval.
572      */

573     private final void applyOwnProperties (Properties JavaDoc properties)
574         throws TKException
575     {
576         TKVector own = PropertyFactory.getInstance()
577                                         .getProperties(this);
578
579         Property property = null;
580
581         for (int i = 0; i < own.size(); i++)
582         {
583             property = (Property) own.elementAt(i);
584             properties.setProperty(property.getName(), property.getValue());
585         }
586     }
587
588 }
589
Popular Tags