KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > api > usermgr > mmry > MmryUserMgr


1 package com.raptus.owxv3.api.usermgr.mmry;
2
3 import com.raptus.owxv3.*;
4 import com.raptus.owxv3.api.usermgr.*;
5 import java.util.*;
6
7 public class MmryUserMgr implements UserMgr
8 {
9     
10     /**
11      * The list of users
12      */

13     Vector users = null;
14     Hashtable roles = null;
15
16     Hashtable userroles = null;
17     
18     public MmryUserMgr()
19     {
20         LoggingManager.log("Created user manager!", this);
21         users = new Vector();
22         roles = new Hashtable();
23         userroles = new Hashtable();
24     }
25     
26     /**
27      * add a new user
28      *
29      * @param name the name of the user to be added
30      * @param password the password of the user
31      * @param username the login name of the user to be added
32      * @param email the email address of the new user
33      * @param locale the locale of the user
34      * @param addedby the user who added the new user
35      *
36      * @return true if user was successfully added, else returns false
37      */

38     public boolean addUser(String JavaDoc name, String JavaDoc password,String JavaDoc username,String JavaDoc email,String JavaDoc locale, User addedby)
39     {
40         MmryUser user = new MmryUser(name, username, password, email, locale);
41         if(users == null)
42         {
43             LoggingManager.log("Cannot addUser() since user is null.", this);
44         }
45         users.add(user);
46         return true;
47     }
48     
49     /** Check if user exists
50      *
51      * @param name the name of the user to be checked
52      *
53      * @return true if an user with this name was found, else return false
54      *
55      */

56     public boolean existUser(String JavaDoc name)
57     {
58         Iterator it = users.iterator();
59         while(it.hasNext())
60         {
61             User u = (User)it.next();
62             if(u.getName().equals(name))
63             {
64                 return true;
65             }
66         }
67         
68         return false;
69     }
70     
71     /** get a user specified by it's name and password
72      *
73      * @param name the user name
74      * @param password the user password
75      *
76      * @return the User if found, or null if not found
77      *
78      */

79     public User getUser(String JavaDoc name,String JavaDoc password)
80     {
81         Iterator it = users.iterator();
82         while(it.hasNext())
83         {
84             User u = (User)it.next();
85             if(u.getName().equals(name) && u.getPassword().equals(password))
86             {
87                 return u;
88             }
89         }
90         
91         return null;
92     }
93     
94     /** Return all available users
95      *
96      * @return an iterator with all users in system
97      *
98      */

99     public Iterator getUsers()
100     {
101         return users.iterator();
102     }
103     
104     /** Return the number of users in system.
105      *
106      * @return the number of users in system
107      *
108      */

109     public int getUsersCount()
110     {
111         return users.size();
112     }
113     
114     /** remove a user
115      *
116      * @param user the user to be removed
117      * @param removedby the oser who removed the specified user
118      *
119      * @return true if user was removed successfully else return false
120      *
121      */

122     public synchronized boolean removeUser(User user, User removedby)
123     {
124         /*Vector result = new Vector();
125         boolean found = false;
126         Iterator it = users.iterator();
127         while(it.hasNext())
128         {
129             User u = (User)it.next();
130             if(u.getName().equals(user.getName()))
131             {
132                 users.removeElement(u);
133                 found = true;
134             }
135             else
136             {
137                 result.add(u);
138             }
139         }
140         users = result;
141         return found;
142          */

143         /*
144         for(int i=0;i<users.size();i++)
145         {
146             User u = (User)users.get(i);
147             if(u.getUsername().equals(user.getUsername()))
148             {
149                 users.removeElementAt(i);
150                 return true;
151             }
152         }
153         
154         return false; */

155         return users.removeElement(user);
156
157     }
158     
159      /** get a user specified by it's name and password
160      *
161      * @param name the user name
162      *
163      * @return the User if found, or null if not found
164      *
165      */

166     public User getUser(String JavaDoc name)
167     {
168         Iterator it = users.iterator();
169         while(it.hasNext())
170         {
171             User u = (User)it.next();
172             if(u.getUsername().equals(name))
173             {
174                 return u;
175             }
176         }
177         
178         return null;
179     }
180    
181     /**
182      * update a user
183      *
184      * @param name the name of the user to be updaed
185      * @param password the password of the user
186      * @param username the login name of the user to be updated
187      * @param email the email address of the user
188      * @param locale the locale of the user
189      * @param addedby the user who added the user
190      *
191      * @return true if user was successfully updated, else returns false
192      */

193     public boolean updateUser(String JavaDoc name, String JavaDoc password, String JavaDoc username, String JavaDoc email, String JavaDoc locale, User addedby)
194     {
195         User u = getUser(username);
196         if(u ==null)
197         {
198             LoggingManager.log("User not found!", this);
199             return false;
200         }
201         removeUser(u, addedby);
202         LoggingManager.log("User removed!", this);
203         addUser(name, password, username, email, locale, addedby);
204         return true;
205     }
206     
207     /** Add a new role
208      *
209      * @param role the role to be added
210      * @param description the description of the new role
211      *
212      * @return true if role was successfully added, false if not
213      *
214      */

215     public boolean addRole(String JavaDoc role, String JavaDoc description)
216     {
217         if(roles.get(role)!=null)
218         {
219             LoggingManager.log("Role "+role+" already exists!", this);
220             return false;
221         }
222         Role r = (Role)new MmryRole(role, description);
223         roles.put(role, r);
224         
225         return true;
226     }
227     
228     /** Update a role
229      *
230      * @param role the role to be updated
231      * @param description the new description
232      *
233      * @return true if role was successfully updated, or false if not
234      *
235      */

236     public boolean updateRole(String JavaDoc role, String JavaDoc description)
237     {
238         MmryRole r = (MmryRole)roles.get(role);
239         if(r == null)
240         {
241             LoggingManager.log("Role not found in usermgr!", this);
242             return false;
243         }
244         r.setDescription(description);
245         return true;
246     }
247     
248     /** Return a role referenced by it's name
249      *
250      * @param name the name of the role
251      *
252      * @return a Role referenced by its name
253      *
254      */

255     public Role getRole(String JavaDoc name)
256     {
257         return (Role)roles.get(name);
258     }
259     
260     /** Remove a role
261      *
262      * @param role the role to be removed
263      *
264      * @return true if role was removed, false if not
265      *
266      */

267     public boolean removeRole(Role role)
268     {
269         Role r = (Role)roles.get(role.getName());
270         if(r == null)
271         {
272             return false;
273         }
274         roles.remove(r.getName());
275         return true;
276     }
277     
278     /** Return the roles
279      *
280      * @return an Iterator with the available roles
281      *
282      */

283     public Iterator getAllRolesAsString()
284     {
285         List l = new ArrayList();
286         Iterator it = roles.values().iterator();
287         while(it.hasNext())
288         {
289             Role r = (Role)it.next();
290             l.add(r.getName());
291         }
292         
293         return l.iterator();
294     }
295     
296     /**
297      * return all roles available in system, az Role objects
298      *
299      * @return all roles available in system, a Role objects
300      */

301     public Iterator getAllRoles()
302     {
303         return roles.values().iterator();
304     }
305     
306     /**
307      * return roles asociated to specified user, as Role objects
308      *
309      * @param u the user for who we require the roles
310      *
311      * @return all roles associated to specified user, as Role objects
312      */

313     public Iterator getRolesForUser(User u)
314     {
315         LoggingManager.log("Retrieving roles for "+u.getUsername(), this);
316         Vector v = (Vector)this.userroles.get(u.getUsername());
317         if(v == null)
318         {
319             return new ArrayList().iterator();
320         }
321         else
322         {
323             return v.iterator();
324         }
325     }
326     
327     /**
328      * return roles asociated to specified user, as String objects
329      *
330      * @param u the user for who we require the roles
331      *
332      * @return all roles associated to specified user, as String objects
333      */

334     public Iterator getRolesAsStringForUser(User u)
335     {
336         LoggingManager.log("Retrieving roles as string for user "+u.getUsername(), this);
337         Vector v = new Vector();
338         Iterator it = getRolesForUser(u);
339         while(it.hasNext())
340         {
341             Role r = (Role)it.next();
342             v.add(r.getName());
343         }
344         return v.iterator();
345         //return getAllRolesAsString();
346
}
347     
348     /**
349      * Set the roles for the specified user
350      *
351      * @param u the user for who we're setting the roles
352      * @param roles the names of the roles
353      */

354     public void setRolesForUser(User u, String JavaDoc[] roles)
355     {
356         LoggingManager.log("Setting "+roles.length+" roles for "+u.getUsername(), this);
357         Vector v = new Vector();
358         for(int i=0;i<roles.length;i++)
359         {
360             Role r = getRole(roles[i]);
361             if(r !=null)
362             {
363                 v.add(r);
364             }
365         }
366         userroles.put(u.getUsername(), v);
367     }
368     
369     /** Set the roles for the specified user
370      *
371      * @param u the user for who we're setting the roles
372      * @param roles the names of the roles
373      *
374      */

375     public void setRolesForUser(User u, List roles)
376     {
377         LoggingManager.log("Setting "+roles.size()+" roles for "+u.getUsername(), this);
378         Vector v = new Vector();
379         for(int i=0;i<roles.size();i++)
380         {
381             Role r = getRole((String JavaDoc)roles.get(i));
382             if(r !=null)
383             {
384                 v.add(r);
385             }
386         }
387         userroles.put(u.getUsername(), v);
388     }
389     
390 }
Popular Tags