KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > UserManager


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: UserManager.java,v 1.2 2002/06/08 00:49:38 mediumnet Exp $
8

9 package org.ozoneDB.core;
10
11 import java.io.*;
12 import org.ozoneDB.DxLib.*;
13 import org.ozoneDB.util.*;
14
15
16 /**
17  * The UserManager holds all information about users and groups.
18  *
19  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
20  * @version $Revision: 1.2 $Date: 2002/06/08 00:49:38 $
21  * @see User
22  * @see Group
23  */

24 public final class UserManager extends ServerComponent {
25
26     // magic number for streaming
27
protected final static long serialVersionUID = 2;
28     protected final static byte subSerialVersionUID = 1;
29
30     // property names
31
public final static String JavaDoc GROUP_TABLE = "ozoneDB.userManager.groupTable";
32     public final static String JavaDoc USER_TABLE = "ozoneDB.userManager.userTable";
33
34     protected transient Env env;
35
36     /**
37      * All currently known users. Maps String into User.
38      */

39     protected DxMap userTable;
40
41     /**
42      * All currently known users. Maps IDs into User.
43      */

44     protected DxMap idUserTable;
45
46     /**
47      * All currently known groups. Maps String into Group.
48      */

49     protected DxMap groupTable;
50
51     /**
52      * All currently known groups. Maps IDs into Group.
53      */

54     protected DxMap idGroupTable;
55
56
57     /**
58         This is the userID of the system. The system has all rights and is comparable to root in
59         UNIX systems.
60     */

61     protected final static int SYSTEM_USER_ID = -1;
62
63     /**
64         The User object of the GarbageCollector.
65     */

66     protected final static User garbageCollectorUser = new User("garbageCollector",SYSTEM_USER_ID);
67
68     /**
69         Returns the User object of the GarbageCollector.
70     */

71     protected User getGarbageCollectorUser() {
72         return garbageCollectorUser;
73     }
74
75     public UserManager( Env _env ) {
76         super( _env );
77         env = _env;
78         groupTable = new DxHashMap();
79         userTable = new DxHashMap();
80     }
81
82
83     public void startup() throws Exception JavaDoc {
84         env.logWriter.newEntry( this, "startup...", LogWriter.INFO );
85
86         groupTable = (DxMap)env.state.property( GROUP_TABLE, null );
87         userTable = (DxMap)env.state.property( USER_TABLE, null );
88
89         boolean isInitialized = true;
90         if (groupTable == null || userTable == null) {
91             env.logWriter.newEntry( this, "No state properties found. Initializing...", LogWriter.INFO );
92             groupTable = new DxHashMap();
93             idGroupTable = new DxHashMap();
94             userTable = new DxHashMap();
95             isInitialized = false;
96         }
97
98         // initialize idUserTable from the content of userTable
99
idUserTable = new DxHashMap();
100         DxIterator it = userTable.iterator();
101         User user;
102         while ((user = (User)it.next()) != null) {
103             idUserTable.addForKey( user, user.id() );
104         }
105
106         // initialize idGroupTable from the content of groupTable
107
idGroupTable = new DxHashMap();
108         it = groupTable.iterator();
109         Group group;
110         while ((group = (Group)it.next()) != null) {
111             idGroupTable.addForKey( group, group.id() );
112         }
113
114         // add admin user and group
115
if (isInitialized == false) {
116             String JavaDoc adminName = System.getProperty( "user.name" );
117
118             env.logWriter.newEntry( this, "admin user: " + adminName, LogWriter.INFO );
119
120             newUser( adminName, 0 );
121             newGroup( "admin", 0 );
122             addUserToGroup( adminName, "admin" );
123         }
124     }
125
126
127     public void shutdown() throws Exception JavaDoc {
128         env.logWriter.newEntry( this, "shutdown...", LogWriter.INFO );
129         save();
130     }
131
132
133     public void save() throws Exception JavaDoc {
134         env.state.setProperty( GROUP_TABLE, groupTable );
135         env.state.setProperty( USER_TABLE, userTable );
136     }
137
138
139     public boolean checkPermission( User user, ObjectContainer container, int lockLevel ) {
140         if (lockLevel <= Lock.LEVEL_READ) {
141             return checkReadPermission( user, container );
142         } else {
143             return checkWritePermission( user, container );
144         }
145     }
146
147
148     protected boolean checkReadPermission( User reader, ObjectContainer container ) {
149         // allRead can be checked fast and is true in most cases so we
150
// check it first
151
if (container.permissions().allRead()) {
152             return true;
153         } else if (container.permissions().ownerID == reader.id) {
154
155             return true;
156         } else {
157
158             if (container.permissions().groupRead()) {
159                 User owner = userForID( container.permissions().ownerID );
160                 // if reader is in any group of the owner permission is granted
161
DxIterator it = groupsOfUser( owner ).iterator();
162                 Group group;
163                 while ((group = (Group)it.next()) != null) {
164                     if (group.containsUser( reader )) {
165                         return true;
166                     }
167                 }
168             }
169         }
170
171         if (reader.getID()==SYSTEM_USER_ID) {
172             return true;
173         }
174
175         return false;
176     }
177
178
179     protected boolean checkWritePermission( User locker, ObjectContainer container ) {
180         // allRead can be checked fast and is true in most cases so we
181
// check it first
182
if (container.permissions().allLock()) {
183             return true;
184         } else if (container.permissions().ownerID == locker.id) {
185
186             return true;
187         } else {
188
189             if (container.permissions().groupLock()) {
190                 User owner = userForID( container.permissions().ownerID );
191                 // if reader is in any group of the owner permission is granted
192
DxIterator it = groupsOfUser( owner ).iterator();
193                 Group group;
194                 while ((group = (Group)it.next()) != null) {
195                     if (group.containsUser( locker )) {
196                         return true;
197                     }
198                 }
199             }
200         }
201
202         if (locker.getID()==SYSTEM_USER_ID) {
203             return true;
204         }
205
206         return false;
207     }
208
209
210     public void newGroup( String JavaDoc name, int id ) throws UserManagerException {
211         if (name == null) {
212             throw new UserManagerException( "username is null." );
213         }
214         Group group = new Group( name, id );
215
216         if (groupForID( id ) != null) {
217             throw new UserManagerException( "Group id " + id + " already exists." );
218         }
219
220         if (groupForName( name ) != null) {
221             throw new UserManagerException( "Group name '" + name + "' already exists." );
222         }
223
224         groupTable.addForKey( group, name );
225         idGroupTable.addForKey( group, new Integer JavaDoc( id ) );
226
227         setChanged();
228     }
229
230
231     /**
232      * Delete the group for the given name.
233      */

234     public void removeGroup( String JavaDoc name ) throws UserManagerException {
235         if (name == null) {
236             throw new UserManagerException( "username is null." );
237         }
238         Group group = groupForName( name );
239
240         if (group == null) {
241             throw new UserManagerException( "Group '" + name + "' does not exist." );
242         }
243
244         groupTable.removeForKey( group.name );
245         idGroupTable.removeForKey( new Integer JavaDoc( group.id ) );
246
247         setChanged();
248     }
249
250
251     protected DxBag groupsOfUser( User user ) {
252         DxArrayBag result = new DxArrayBag();
253
254         DxIterator it = groupTable.iterator();
255         Group group;
256         while ((group = (Group)it.next()) != null) {
257             if (group.containsUser( user )) {
258                 result.add( group );
259             }
260         }
261         return result;
262     }
263
264
265     public void newUser( String JavaDoc name, int id ) throws UserManagerException {
266         newUser( name, name, id);
267     }
268
269
270     public void newUser( String JavaDoc name, String JavaDoc passwd, int id ) throws UserManagerException {
271         if (name == null) {
272             throw new UserManagerException( "username is null." );
273         }
274
275         if (passwd == null) {
276             passwd = name;
277         }
278
279         User user = new User( name, passwd, id );
280
281         if (userForID( id ) != null) {
282             throw new UserManagerException( "User id " + id + " already exists." );
283         }
284         if (userForName( name ) != null) {
285             throw new UserManagerException( "User name '" + name + "' already exists." );
286         }
287
288         userTable.addForKey( user, user.name );
289         idUserTable.addForKey( user, new Integer JavaDoc( user.id ) );
290
291         setChanged();
292     }
293
294
295     public void addUserToGroup( String JavaDoc userName, String JavaDoc groupName ) throws UserManagerException {
296         if (groupName == null) {
297             throw new UserManagerException( "groupname is null." );
298         }
299         if (userName == null) {
300             throw new UserManagerException( "username is null." );
301         }
302         Group group = groupForName( groupName );
303         User user = userForName( userName );
304
305         if (group == null) {
306             throw new UserManagerException( "Group '" + groupName + "' does not exist." );
307         }
308         if (user == null) {
309             throw new UserManagerException( "User '" + userName + "' does not exist." );
310         }
311
312         if (!group.addUser( user )) {
313             throw new UserManagerException( "User '" + userName + "' is in this group already." );
314         }
315
316         setChanged();
317     }
318
319
320     public void removeUserFromGroup( String JavaDoc userName, String JavaDoc groupName ) throws UserManagerException {
321         if (groupName == null) {
322             throw new UserManagerException( "groupname is null." );
323         }
324         if (userName == null) {
325             throw new UserManagerException( "username is null." );
326         }
327         Group group = groupForName( groupName );
328         User user = userForName( userName );
329
330         if (group == null) {
331             throw new UserManagerException( "Group '" + groupName + "' does not exist." );
332         }
333         if (user == null) {
334             throw new UserManagerException( "User '" + userName + "' does not exist." );
335         }
336
337         if (!group.containsUser( user )) {
338             throw new UserManagerException( "User '" + userName + "' is not member of '" + groupName + "'." );
339         }
340
341         group.removeUser( user );
342         setChanged();
343     }
344
345
346     public void removeUser( String JavaDoc name ) throws UserManagerException {
347         if (name == null) {
348             throw new UserManagerException( "username is null." );
349         }
350         User user = (User)userTable.removeForKey( name );
351         if (user == null) {
352             throw new UserManagerException( "User '" + name + "' does not exist." );
353         }
354
355         idUserTable.removeForKey( new Integer JavaDoc( user.id ) );
356
357         // remove this user from all groups
358
DxIterator it = groupsOfUser( user ).iterator();
359         Group group;
360         while ((group = (Group)it.next()) != null) {
361             group.removeUser( user );
362         }
363
364         setChanged();
365     }
366
367
368     public Group groupForName( String JavaDoc name ) throws UserManagerException {
369         if (name == null) {
370             throw new UserManagerException( "username is null." );
371         }
372         return (Group)groupTable.elementForKey( name );
373     }
374
375
376     public Group groupForID( int id ) {
377         return (Group)idGroupTable.elementForKey( new Integer JavaDoc( id ) );
378     }
379
380
381     public User userForName( String JavaDoc name ) throws UserManagerException {
382         if (name == null) {
383             throw new UserManagerException( "username is null." );
384         }
385         return (User)userTable.elementForKey( name );
386     }
387
388
389     public User userForID( int id ) {
390         return (User)idUserTable.elementForKey( new Integer JavaDoc( id ) );
391     }
392
393
394     public DxCollection allGroups() {
395         return groupTable;
396     }
397
398
399     public DxCollection allUsers() {
400         return userTable;
401     }
402 }
403
Popular Tags