001/* 002 * Copyright 2020 Anyware Services 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 017package org.ametys.plugins.workspaces.user; 018 019import java.util.ArrayList; 020import java.util.Arrays; 021import java.util.Collections; 022import java.util.List; 023import java.util.Map; 024import java.util.Set; 025import java.util.stream.Collectors; 026 027import org.apache.avalon.framework.component.Component; 028import org.apache.avalon.framework.service.ServiceException; 029import org.apache.avalon.framework.service.ServiceManager; 030import org.apache.avalon.framework.service.Serviceable; 031 032import org.ametys.core.ui.Callable; 033import org.ametys.core.user.User; 034import org.ametys.core.user.UserManager; 035import org.ametys.core.user.population.UserPopulation; 036import org.ametys.core.user.population.UserPopulationDAO; 037import org.ametys.plugins.core.user.UserHelper; 038import org.ametys.plugins.repository.AmetysObjectResolver; 039import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder; 040import org.ametys.web.repository.page.ZoneItem; 041 042import com.google.common.collect.Sets; 043 044/** 045 * Component to manager user 046 */ 047public class UserComponent implements Component, Serviceable 048{ 049 /** The avalon role */ 050 public static final String ROLE = UserComponent.class.getName(); 051 052 private UserManager _userManager; 053 private UserPopulationDAO _userPopulationDAO; 054 private UserHelper _userHelper; 055 private AmetysObjectResolver _resolver; 056 057 public void service(ServiceManager manager) throws ServiceException 058 { 059 _userManager = (UserManager) manager.lookup(UserManager.ROLE); 060 _userPopulationDAO = (UserPopulationDAO) manager.lookup(UserPopulationDAO.ROLE); 061 _userHelper = (UserHelper) manager.lookup(UserHelper.ROLE); 062 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 063 } 064 065 /** 066 * Search users in the given site 067 * @param siteName The site name 068 * @param count Number of results to return 069 * @param offset Offset to start results 070 * @param search Text to filter search 071 * @param zoneItemId the zoneItemId of the catalog service, used to get allowed populations 072 * @return The users as JSON 073 */ 074 @Callable 075 public Map<String, Object> getUsers(String siteName, int count, int offset, String search, String zoneItemId) 076 { 077 ZoneItem zoneItem = _resolver.resolveById(zoneItemId); 078 ModelAwareDataHolder serviceDataHolder = zoneItem.getServiceParameters(); 079 String[] populationIds = serviceDataHolder.getValue("populationIds") != null ? serviceDataHolder.getValue("populationIds") : new String[0]; 080 081 Map<String, Object> parameters = Collections.singletonMap("pattern", search); 082 083 List<User> usersByContext = new ArrayList<>(); 084 085 if (populationIds.length == 0) 086 { 087 Set<String> contexts = Sets.newHashSet("/sites/" + siteName, "/sites-fo/" + siteName); 088 089 usersByContext = _userManager.getUsersByContext(contexts, count, offset, parameters, true, true); 090 } 091 else 092 { 093 List<UserPopulation> userPopulations = Arrays.stream(populationIds) 094 .map(userPopulationId -> _userPopulationDAO.getUserPopulation(userPopulationId)) 095 .collect(Collectors.toList()); 096 usersByContext = _userManager.getUsers(userPopulations, count, offset, parameters, true); 097 } 098 099 List<Map<String, Object>> usersAsJson = usersByContext.stream() 100 .map(_userHelper::user2json) 101 .collect(Collectors.toList()); 102 103 return Collections.singletonMap("users", usersAsJson); 104 } 105}