KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > security > UserGroup


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on 03.07.2004
33  */

34 package com.nightlabs.ipanema.security;
35
36 import java.util.Collection JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Map JavaDoc;
40
41 import org.apache.log4j.Logger;
42
43 /**
44  * @author marco
45  */

46
47 /**
48  * @jdo.persistence-capable
49  * identity-type = "application"
50  * persistence-capable-superclass = "com.nightlabs.ipanema.security.User"
51  * detachable = "true"
52  *
53  * @jdo.inheritance strategy="new-table"
54  */

55 public class UserGroup extends User
56 {
57     public static Logger LOGGER = Logger.getLogger(UserGroup.class);
58     
59     public UserGroup() { }
60     
61     /**
62      * @param _userID
63      */

64     public UserGroup(String JavaDoc _userID) {
65         super(_userID);
66         if (USERTYPE_USERGROUP != getUserType())
67             throw new IllegalArgumentException JavaDoc("UserID must start with \""+USERID_PREFIX_TYPE_USERGROUP+"\" to create a user group!");
68     }
69
70     /**
71      * @param _organisationID
72      * @param _userID
73      */

74     public UserGroup(String JavaDoc _organisationID, String JavaDoc _userID) {
75         super(_organisationID, _userID);
76         if (USERTYPE_USERGROUP != getUserType())
77             throw new IllegalArgumentException JavaDoc("UserID must start with \""+USERID_PREFIX_TYPE_USERGROUP+"\" to create a user group!");
78     }
79
80     /**
81      * key: String userID<br/>
82      * value: User user
83      * <br/><br/>
84      * UserGroup (m) - (n) User
85      *
86      * @jdo.field
87      * persistence-modifier="persistent"
88      * collection-type="map"
89      * key-type="java.lang.String"
90      * value-type="User"
91      *
92      * @jdo.join
93      *
94      * @!jdo.map-vendor-extension vendor-name="jpox" key="key-field" value="userID"
95      */

96     private Map JavaDoc users = new HashMap JavaDoc();
97
98     public void addUser(User user)
99     {
100         if (user == null)
101             throw new NullPointerException JavaDoc("user must not be null!");
102
103         if (user instanceof UserGroup)
104             throw new IllegalArgumentException JavaDoc("A user group cannot contain another user group!");
105
106         if (users.get(user.getUserID()) != null)
107             return;
108
109         // Now all the roleRefs of the user must be updated.
110
// Therefore, we need to find out first, which RoleGroupRefs are granted to this
111
// user group. Thus, we iterate all UserRefs of this UserGroup.
112
for (Iterator JavaDoc itUserGroupRefs = getUserRefs().iterator(); itUserGroupRefs.hasNext(); ) {
113             UserRef userGroupRef = (UserRef) itUserGroupRefs.next();
114
115             // which RoleRefs do we have in the authority of this UserRef?
116
for (Iterator JavaDoc itRoleRefs = userGroupRef.getRoleRefs().iterator(); itRoleRefs.hasNext(); ) {
117                 RoleRef roleRef = (RoleRef)itRoleRefs.next();
118                 // To access rights for the user within an authority, we need a UserRef instance.
119
// If it does not yet exist, we create an invisible one.
120
UserRef userRef = roleRef.getAuthority()._createUserRef(user, false);
121                 userRef.incrementUserGroupReferenceCount(1);
122     
123                 // Now, we need to add the reference count.
124
userRef._addRole(roleRef.getRole(), roleRef.getReferenceCount());
125             }
126         }
127         
128         user._addUserGroup(this);
129         users.put(user.getUserID(), user);
130     }
131     
132     public void removeUser(String JavaDoc userID)
133     {
134         if (userID == null)
135             throw new NullPointerException JavaDoc("userID must not be null!");
136
137         User user = (User)users.get(userID);
138         if (user == null)
139             return;
140
141         // Now all the roleRefs of the user must be updated.
142
// Therefore, we need to find out first, which RoleGroupRefs are granted to this
143
// user group. Thus, we iterate all UserRefs of this UserGroup.
144
for (Iterator JavaDoc itUserGroupRefs = getUserRefs().iterator(); itUserGroupRefs.hasNext(); ) {
145             UserRef userGroupRef = (UserRef) itUserGroupRefs.next();
146             // which RoleRefs do we have in the authority of this UserRef?
147
for (Iterator JavaDoc itRoleRefs = userGroupRef.getRoleRefs().iterator(); itRoleRefs.hasNext(); ) {
148                 RoleRef roleRef = (RoleRef)itRoleRefs.next();
149                 // To access rights for the user within an authority, we need a UserRef instance.
150
// If it does not yet exist, we create an invisible one.
151
UserRef userRef = roleRef.getAuthority().getUserRef(userID);
152                 if(userRef == null) // TODO: no userRef for this Auhtority! Why?
153
continue;
154                     
155                 // Now, we need to subtract the reference count, if the userRef exists.
156
if (userID == null)
157                     LOGGER.warn("UserGroup \""+getUserID()+"\" exists in Authority \""+roleRef.getAuthority().getAuthorityID()+"\", but the Authority does not contain a UserRef for User \""+userID+"\"!", new IllegalStateException JavaDoc("UserRef missing."));
158                 else
159                     userRef._removeRole(roleRef.getRole(), roleRef.getReferenceCount());
160
161                 // The userRef is not used by this group anymore, thus decrement reference count.
162
userRef.decrementUserGroupReferenceCount(1);
163                 // In case, the userRef exists only for this group, we can delete it now.
164
roleRef.getAuthority()._destroyUserRef(userID, false);
165             }
166         }
167         user._removeUserGroup(this.getUserID());
168         users.remove(userID);
169     }
170
171     /**
172      * @see com.nightlabs.ipanema.security.User#_addUserRef(com.nightlabs.ipanema.security.UserRef)
173      */

174     protected void _addUserRef(UserRef userGroupRef) {
175         if (!(userGroupRef instanceof UserGroupRef))
176             throw new IllegalArgumentException JavaDoc("userGroupRef is not an instance of UserGroupRef!");
177
178         super._addUserRef(userGroupRef);
179
180         // We need to create a UserRef for all Users that are members of this group.
181
// We do NOT need to give any rights, because the UserGroupRef does that already.
182
for (Iterator JavaDoc itUsers = getUsers().iterator(); itUsers.hasNext(); ) {
183             User user = (User)itUsers.next();
184             UserRef userRef = userGroupRef.getAuthority()._createUserRef(user, false);
185             userRef.incrementUserGroupReferenceCount(1);
186         }
187
188 // // We need to add all of these rights to all users that are member of this group.
189
// for (Iterator itUsers = getUsers().iterator(); itUsers.hasNext(); ) {
190
// User user = (User)itUsers.next();
191
// UserRef userRef = userGroupRef.getAuthority()._createUserRef(user, false);
192
// for (Iterator itRoleRefs = userGroupRef.getRoleRefs().iterator(); itRoleRefs.hasNext(); ) {
193
// RoleRef roleRef = (RoleRef)itRoleRefs.next();
194
// userRef._addRole(roleRef.getRole(), roleRef.getReferenceCount());
195
// }
196
// }
197
}
198     /**
199      * @see com.nightlabs.ipanema.security.User#_removeUserRef(java.lang.String)
200      */

201     protected void _removeUserRef(String JavaDoc authorityID) {
202         UserRef userGroupRef = getUserRef(authorityID);
203         if (userGroupRef == null) {
204             LOGGER.warn("Call to _removeUserRef(\""+authorityID+"\"), but no userGroupRef existent for userGroup \""+getUserID()+"\" in this authority.", new IllegalStateException JavaDoc("UserGroupRef missing."));
205             return;
206         }
207
208         Authority authority = userGroupRef.getAuthority();
209
210         for (Iterator JavaDoc itUsers = getUsers().iterator(); itUsers.hasNext(); ) {
211             User user = (User)itUsers.next();
212             UserRef userRef = userGroupRef.getAuthority().getUserRef(user.getUserID());
213             if (userRef == null)
214                 LOGGER.warn("UserGroup \""+getUserID()+"\" existed in Authority \""+authorityID+"\", but the Authority does not contain a UserRef for User \""+user.getUserID()+"\"!", new IllegalStateException JavaDoc("UserRef missing."));
215             else {
216                 // The userRef is not used by this group anymore, thus decrement reference count.
217
userRef.decrementUserGroupReferenceCount(1);
218                 // In case, the userRef exists only for this group, we can delete it now.
219
authority._destroyUserRef(user.getUserID(), false);
220             }
221         }
222
223 //
224
// for (Iterator itUsers = getUsers().iterator(); itUsers.hasNext(); ) {
225
// User user = (User)itUsers.next();
226
// UserRef userRef = userGroupRef.getAuthority().getUserRef(user.getUserID());
227
// if (userRef == null)
228
// LOGGER.warn("UserGroup \""+getUserID()+"\" existed in Authority \""+authorityID+"\", but the Authority does not contain a UserRef for User \""+user.getUserID()+"\"!", new IllegalStateException("UserRef missing."));
229
// else {
230
// for (Iterator itRoleRefs = userGroupRef.getRoleRefs().iterator(); itRoleRefs.hasNext(); ) {
231
// RoleRef roleRef = (RoleRef)itRoleRefs.next();
232
// userRef._removeRole(roleRef.getRole(), roleRef.getReferenceCount());
233
// }
234
// }
235
// }
236

237         super._removeUserRef(authorityID);
238     }
239
240     public Collection JavaDoc getUsers()
241     {
242         return users.values();
243     }
244
245     public static final int INCLUDE_USERS = 0x4;
246     /**
247      * @see com.nightlabs.ipanema.security.User#makeTransient(int)
248      */

249     public void makeTransient(int includeMask) {
250         Map JavaDoc tmpUsers = new HashMap JavaDoc();
251
252         if ((includeMask & INCLUDE_USERS) != 0) {
253             for (Iterator JavaDoc it = users.values().iterator(); it.hasNext(); ) {
254                 User user = (User)it.next();
255                 user.makeTransient(INCLUDE_NONE);
256                 tmpUsers.put(user.getUserID(), user);
257             }
258         }
259
260         super.makeTransient(includeMask);
261
262         this.users = tmpUsers;
263     }
264 }
265
Popular Tags