001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.server.store;
020:
021: import org.lucane.server.*;
022: import org.lucane.common.concepts.*;
023:
024: import java.util.*;
025:
026: public abstract class UserStore {
027: /**
028: * Store a user
029: *
030: * @param user the UserConcept to store
031: */
032: public abstract void storeUser(UserConcept user) throws Exception;
033:
034: /**
035: * Update a user
036: *
037: * @param user the UserConcept to update
038: */
039: public abstract void updateUser(UserConcept user) throws Exception;
040:
041: /**
042: * Remove a user
043: *
044: * @param user the UserConcept to remove from the store
045: */
046: public abstract void removeUser(UserConcept user) throws Exception;
047:
048: /**
049: * Fetch a user by its login
050: *
051: * @param login the user to fetch
052: * @return the corresponding user or null
053: */
054: public abstract UserConcept getUser(String login) throws Exception;
055:
056: /**
057: * Fetch all users
058: *
059: * @return an iterator listing all users
060: */
061: public abstract Iterator getAllUsers() throws Exception;
062:
063: /**
064: * Check wether a password is correct
065: *
066: * @param user the UserConcept
067: * @param passwd the password to check
068: * @return true if the password is correct.
069: */
070: public boolean checkUserPassword(UserConcept user, String passwd) {
071: try {
072: return user.getPassword().equals(passwd);
073: } catch (Exception e) {
074: return false;
075: }
076: }
077:
078: /**
079: * Get all groups where a user is contained
080: *
081: * @param user the user
082: * @return an iterator listing all groups a user is in
083: */
084: public Iterator getAllUserGroups(UserConcept user) throws Exception {
085: ArrayList userGroups = new ArrayList();
086: GroupStore gm = Server.getInstance().getStore().getGroupStore();
087:
088: Iterator groups = gm.getAllGroups();
089: while (groups.hasNext()) {
090: GroupConcept group = (GroupConcept) groups.next();
091: if (group.hasUser(user))
092: recurseGroups(group, userGroups);
093: }
094:
095: return userGroups.iterator();
096: }
097:
098: /**
099: * Recurse parents of a group to add them to the list
100: *
101: * @param group the root group
102: * @param groups the list of groups
103: */
104: private void recurseGroups(GroupConcept group, ArrayList groups) {
105: if (group == null || groups.contains(group))
106: return;
107:
108: groups.add(group);
109: Iterator i = group.getParents();
110:
111: while (i.hasNext())
112: recurseGroups((GroupConcept) i.next(), groups);
113: }
114: }
|