001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.geonet.services.user;
025:
026: import java.util.ArrayList;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.Set;
030: import jeeves.constants.Jeeves;
031: import jeeves.interfaces.Service;
032: import jeeves.resources.dbms.Dbms;
033: import jeeves.server.ProfileManager;
034: import jeeves.server.ServiceConfig;
035: import jeeves.server.UserSession;
036: import jeeves.server.context.ServiceContext;
037: import org.fao.geonet.constants.Geonet;
038: import org.jdom.Element;
039:
040: //=============================================================================
041:
042: /** Retrieves all users in the system
043: */
044:
045: public class List implements Service {
046: //--------------------------------------------------------------------------
047: //---
048: //--- Init
049: //---
050: //--------------------------------------------------------------------------
051:
052: public void init(String appPath, ServiceConfig params)
053: throws Exception {
054: }
055:
056: //--------------------------------------------------------------------------
057: //---
058: //--- Service
059: //---
060: //--------------------------------------------------------------------------
061:
062: public Element exec(Element params, ServiceContext context)
063: throws Exception {
064: UserSession session = context.getUserSession();
065:
066: //--- retrieve groups for myself
067:
068: Dbms dbms = (Dbms) context.getResourceManager().open(
069: Geonet.Res.MAIN_DB);
070:
071: HashSet hsMyGroups = getGroups(dbms, session.getUserId(),
072: session.getProfile());
073:
074: Set profileSet = context.getProfileManager().getProfilesSet(
075: session.getProfile());
076:
077: //--- retrieve all users
078:
079: Element elUsers = dbms
080: .select("SELECT * FROM Users ORDER BY username");
081:
082: //--- now filter them
083:
084: ArrayList alToRemove = new ArrayList();
085:
086: for (Iterator i = elUsers.getChildren().iterator(); i.hasNext();) {
087: Element elRec = (Element) i.next();
088:
089: String userId = elRec.getChildText("id");
090: String profile = elRec.getChildText("profile");
091:
092: if (!hsMyGroups
093: .containsAll(getGroups(dbms, userId, profile)))
094: alToRemove.add(elRec);
095:
096: if (!profileSet.contains(profile))
097: alToRemove.add(elRec);
098: }
099:
100: //--- remove unwanted users
101:
102: for (int i = 0; i < alToRemove.size(); i++)
103: ((Element) alToRemove.get(i)).detach();
104:
105: //--- return result
106:
107: return elUsers;
108: }
109:
110: //--------------------------------------------------------------------------
111: //---
112: //--- Private methods
113: //---
114: //--------------------------------------------------------------------------
115:
116: private HashSet getGroups(Dbms dbms, String id, String profile)
117: throws Exception {
118: String query = (profile.equals(ProfileManager.ADMIN)) ? "SELECT id FROM Groups"
119: : "SELECT groupId AS id FROM UserGroups WHERE userId="
120: + id;
121:
122: java.util.List list = dbms.select(query).getChildren();
123:
124: HashSet hs = new HashSet();
125:
126: for (int i = 0; i < list.size(); i++) {
127: Element el = (Element) list.get(i);
128: hs.add(el.getChildText("id"));
129: }
130:
131: return hs;
132: }
133: }
134:
135: //=============================================================================
|