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 @Override 044 public List<String> getAllGroupIds() { 045 046 List<String> companies = new ArrayList<>(); 047 for (String userId : getUM().getUserIds()) { 048 DocumentModel doc = getUM().getUserModel(userId); 049 if (doc != null) { 050 String companyName = (String) doc.getProperty(getUM().getUserSchemaName(), 051 getAttributeForGroupComputation()); 052 if (!companies.contains(companyName)) { 053 companies.add(companyName); 054 } 055 } 056 } 057 return companies; 058 } 059 060 @Override 061 public List<String> getGroupMembers(String groupName) { 062 063 Map<String, Serializable> filter = new HashMap<>(); 064 filter.put(getAttributeForGroupComputation(), groupName); 065 066 DocumentModelList users = getUM().searchUsers(filter, null); 067 068 List<String> memberIds = new ArrayList<>(); 069 070 for (DocumentModel user : users) { 071 memberIds.add(user.getId()); 072 } 073 return memberIds; 074 } 075 076 @Override 077 public List<String> getGroupsForUser(NuxeoPrincipalImpl nuxeoPrincipal) { 078 List<String> grpNames = new ArrayList<>(); 079 String property = (String) nuxeoPrincipal.getModel().getProperty(getUM().getUserSchemaName(), 080 getAttributeForGroupComputation()); 081 if (property != null && !"".equals(property.trim())) { 082 grpNames.add(property); 083 } 084 return grpNames; 085 } 086 087 @Override 088 public List<String> getParentsGroupNames(String groupName) { 089 return null; 090 } 091 092 @Override 093 public List<String> getSubGroupsNames(String groupName) { 094 return null; 095 } 096 097 @Override 098 public List<String> searchGroups(Map<String, Serializable> filter, Set<String> fulltext) { 099 100 List<String> companies = new ArrayList<>(); 101 102 String grpName = (String) filter.get(getUM().getGroupIdField()); 103 if (grpName != null) { 104 Map<String, Serializable> gFilter = new HashMap<>(); 105 Set<String> gFulltext = new HashSet<>(); 106 gFilter.put(getAttributeForGroupComputation(), grpName); 107 gFulltext.add(getAttributeForGroupComputation()); 108 for (DocumentModel userDoc : getUM().searchUsers(gFilter, gFulltext)) { 109 String companyName = (String) userDoc.getProperty(getUM().getUserSchemaName(), 110 getAttributeForGroupComputation()); 111 if (!companies.contains(companyName)) { 112 companies.add(companyName); 113 } 114 } 115 } 116 return companies; 117 } 118}