001/* 002 * Copyright 2019 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 */ 016package org.ametys.plugins.core.search; 017 018import java.util.HashMap; 019import java.util.HashSet; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.parameters.Parameters; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.avalon.framework.service.Serviceable; 028import org.apache.avalon.framework.thread.ThreadSafe; 029import org.apache.cocoon.acting.AbstractAction; 030import org.apache.cocoon.environment.ObjectModelHelper; 031import org.apache.cocoon.environment.Redirector; 032import org.apache.cocoon.environment.Request; 033import org.apache.cocoon.environment.SourceResolver; 034 035import org.ametys.core.cocoon.JSonReader; 036import org.ametys.core.user.CurrentUserProvider; 037import org.ametys.runtime.authentication.AccessDeniedException; 038 039/** 040 * Get users and groups, by context, sorted alphabetically 041 */ 042public class UserAndGroupSearchAction extends AbstractAction implements ThreadSafe, Serviceable 043{ 044 private CurrentUserProvider _userProvider; 045 046 private UserAndGroupSearchManager _userAndGroupSearchManager; 047 048 @Override 049 public void service(ServiceManager manager) throws ServiceException 050 { 051 _userProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 052 _userAndGroupSearchManager = (UserAndGroupSearchManager) manager.lookup(UserAndGroupSearchManager.ROLE); 053 } 054 055 056 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 057 { 058 if (_userProvider.getUser() == null) 059 { 060 throw new AccessDeniedException("Anonymous user tried to access to users list"); 061 } 062 063 @SuppressWarnings("unchecked") 064 Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 065 @SuppressWarnings("unchecked") 066 Set<String> contexts = new HashSet<>((List<String>) jsParameters.get("contexts")); 067 @SuppressWarnings("unchecked") 068 Map<String, Object> searchData = (Map<String, Object>) jsParameters.getOrDefault("searchData", null); 069 070 Map<String, Object> params = new HashMap<>(); 071 params.put("pattern", source); 072 params.put("sort", true); 073 int limit = parameters.getParameterAsInteger("limit", Integer.MAX_VALUE); 074 075 Map<String, Object> result = _userAndGroupSearchManager.searchUsersAndGroupByContext(contexts, limit, searchData, params); 076 077 Request request = ObjectModelHelper.getRequest(objectModel); 078 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 079 080 return EMPTY_MAP; 081 } 082}