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: import java.util.*;
024:
025: /**
026: * ServiceManager abstraction
027: */
028: public abstract class ServiceStore {
029: /**
030: * Store a service
031: *
032: * @param service the ServiceConcept to store
033: */
034: public abstract void storeService(ServiceConcept service)
035: throws Exception;
036:
037: /**
038: * Update a service
039: *
040: * @param service the ServiceConcept to update
041: */
042: public abstract void updateService(ServiceConcept service)
043: throws Exception;
044:
045: /**
046: * Remove a service
047: *
048: * @param service the ServiceConcept to remove
049: */
050: public abstract void removeService(ServiceConcept service)
051: throws Exception;
052:
053: /**
054: * Get a service by its name
055: *
056: * @param name the service to fetch
057: * @return the corresponding ServiceConcept or null
058: */
059: public abstract ServiceConcept getService(String name)
060: throws Exception;
061:
062: /**
063: * Get all services
064: *
065: * @return an iterator listing all services
066: */
067: public abstract Iterator getAllServices() throws Exception;
068:
069: /**
070: * Get the services that a user can use
071: *
072: * @param user the UserConcept
073: * @return the list of services this user can access to.
074: */
075: public Iterator getAuthorizedServices(UserConcept user)
076: throws Exception {
077: ArrayList authorizedServices = new ArrayList();
078:
079: UserStore um = Server.getInstance().getStore().getUserStore();
080: Iterator groups = um.getAllUserGroups(user);
081: while (groups.hasNext()) {
082: GroupConcept group = (GroupConcept) groups.next();
083: Iterator services = group.getServices();
084: while (services.hasNext()) {
085: ServiceConcept service = (ServiceConcept) services
086: .next();
087: if (!authorizedServices.contains(service))
088: authorizedServices.add(service);
089: }
090: }
091:
092: return authorizedServices.iterator();
093: }
094:
095: /**
096: * Check if a user can use a service
097: *
098: * @param user the UserConcept
099: * @param service the ServiceConcept
100: * @return true if the user has access to the service, false instead.
101: */
102: public boolean isAuthorizedService(UserConcept user,
103: ServiceConcept service) throws Exception {
104: Iterator i = getAuthorizedServices(user);
105: while (i.hasNext()) {
106: Object o = i.next();
107:
108: if (service.equals(o))
109: return true;
110: }
111:
112: return false;
113: }
114:
115: /**
116: * Get all users that can use a service
117: *
118: * @param service the ServiceConcept
119: * @return the list of users that have access to this service
120: */
121: public Iterator getUsersFor(ServiceConcept service)
122: throws Exception {
123: ArrayList users = new ArrayList();
124:
125: UserStore us = Server.getInstance().getStore().getUserStore();
126: Iterator i = us.getAllUsers();
127: while (i.hasNext()) {
128: UserConcept user = (UserConcept) i.next();
129: if (isAuthorizedService(user, service))
130: users.add(user);
131: }
132:
133: return users.iterator();
134: }
135: }
|