001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.core;
025:
026: import java.io.*;
027: import java.util.*;
028:
029: import org.myoodb.util.*;
030: import org.myoodb.exception.*;
031:
032: public final class UserManager extends AbstractManager {
033: public final static String USERS = "myoodb.users";
034: private static volatile boolean s_authenticationPassThroughFlag = false;
035:
036: private HashMap m_users;
037:
038: public static void setAuthenticationPassThroughFlag(boolean flag) {
039: s_authenticationPassThroughFlag = flag;
040: }
041:
042: public static boolean getAuthenticationPassThroughFlag() {
043: return s_authenticationPassThroughFlag;
044: }
045:
046: public UserManager() {
047: m_users = new HashMap();
048: }
049:
050: public void startup() throws Exception {
051: if (getAuthenticationPassThroughFlag() == false) {
052: m_users = (HashMap) MyOodbManager.getTheManager()
053: .getUserProperties().getProperty(USERS,
054: (Object) /* force myoodb get */null);
055: }
056:
057: if (m_users == null) {
058: m_users = new HashMap();
059: }
060: }
061:
062: public void shutdown() throws Exception {
063: }
064:
065: public User newUser(String username, String password)
066: throws PermissionException {
067: if ((username == null) || (username.equals("") == true)) {
068: throw new PermissionException(
069: "Invalid parameter (username is null)");
070: }
071:
072: if ((password == null) /*|| (password.equals("") == true)*/) {
073: throw new PermissionException(
074: "Invalid parameter (password is null)");
075: }
076:
077: if (getUser(username) != null) {
078: throw new PermissionException("Invalid user (username '"
079: + username + "' already exists)");
080: }
081:
082: User user = new User(username, password);
083: m_users.put(user.getName(), user);
084:
085: if (getAuthenticationPassThroughFlag() == false) {
086: MyOodbManager.getTheManager().getUserProperties()
087: .setProperty(USERS, m_users);
088: MyOodbManager.getTheManager().storeUserProperties();
089: }
090:
091: return user;
092: }
093:
094: public void renameUser(String oldName, String newName)
095: throws PermissionException {
096: if (oldName == null) {
097: throw new PermissionException(
098: "Invalid rename (old username is null)");
099: }
100:
101: if ((newName == null) || (newName.equals("") == true)) {
102: throw new PermissionException(
103: "Invalid rename (new username is null)");
104: }
105:
106: User user = (User) m_users.remove(oldName);
107:
108: if (user == null) {
109: throw new PermissionException("Invalid rename (old user '"
110: + oldName + "' does not exist)");
111: }
112:
113: user = new User(newName, user.getPassword());
114: m_users.put(user.getName(), user);
115:
116: if (getAuthenticationPassThroughFlag() == false) {
117: MyOodbManager.getTheManager().getUserProperties()
118: .setProperty(USERS, m_users);
119: MyOodbManager.getTheManager().storeUserProperties();
120: }
121: }
122:
123: public void updateUserPassword(String username, String password)
124: throws PermissionException {
125: if (username == null) {
126: throw new PermissionException(
127: "Invalid password change (username is null)");
128: }
129:
130: if (password == null /*|| password.equals("")*/) {
131: throw new PermissionException(
132: "Invalid password change (password is null)");
133: }
134:
135: User user = (User) m_users.get(username);
136:
137: if (user == null) {
138: throw new PermissionException(
139: "Invalid password change (username '" + username
140: + "' does not exist)");
141: }
142:
143: user.setPassword(password);
144:
145: if (getAuthenticationPassThroughFlag() == false) {
146: MyOodbManager.getTheManager().getUserProperties()
147: .setProperty(USERS, m_users);
148: MyOodbManager.getTheManager().storeUserProperties();
149: }
150: }
151:
152: public void removeUser(String username) throws PermissionException {
153: if (username == null) {
154: throw new PermissionException(
155: "Invalid remove (username is null)");
156: }
157:
158: User user = (User) m_users.remove(username);
159:
160: if (user == null) {
161: throw new PermissionException("Invalid remove (username '"
162: + username + "' does not exist)");
163: }
164:
165: if (getAuthenticationPassThroughFlag() == false) {
166: MyOodbManager.getTheManager().getUserProperties()
167: .setProperty(USERS, m_users);
168: MyOodbManager.getTheManager().storeUserProperties();
169: }
170: }
171:
172: public User getUser(String name) throws PermissionException {
173: if (name == null) {
174: throw new PermissionException(
175: "Invalid get (username is null)");
176: }
177:
178: return (User) m_users.get(name);
179: }
180:
181: public HashMap getUsers() {
182: return m_users;
183: }
184: }
|