001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thierry Delprat 018 * * 019 */ 020package org.nuxeo.ecm.platform.computedgroups; 021 022import java.io.Serializable; 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.HashSet; 026import java.util.List; 027import java.util.Map; 028import java.util.Set; 029 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.DocumentModelList; 032import org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl; 033 034/** 035 * Base class for {@link GroupComputer} implementation that uses User attribute to compute groups. 036 * 037 * @author Thierry Delprat 038 */ 039public abstract class AbstractAttributeBasedGroupComputer extends AbstractGroupComputer { 040 041 protected abstract String getAttributeForGroupComputation(); 042 043 public List<String> getAllGroupIds() { 044 045 List<String> companies = new ArrayList<String>(); 046 for (String userId : getUM().getUserIds()) { 047 DocumentModel doc = getUM().getUserModel(userId); 048 if (doc != null) { 049 String companyName = (String) doc.getProperty(getUM().getUserSchemaName(), 050 getAttributeForGroupComputation()); 051 if (!companies.contains(companyName)) { 052 companies.add(companyName); 053 } 054 } 055 } 056 return companies; 057 } 058 059 public List<String> getGroupMembers(String groupName) { 060 061 Map<String, Serializable> filter = new HashMap<String, Serializable>(); 062 filter.put(getAttributeForGroupComputation(), groupName); 063 064 DocumentModelList users = getUM().searchUsers(filter, null); 065 066 List<String> memberIds = new ArrayList<String>(); 067 068 for (DocumentModel user : users) { 069 memberIds.add(user.getId()); 070 } 071 return memberIds; 072 } 073 074 public List<String> getGroupsForUser(NuxeoPrincipalImpl nuxeoPrincipal) { 075 List<String> grpNames = new ArrayList<String>(); 076 String property = (String) nuxeoPrincipal.getModel().getProperty(getUM().getUserSchemaName(), 077 getAttributeForGroupComputation()); 078 if (property != null && !"".equals(property.trim())) { 079 grpNames.add(property); 080 } 081 return grpNames; 082 } 083 084 public List<String> getParentsGroupNames(String groupName) { 085 return null; 086 } 087 088 public List<String> getSubGroupsNames(String groupName) { 089 return null; 090 } 091 092 @Override 093 public List<String> searchGroups(Map<String, Serializable> filter, Set<String> fulltext) { 094 095 List<String> companies = new ArrayList<String>(); 096 097 String grpName = (String) filter.get(getUM().getGroupIdField()); 098 if (grpName != null) { 099 Map<String, Serializable> gFilter = new HashMap<String, Serializable>(); 100 Set<String> gFulltext = new HashSet<String>(); 101 gFilter.put(getAttributeForGroupComputation(), grpName); 102 gFulltext.add(getAttributeForGroupComputation()); 103 for (DocumentModel userDoc : getUM().searchUsers(gFilter, gFulltext)) { 104 String companyName = (String) userDoc.getProperty(getUM().getUserSchemaName(), 105 getAttributeForGroupComputation()); 106 if (!companies.contains(companyName)) { 107 companies.add(companyName); 108 } 109 } 110 } 111 return companies; 112 } 113}