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.Collections; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023import java.util.stream.Collectors; 024 025import org.apache.avalon.framework.component.Component; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.service.Serviceable; 029 030import org.ametys.core.ui.Callable; 031import org.ametys.core.user.User; 032import org.ametys.core.user.UserManager; 033import org.ametys.plugins.core.user.UserHelper; 034 035import com.google.common.collect.Sets; 036 037/** 038 * Component to manager user 039 */ 040public class UserComponent implements Component, Serviceable 041{ 042 /** The avalon role */ 043 public static final String ROLE = UserComponent.class.getName(); 044 045 private UserManager _userManager; 046 private UserHelper _userHelper; 047 048 public void service(ServiceManager manager) throws ServiceException 049 { 050 _userManager = (UserManager) manager.lookup(UserManager.ROLE); 051 _userHelper = (UserHelper) manager.lookup(UserHelper.ROLE); 052 } 053 054 /** 055 * Search users in the given site 056 * @param siteName The site name 057 * @param count Number of results to return 058 * @param offset Offset to start results 059 * @param search Text to filter search 060 * @return The users as JSON 061 */ 062 @Callable 063 public Map<String, Object> getUsers(String siteName, int count, int offset, String search) 064 { 065 Set<String> contexts = Sets.newHashSet("/sites/" + siteName, "/sites-fo/" + siteName); 066 Map<String, Object> parameters = Collections.singletonMap("pattern", search); 067 068 List<User> usersByContext = _userManager.getUsersByContext(contexts, count, offset, parameters, true); 069 070 List<Map<String, Object>> usersAsJson = usersByContext.stream() 071 .map(_userHelper::user2json) 072 .collect(Collectors.toList()); 073 074 return Collections.singletonMap("users", usersAsJson); 075 } 076}