001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Core License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: UserManager.java,v 1.2 2002/06/08 00:49:38 mediumnet Exp $
008:
009: package org.ozoneDB.core;
010:
011: import java.io.*;
012: import org.ozoneDB.DxLib.*;
013: import org.ozoneDB.util.*;
014:
015: /**
016: * The UserManager holds all information about users and groups.
017: *
018: * @author <a href="http://www.softwarebuero.de/">SMB</a>
019: * @version $Revision: 1.2 $Date: 2002/06/08 00:49:38 $
020: * @see User
021: * @see Group
022: */
023: public final class UserManager extends ServerComponent {
024:
025: // magic number for streaming
026: protected final static long serialVersionUID = 2;
027: protected final static byte subSerialVersionUID = 1;
028:
029: // property names
030: public final static String GROUP_TABLE = "ozoneDB.userManager.groupTable";
031: public final static String USER_TABLE = "ozoneDB.userManager.userTable";
032:
033: protected transient Env env;
034:
035: /**
036: * All currently known users. Maps String into User.
037: */
038: protected DxMap userTable;
039:
040: /**
041: * All currently known users. Maps IDs into User.
042: */
043: protected DxMap idUserTable;
044:
045: /**
046: * All currently known groups. Maps String into Group.
047: */
048: protected DxMap groupTable;
049:
050: /**
051: * All currently known groups. Maps IDs into Group.
052: */
053: protected DxMap idGroupTable;
054:
055: /**
056: This is the userID of the system. The system has all rights and is comparable to root in
057: UNIX systems.
058: */
059: protected final static int SYSTEM_USER_ID = -1;
060:
061: /**
062: The User object of the GarbageCollector.
063: */
064: protected final static User garbageCollectorUser = new User(
065: "garbageCollector", SYSTEM_USER_ID);
066:
067: /**
068: Returns the User object of the GarbageCollector.
069: */
070: protected User getGarbageCollectorUser() {
071: return garbageCollectorUser;
072: }
073:
074: public UserManager(Env _env) {
075: super (_env);
076: env = _env;
077: groupTable = new DxHashMap();
078: userTable = new DxHashMap();
079: }
080:
081: public void startup() throws Exception {
082: env.logWriter.newEntry(this , "startup...", LogWriter.INFO);
083:
084: groupTable = (DxMap) env.state.property(GROUP_TABLE, null);
085: userTable = (DxMap) env.state.property(USER_TABLE, null);
086:
087: boolean isInitialized = true;
088: if (groupTable == null || userTable == null) {
089: env.logWriter.newEntry(this ,
090: "No state properties found. Initializing...",
091: LogWriter.INFO);
092: groupTable = new DxHashMap();
093: idGroupTable = new DxHashMap();
094: userTable = new DxHashMap();
095: isInitialized = false;
096: }
097:
098: // initialize idUserTable from the content of userTable
099: 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 adminName = System.getProperty("user.name");
117:
118: env.logWriter.newEntry(this , "admin user: " + adminName,
119: LogWriter.INFO);
120:
121: newUser(adminName, 0);
122: newGroup("admin", 0);
123: addUserToGroup(adminName, "admin");
124: }
125: }
126:
127: public void shutdown() throws Exception {
128: env.logWriter.newEntry(this , "shutdown...", LogWriter.INFO);
129: save();
130: }
131:
132: public void save() throws Exception {
133: env.state.setProperty(GROUP_TABLE, groupTable);
134: env.state.setProperty(USER_TABLE, userTable);
135: }
136:
137: public boolean checkPermission(User user,
138: ObjectContainer container, int lockLevel) {
139: if (lockLevel <= Lock.LEVEL_READ) {
140: return checkReadPermission(user, container);
141: } else {
142: return checkWritePermission(user, container);
143: }
144: }
145:
146: protected boolean checkReadPermission(User reader,
147: ObjectContainer container) {
148: // allRead can be checked fast and is true in most cases so we
149: // check it first
150: if (container.permissions().allRead()) {
151: return true;
152: } else if (container.permissions().ownerID == reader.id) {
153:
154: return true;
155: } else {
156:
157: if (container.permissions().groupRead()) {
158: User owner = userForID(container.permissions().ownerID);
159: // if reader is in any group of the owner permission is granted
160: DxIterator it = groupsOfUser(owner).iterator();
161: Group group;
162: while ((group = (Group) it.next()) != null) {
163: if (group.containsUser(reader)) {
164: return true;
165: }
166: }
167: }
168: }
169:
170: if (reader.getID() == SYSTEM_USER_ID) {
171: return true;
172: }
173:
174: return false;
175: }
176:
177: protected boolean checkWritePermission(User locker,
178: ObjectContainer container) {
179: // allRead can be checked fast and is true in most cases so we
180: // check it first
181: if (container.permissions().allLock()) {
182: return true;
183: } else if (container.permissions().ownerID == locker.id) {
184:
185: return true;
186: } else {
187:
188: if (container.permissions().groupLock()) {
189: User owner = userForID(container.permissions().ownerID);
190: // if reader is in any group of the owner permission is granted
191: DxIterator it = groupsOfUser(owner).iterator();
192: Group group;
193: while ((group = (Group) it.next()) != null) {
194: if (group.containsUser(locker)) {
195: return true;
196: }
197: }
198: }
199: }
200:
201: if (locker.getID() == SYSTEM_USER_ID) {
202: return true;
203: }
204:
205: return false;
206: }
207:
208: public void newGroup(String name, int id) throws UserManagerExc {
209: if (name == null) {
210: throw new UserManagerExc("username is null.");
211: }
212: Group group = new Group(name, id);
213:
214: if (groupForID(id) != null) {
215: throw new UserManagerExc("Group id " + id
216: + " already exists.");
217: }
218:
219: if (groupForName(name) != null) {
220: throw new UserManagerExc("Group name '" + name
221: + "' already exists.");
222: }
223:
224: groupTable.addForKey(group, name);
225: idGroupTable.addForKey(group, new Integer(id));
226:
227: setChanged();
228: }
229:
230: /**
231: * Delete the group for the given name.
232: */
233: public void removeGroup(String name) throws UserManagerExc {
234: if (name == null) {
235: throw new UserManagerExc("username is null.");
236: }
237: Group group = groupForName(name);
238:
239: if (group == null) {
240: throw new UserManagerExc("Group '" + name
241: + "' does not exist.");
242: }
243:
244: groupTable.removeForKey(group.name);
245: idGroupTable.removeForKey(new Integer(group.id));
246:
247: setChanged();
248: }
249:
250: protected DxBag groupsOfUser(User user) {
251: DxArrayBag result = new DxArrayBag();
252:
253: DxIterator it = groupTable.iterator();
254: Group group;
255: while ((group = (Group) it.next()) != null) {
256: if (group.containsUser(user)) {
257: result.add(group);
258: }
259: }
260: return result;
261: }
262:
263: public void newUser(String name, int id) throws UserManagerExc {
264: if (name == null) {
265: throw new UserManagerExc("username is null.");
266: }
267: User user = new User(name, id);
268:
269: if (userForID(id) != null) {
270: throw new UserManagerExc("User id " + id
271: + " already exists.");
272: }
273: if (userForName(name) != null) {
274: throw new UserManagerExc("User name '" + name
275: + "' already exists.");
276: }
277:
278: userTable.addForKey(user, user.name);
279: idUserTable.addForKey(user, new Integer(user.id));
280:
281: setChanged();
282: }
283:
284: public void addUserToGroup(String userName, String groupName)
285: throws UserManagerExc {
286: if (groupName == null) {
287: throw new UserManagerExc("groupname is null.");
288: }
289: if (userName == null) {
290: throw new UserManagerExc("username is null.");
291: }
292: Group group = groupForName(groupName);
293: User user = userForName(userName);
294:
295: if (group == null) {
296: throw new UserManagerExc("Group '" + groupName
297: + "' does not exist.");
298: }
299: if (user == null) {
300: throw new UserManagerExc("User '" + userName
301: + "' does not exist.");
302: }
303:
304: if (!group.addUser(user)) {
305: throw new UserManagerExc("User '" + userName
306: + "' is in this group already.");
307: }
308:
309: setChanged();
310: }
311:
312: public void removeUserFromGroup(String userName, String groupName)
313: throws UserManagerExc {
314: if (groupName == null) {
315: throw new UserManagerExc("groupname is null.");
316: }
317: if (userName == null) {
318: throw new UserManagerExc("username is null.");
319: }
320: Group group = groupForName(groupName);
321: User user = userForName(userName);
322:
323: if (group == null) {
324: throw new UserManagerExc("Group '" + groupName
325: + "' does not exist.");
326: }
327: if (user == null) {
328: throw new UserManagerExc("User '" + userName
329: + "' does not exist.");
330: }
331:
332: if (!group.containsUser(user)) {
333: throw new UserManagerExc("User '" + userName
334: + "' is not member of '" + groupName + "'.");
335: }
336:
337: group.removeUser(user);
338: setChanged();
339: }
340:
341: public void removeUser(String name) throws UserManagerExc {
342: if (name == null) {
343: throw new UserManagerExc("username is null.");
344: }
345: User user = (User) userTable.removeForKey(name);
346: if (user == null) {
347: throw new UserManagerExc("User '" + name
348: + "' does not exist.");
349: }
350:
351: idUserTable.removeForKey(new Integer(user.id));
352:
353: // remove this user from all groups
354: DxIterator it = groupsOfUser(user).iterator();
355: Group group;
356: while ((group = (Group) it.next()) != null) {
357: group.removeUser(user);
358: }
359:
360: setChanged();
361: }
362:
363: public Group groupForName(String name) throws UserManagerExc {
364: if (name == null) {
365: throw new UserManagerExc("username is null.");
366: }
367: return (Group) groupTable.elementForKey(name);
368: }
369:
370: public Group groupForID(int id) {
371: return (Group) idGroupTable.elementForKey(new Integer(id));
372: }
373:
374: public User userForName(String name) throws UserManagerExc {
375: if (name == null) {
376: throw new UserManagerExc("username is null.");
377: }
378: return (User) userTable.elementForKey(name);
379: }
380:
381: public User userForID(int id) {
382: return (User) idUserTable.elementForKey(new Integer(id));
383: }
384:
385: public DxCollection allGroups() {
386: return groupTable;
387: }
388:
389: public DxCollection allUsers() {
390: return userTable;
391: }
392: }
|