KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > usermanager > JahiaGroup


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 18-DEC-2001, Xo3 SA, Khue Nguyen
37  * 19-AUG-2003, Jahia Solutions Sarl, Fulco Houkes
38  *
39  * ----- END LICENSE BLOCK -----
40  */

41
42 /* ------------------------------------------------------------------------
43                                        ____.
44                            __/\ ______| |__/\. _______
45                 __ .____| | \ | +----+ \
46         _______| /--| | | - \ _ | : - \_________
47        \\______: :---| : : | : | \________>
48                |__\---\_____________:______: :____|____:_____\
49                                           /_____|
50
51                     . . . i n j a h i a w e t r u s t . . .
52
53
54     ------------------------------------------------------------------------
55
56     CHANGES:
57     18-DEC-2001 (NK) : Added properties to group
58     19-AUG-2003 (FH) : - Made the class Serializable to comply with the JCS
59                          caching system.
60                        - Javadoc fixes
61
62     ------------------------------------------------------------------------ */

63
64 package org.jahia.services.usermanager;
65
66 import org.jahia.registries.ServicesRegistry;
67
68 import java.io.Serializable JavaDoc;
69 import java.security.Principal JavaDoc;
70 import java.security.acl.Group JavaDoc;
71 import java.util.*;
72 import org.jahia.exceptions.*;
73
74 /**
75  * This class represents a group of <code>Principals</code> which can be users
76  * or groups. A group can have subgroups holding again users or subgroups.</br>
77  * </br>
78  * Each group is defined with a name, which is unique inside a site, but not
79  * necessarily unique between two sites. Each group has also a unique
80  * identification key, which is unique in the database. This way, a group is
81  * uniquely identified in all the sites and Jahia servers sharing the same
82  * database.</br>
83  * </br>
84  * The group names are used only to present an intuitive signification for the
85  * administrators of each site. As the key is the only way to assure unicity
86  * between the sites and servers, it will be used to represent the group inside
87  * of jahia and not the group name.
88  *
89  * @author Fulco Houkes
90  * @version 2.2
91  */

92 public abstract class JahiaGroup implements Group JavaDoc, Serializable JavaDoc {
93
94     private static org.apache.log4j.Logger logger =
95             org.apache.log4j.Logger.getLogger(JahiaGroup.class);
96
97     /** Group unique identification name */
98     protected String JavaDoc mGroupname;
99
100     /**
101      * Group global identification key, unique in all the sites and Jahia
102      * servers sharing the same groups data source.
103      */

104     protected String JavaDoc mGroupKey;
105
106     /** The site id */
107     protected int mSiteID;
108
109     /** Hashtable holding all the group members. Is assumed to be never null. */
110     protected Hashtable mMembers = new Hashtable ();
111
112
113     /**
114      * Get grp's properties list.
115      *
116      * @return Return a reference on the grp's properties list, or null if no
117      * property is present.
118      */

119     public abstract Properties getProperties ();
120
121
122     /**
123      * Retrieve the requested grp property.
124      *
125      * @param key Property's name.
126      *
127      * @return Return the property's value of the specified key, or null if the
128      * property does not exist.
129      */

130     public abstract String JavaDoc getProperty (String JavaDoc key);
131
132
133     /**
134      * Remove the specified property from the properties list.
135      *
136      * @param key Property's name.
137      */

138     public abstract boolean removeProperty (String JavaDoc key);
139
140
141     /**
142      * Add (or update if not already in the property list) a property key-value
143      * pair in the grp's properties list.
144      *
145      * @param key Property's name.
146      * @param value Property's value.
147      */

148     public abstract boolean setProperty (String JavaDoc key, String JavaDoc value);
149
150
151     /**
152      * Adds the specified member to the group.
153      *
154      * @param principal The principal to add to this group.
155      *
156      * @return Return true if the member was successfully added, false if the
157      * principal was already a member.
158      */

159     public abstract boolean addMember (Principal JavaDoc principal);
160
161
162     /**
163      * Compares this principal to the specified object. Returns true if the object
164      * passed in matches the principal represented by the implementation of this
165      * interface.
166      *
167      * @param another Principal to compare with.
168      *
169      * @return Return true if the principal passed in is the same as that
170      * encapsulated by this principal, and false otherwise.
171      */

172     public boolean equals (Object JavaDoc another) {
173         return (mGroupname.equals (((JahiaGroup) another).getName ()));
174     }
175
176
177     /**
178      * Return the group key.
179      *
180      * @return REturn the unique group identification key.
181      */

182     public String JavaDoc getGroupKey () {
183         return mGroupKey;
184     }
185
186
187     /**
188      * Returns the unique identifier of this principal object.
189      *
190      * @return The unique identifier of this group.
191      */

192     public String JavaDoc getName () {
193         return mGroupKey;
194     }
195
196
197     /**
198      * Returns the name of this group.
199      *
200      * @return The name of this group.
201      */

202     public String JavaDoc getGroupname () {
203         return mGroupname;
204     }
205
206
207     /**
208      * Returns the site id.
209      *
210      * @return int the siteID.
211      */

212     public int getSiteID () {
213         return mSiteID;
214     }
215
216
217     /**
218      * Returns the group's home page id.
219      * -1 : undefined
220      *
221      * @return int The group homepage id.
222      */

223     public abstract int getHomepageID ();
224
225
226     /**
227      * Set the home page id.
228      *
229      * @param id the group homepage id.
230      *
231      * @return false on error
232      */

233     public abstract boolean setHomepageID (int id);
234
235
236     /**
237      * Returns a hashcode for this principal.
238      *
239      * @return A hashcode for this principal.
240      */

241     public abstract int hashCode ();
242
243
244     /**
245      * Returns true if the passed principal is a member of the group. This method
246      * does a recursive search, so if a principal belongs to a group which is a
247      * member of this group, true is returned.
248      *
249      * @param principal The principal whose membership is to be checked.
250      *
251      * @return Return true if the principal is a member of this group, false otherwise.
252      */

253     public boolean isMember (Principal JavaDoc principal) {
254         if (principal == null) {
255             return false;
256         }
257
258         // By default, the principal is not a member of the group.
259
boolean result = false;
260
261         // Get the list of members
262
Enumeration members = mMembers.elements ();
263
264         // For each member check if it's the member we are looking for,
265
// otherwise, if the member is a group, check recursively in this group
266
// for the requested member.
267
while ((members.hasMoreElements ()) && !result) {
268             Principal JavaDoc member = (Principal JavaDoc) members.nextElement ();
269             if (member != null) {
270
271                 // check if the member is the one we are looking for
272
if (member.getName ().equals (principal.getName ())) {
273                     result = true;
274                 } else {
275
276                     // if the member is a group look for the principal in this
277
// group. Groups are already loaded.
278
if (member instanceof Group JavaDoc) {
279                         result = ((Group JavaDoc) member).isMember (principal);
280                     }
281                 }
282             }
283         }
284
285         if (result == false) {
286             /** @todo this is a temporary solution until we have implicit
287              * group implementation. Then we will have guest_provider, users_provider
288              * groups that are contained within the global users and guest groups
289              */

290             // let's now check if we are in the special case of guest and users
291
// for external sources users
292
// user could be external database user, let's look him up...
293
if ((JahiaGroupManagerService.GUEST_GROUPNAME.equals (mGroupname)) ||
294                     (JahiaGroupManagerService.USERS_GROUPNAME.equals (mGroupname))) {
295                 JahiaUser extUser = ServicesRegistry.getInstance ().getJahiaUserManagerService ()
296                         .lookupUser (principal.getName ());
297                 if (extUser != null) {
298                     if (!(extUser instanceof JahiaDBUser)) {
299                         result = true;
300                     } else if ( this instanceof JahiaDBGroup
301                         && JahiaGroupManagerService.GUEST_GROUPNAME.equals (mGroupname)
302                         && JahiaUserManagerService.GUEST_USERNAME.equals(extUser.getUsername()) ){
303                         result = true;
304                     }
305                 }
306             }
307         }
308
309         return result;
310
311         //Principal tmp = (Principal)mMembers.get(principal.getName());
312
//return (tmp != null);
313
}
314
315
316     /**
317      * Returns an enumeration of the members in the group. The returned objects
318      * can be instances of either <code>Principal</code> or <code>Group</code>.
319      * </br></br>
320      * Note that the <code>Group</code> is an instanciation of the
321      * <code>Principal</code> class.
322      *
323      * @return An enumeration of the group members.
324      */

325     public Enumeration members () {
326         return mMembers.elements ();
327     }
328
329     public List memberList() {
330         return new ArrayList(mMembers.values());
331     }
332
333
334     /**
335      * This method returns ONLY a list of users. All sub groups are expanded
336      * to return only the full list of members.
337      *
338      * @return Set a set of JahiaUsers that are all the implicit and explicit
339      * users in this group
340      */

341     public Set getRecursiveUserMembers () {
342         Set users = new HashSet ();
343
344         /** @todo this is a temporary solution until we have implicit
345          * group implementation. Then we will have guest_provider, users_provider
346          * groups that are contained within the global users and guest groups
347          */

348         // let's now check if we are in the special case of guest and users
349
// for external sources users
350
// user could be external database user, let's look him up...
351
if ((JahiaGroupManagerService.GUEST_GROUPNAME.equals (mGroupname)) ||
352                 (JahiaGroupManagerService.USERS_GROUPNAME.equals (mGroupname))) {
353             Vector userList = null;
354             try {
355                 userList = ServicesRegistry.getInstance().
356                               getJahiaSiteUserManagerService().getMembers(
357                     getSiteID());
358             } catch (JahiaException ex) {
359                 logger.error("Error while trying to retrieve full user list for site " + getSiteID(), ex);
360             }
361             if (userList != null) {
362                 users.addAll(userList);
363             }
364             // now we still need to get the list of users coming that don't
365
// belong to any site and that may come from an LDAP repository.
366
Vector userKeyList = ServicesRegistry.getInstance().getJahiaUserManagerService().getUserList();
367             if ( userKeyList != null) {
368                 Enumeration userKeyEnum = userKeyList.elements();
369                 while (userKeyEnum.hasMoreElements()) {
370                     String JavaDoc curUserKey = (String JavaDoc) userKeyEnum.nextElement();
371                     JahiaUser curUser = ServicesRegistry.getInstance().getJahiaUserManagerService().lookupUser(curUserKey);
372                     if ((curUser.getSiteID() < 1) && (!users.contains(curUser))) {
373                         // this should add all users that don't come from
374
// the database provider and that aren't root.
375
users.add(curUser);
376                     }
377                 }
378                 return users;
379             }
380         }
381
382         // Get the list of members
383
Enumeration members = mMembers.elements ();
384
385         // For each member check if it's the member we are looking for,
386
// otherwise, if the member is a group, check recursively in this group
387
// for the requested member.
388
while (members.hasMoreElements ()) {
389             Principal JavaDoc curMember = (Principal JavaDoc) members.nextElement ();
390
391             // if the member is a group look for the principal in this
392
// group. Groups are already loaded.
393
if (curMember instanceof JahiaGroup) {
394                 JahiaGroup groupMember = (JahiaGroup) curMember;
395                 users.addAll (groupMember.getRecursiveUserMembers ());
396             } else {
397                 users.add (curMember);
398             }
399         }
400
401         return users;
402     }
403
404
405     /**
406      * Removes the specified member from the group.
407      *
408      * @param principal The principal to remove from this group.
409      *
410      * @return Return true if the principal was removed, or false if the
411      * principal was not a member.
412      */

413     public abstract boolean removeMember (Principal JavaDoc principal);
414
415
416     /**
417      * Removes all members from the group.
418      *
419      * @return Return false on error
420      */

421     public boolean removeMembers () {
422
423         Enumeration members = mMembers.elements ();
424         Principal JavaDoc aMember = null;
425         while (members.hasMoreElements ()) {
426             aMember = (Principal JavaDoc) members.nextElement ();
427             removeMember (aMember);
428         }
429         return true;
430     }
431
432
433     /**
434      * Returns a string representation of this group.
435      *
436      * @return A string representation of this group.
437      */

438     public abstract String JavaDoc toString ();
439
440     /**
441      * Get the name of the provider of this group.
442      *
443      * @return String representation of the name of the provider of this group
444      */

445     public abstract String JavaDoc getProviderName ();
446
447 }
448
Popular Tags