KickJava   Java API By Example, From Geeks To Geeks.

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


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.core;
25
26 import java.io.*;
27 import java.util.*;
28
29 import org.myoodb.util.*;
30 import org.myoodb.exception.*;
31
32 public final class UserManager extends AbstractManager
33 {
34     private static boolean m_authenticationPassThroughFlag = false;
35     public final static String JavaDoc USERS = "myoodb.users";
36
37     private HashMap m_users;
38
39     public static void setAuthenticationPassThroughFlag(boolean flag)
40     {
41         m_authenticationPassThroughFlag = flag;
42     }
43
44     public static boolean getAuthenticationPassThroughFlag()
45     {
46         return m_authenticationPassThroughFlag;
47     }
48
49     public UserManager()
50     {
51         m_users = new HashMap();
52     }
53
54     public void startup() throws Exception JavaDoc
55     {
56         if (getAuthenticationPassThroughFlag() == false)
57         {
58             m_users = (HashMap) MyOodbManager.getTheManager().getUserProperties().getProperty(USERS, (Object JavaDoc) /* force myoodb get */ null);
59         }
60
61         if (m_users == null)
62         {
63             m_users = new HashMap();
64         }
65     }
66
67     public void shutdown() throws Exception JavaDoc
68     {
69     }
70
71     public User newUser(String JavaDoc username, String JavaDoc password) throws PermissionException
72     {
73         if ((username == null) || (username.equals("") == true))
74         {
75             throw new PermissionException("Invalid parameter (username is null)");
76         }
77
78         if ((password == null) /*|| (password.equals("") == true)*/)
79         {
80             throw new PermissionException("Invalid parameter (password is null)");
81         }
82
83         if (getUser(username) != null)
84         {
85             throw new PermissionException("Invalid user (username '" + username + "' already exists)");
86         }
87
88         User user = new User(username, password);
89         m_users.put(user.getName(), user);
90
91         if (getAuthenticationPassThroughFlag() == false)
92         {
93             MyOodbManager.getTheManager().getUserProperties().setProperty(USERS, m_users);
94             MyOodbManager.getTheManager().storeUserProperties();
95         }
96
97         return user;
98     }
99
100     public void renameUser(String JavaDoc oldName, String JavaDoc newName) throws PermissionException
101     {
102         if (oldName == null)
103         {
104             throw new PermissionException("Invalid rename (old username is null)");
105         }
106
107         if ((newName == null) || (newName.equals("") == true))
108         {
109             throw new PermissionException("Invalid rename (new username is null)");
110         }
111
112         User user = (User) m_users.remove(oldName);
113
114         if (user == null)
115         {
116             throw new PermissionException("Invalid rename (old user '" + oldName + "' does not exist)");
117         }
118
119         user = new User(newName, user.getPassword());
120         m_users.put(user.getName(), user);
121
122         if (getAuthenticationPassThroughFlag() == false)
123         {
124             MyOodbManager.getTheManager().getUserProperties().setProperty(USERS, m_users);
125             MyOodbManager.getTheManager().storeUserProperties();
126         }
127     }
128
129     public void updateUserPassword(String JavaDoc username, String JavaDoc password) throws PermissionException
130     {
131         if (username == null)
132         {
133             throw new PermissionException("Invalid password change (username is null)");
134         }
135
136         if (password == null /*|| password.equals("")*/)
137         {
138             throw new PermissionException("Invalid password change (password is null)");
139         }
140
141         User user = (User) m_users.get(username);
142
143         if (user == null)
144         {
145             throw new PermissionException("Invalid password change (username '" + username + "' does not exist)");
146         }
147
148         user.setPassword(password);
149
150         if (getAuthenticationPassThroughFlag() == false)
151         {
152             MyOodbManager.getTheManager().getUserProperties().setProperty(USERS, m_users);
153             MyOodbManager.getTheManager().storeUserProperties();
154         }
155     }
156
157     public void removeUser(String JavaDoc username) throws PermissionException
158     {
159         if (username == null)
160         {
161             throw new PermissionException("Invalid remove (username is null)");
162         }
163
164         User user = (User) m_users.remove(username);
165
166         if (user == null)
167         {
168             throw new PermissionException("Invalid remove (username '" + username + "' does not exist)");
169         }
170
171         if (getAuthenticationPassThroughFlag() == false)
172         {
173             MyOodbManager.getTheManager().getUserProperties().setProperty(USERS, m_users);
174             MyOodbManager.getTheManager().storeUserProperties();
175         }
176     }
177
178     public User getUser(String JavaDoc name) throws PermissionException
179     {
180         if (name == null)
181         {
182             throw new PermissionException("Invalid get (username is null)");
183         }
184
185         return (User) m_users.get(name);
186     }
187
188     public HashMap getUsers()
189     {
190         return m_users;
191     }
192 }
193
Popular Tags