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.ownership;
025:
026: import java.sql.SQLException;
027: import java.util.ArrayList;
028: import java.util.HashSet;
029: import java.util.List;
030: import java.util.Set;
031: import jeeves.interfaces.Service;
032: import jeeves.resources.dbms.Dbms;
033: import jeeves.server.ServiceConfig;
034: import jeeves.server.UserSession;
035: import jeeves.server.context.ServiceContext;
036: import org.fao.geonet.constants.Geonet;
037: import org.jdom.Element;
038:
039: //=============================================================================
040:
041: public class Editors implements Service {
042: //--------------------------------------------------------------------------
043: //---
044: //--- Init
045: //---
046: //--------------------------------------------------------------------------
047:
048: public void init(String appPath, ServiceConfig config)
049: throws Exception {
050: }
051:
052: //--------------------------------------------------------------------------
053: //---
054: //--- Service
055: //---
056: //--------------------------------------------------------------------------
057:
058: public Element exec(Element params, ServiceContext context)
059: throws Exception {
060: Dbms dbms = (Dbms) context.getResourceManager().open(
061: Geonet.Res.MAIN_DB);
062:
063: UserSession us = context.getUserSession();
064: List<Element> list = getUsers(context, us, dbms);
065:
066: Element result = new Element("root");
067:
068: for (Element user : list) {
069: user = (Element) user.clone();
070: user.removeChild("password");
071: user.setName("editor");
072:
073: result.addContent(user);
074: }
075:
076: return result;
077: }
078:
079: //--------------------------------------------------------------------------
080: //---
081: //--- Private methods
082: //---
083: //--------------------------------------------------------------------------
084:
085: private List<Element> getUsers(ServiceContext context,
086: UserSession us, Dbms dbms) throws SQLException {
087: if (!us.isAuthenticated())
088: return new ArrayList<Element>();
089:
090: int id = Integer.parseInt(us.getUserId());
091:
092: String query = "SELECT DISTINCT Users.id, name, surname FROM Users, Metadata "
093: + "WHERE owner=Users.id AND profile='Editor'";
094:
095: List list = dbms.select(query).getChildren();
096:
097: if (us.getProfile().equals(Geonet.Profile.ADMINISTRATOR))
098: return (List<Element>) list;
099:
100: //--- we have a user admin
101:
102: Set<String> hsMyGroups = getUserGroups(dbms, id);
103:
104: Set profileSet = context.getProfileManager().getProfilesSet(
105: us.getProfile());
106:
107: //--- now filter them
108:
109: ArrayList<Element> newList = new ArrayList<Element>();
110:
111: for (Object o : list) {
112: Element elRec = (Element) o;
113:
114: String userId = elRec.getChildText("id");
115: String profile = elRec.getChildText("profile");
116:
117: if (profileSet.contains(profile))
118: if (hsMyGroups.containsAll(getUserGroups(dbms, Integer
119: .parseInt(userId))))
120: newList.add(elRec);
121: }
122:
123: //--- return result
124:
125: return newList;
126: }
127:
128: //--------------------------------------------------------------------------
129:
130: private Set<String> getUserGroups(Dbms dbms, int id)
131: throws SQLException {
132: String query = "SELECT groupId AS id FROM UserGroups WHERE userId=?";
133:
134: List list = dbms.select(query, id).getChildren();
135:
136: HashSet<String> hs = new HashSet<String>();
137:
138: for (int i = 0; i < list.size(); i++) {
139: Element el = (Element) list.get(i);
140: hs.add(el.getChildText("id"));
141: }
142:
143: return hs;
144: }
145: }
146:
147: //=============================================================================
|