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.CurrentUserProvider;
034import org.ametys.core.user.User;
035import org.ametys.core.user.UserManager;
036import org.ametys.core.user.population.UserPopulation;
037import org.ametys.core.user.population.UserPopulationDAO;
038import org.ametys.plugins.core.user.UserHelper;
039import org.ametys.plugins.repository.AmetysObjectResolver;
040import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
041import org.ametys.plugins.workspaces.project.rights.ProjectRightHelper;
042import org.ametys.runtime.authentication.AccessDeniedException;
043import org.ametys.web.repository.page.ZoneItem;
044
045import com.google.common.collect.Sets;
046
047/**
048 * Component to manager user
049 */
050public class UserComponent implements Component, Serviceable
051{
052    /** The avalon role */
053    public static final String ROLE = UserComponent.class.getName();
054    
055    private UserManager _userManager;
056    private UserPopulationDAO _userPopulationDAO;
057    private UserHelper _userHelper;
058    private AmetysObjectResolver _resolver;
059    private ProjectRightHelper _projectRightsHelper;
060    private CurrentUserProvider _currentUserProvider;
061    
062    public void service(ServiceManager manager) throws ServiceException
063    {
064        _userManager = (UserManager) manager.lookup(UserManager.ROLE);
065        _userPopulationDAO = (UserPopulationDAO) manager.lookup(UserPopulationDAO.ROLE);
066        _userHelper = (UserHelper) manager.lookup(UserHelper.ROLE);
067        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
068        _projectRightsHelper = (ProjectRightHelper) manager.lookup(ProjectRightHelper.ROLE);
069        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
070    }
071    
072    /**
073     * Search users in the given site
074     * @param siteName The site name
075     * @param count Number of results to return
076     * @param offset Offset to start results
077     * @param search Text to filter search
078     * @param zoneItemId the zoneItemId of the catalog service, used to get allowed populations
079     * @return The users as JSON
080     */
081    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
082    public Map<String, Object> getUsers(String siteName, int count, int offset, String search, String zoneItemId)
083    {
084        ZoneItem zoneItem = _resolver.resolveById(zoneItemId);
085
086        if (!_projectRightsHelper.hasCatalogReadAccess(zoneItem))
087        {
088            throw new AccessDeniedException("User '" + _currentUserProvider.getUser() + "' tried to access users' list without convenient right");
089        }
090        
091        ModelAwareDataHolder serviceDataHolder = zoneItem.getServiceParameters();
092        String[] populationIds = serviceDataHolder.getValue("populationIds") != null ? serviceDataHolder.getValue("populationIds") : new String[0];
093
094        Map<String, Object> parameters = Collections.singletonMap("pattern", search);
095        
096        List<User> usersByContext = new ArrayList<>();
097        
098        if (populationIds.length == 0)
099        {
100            Set<String> contexts = Sets.newHashSet("/sites/" + siteName, "/sites-fo/" + siteName);
101            
102            usersByContext = _userManager.getUsersByContext(contexts, count, offset, parameters, true, true);
103        }
104        else
105        {
106            List<UserPopulation> userPopulations = Arrays.stream(populationIds)
107                    .map(userPopulationId -> _userPopulationDAO.getUserPopulation(userPopulationId))
108                    .collect(Collectors.toList());
109            usersByContext = _userManager.getUsers(userPopulations, count, offset, parameters, true);
110        }
111        
112        List<Map<String, Object>> usersAsJson = usersByContext.stream()
113            .map(_userHelper::user2json)
114            .collect(Collectors.toList());
115        
116        return Collections.singletonMap("users", usersAsJson);
117    }
118}