001/*
002 *  Copyright 2013 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.queriesdirectory.actions.visibility;
017
018import java.util.HashMap;
019import java.util.LinkedList;
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.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.SourceResolver;
032import org.apache.commons.lang3.StringUtils;
033
034import org.ametys.core.cocoon.JSonReader;
035import org.ametys.core.group.Group;
036import org.ametys.core.group.GroupIdentity;
037import org.ametys.core.group.GroupManager;
038import org.ametys.core.user.CurrentUserProvider;
039import org.ametys.core.user.User;
040import org.ametys.core.user.UserIdentity;
041import org.ametys.core.user.UserManager;
042import org.ametys.core.user.population.UserPopulationDAO;
043import org.ametys.plugins.queriesdirectory.Query;
044import org.ametys.plugins.queriesdirectory.Query.QueryProfile;
045import org.ametys.plugins.queriesdirectory.Query.Visibility;
046import org.ametys.plugins.repository.AmetysObjectResolver;
047
048/**
049 * Action populating a map containing information about the visibility of a {@link Query}
050 */
051public class GetQueryProfileTreeAction extends ServiceableAction
052{
053    /** Ametys resolver */
054    protected AmetysObjectResolver _resolver;
055    /** Group manager */
056    protected GroupManager _groupManager;
057    /** User manager */
058    protected UserManager _userManager;
059    /** The current user provider */
060    protected CurrentUserProvider _userProvider;
061    /** The user population DAO */
062    protected UserPopulationDAO _userPopulationDAO;
063    
064    @Override
065    public void service(ServiceManager smanager) throws ServiceException
066    {
067        super.service(smanager);
068        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
069        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
070        _groupManager = (GroupManager) smanager.lookup(GroupManager.ROLE);
071        _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
072        _userPopulationDAO = (UserPopulationDAO) smanager.lookup(UserPopulationDAO.ROLE);
073    }
074    
075    @SuppressWarnings("rawtypes")
076    @Override
077    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
078    {
079        Request request = ObjectModelHelper.getRequest(objectModel);
080        
081        String queryId = request.getParameter("queryId");
082        Query query = null;
083        
084        if (StringUtils.isNotEmpty(queryId))
085        {
086            query = _resolver.resolveById(queryId);
087        }
088        
089        Map<String, Object> results = new HashMap<>();
090        
091        List<Object> profiles = new LinkedList<>();
092        results.put("children", profiles);
093        
094        // Read only profile
095        profiles.add(_getProfileInfo(query, QueryProfile.READ_ACCESS));
096        
097        // Read write profile
098        profiles.add(_getProfileInfo(query, QueryProfile.WRITE_ACCESS));
099        
100        request.setAttribute(JSonReader.OBJECT_TO_READ, results);
101        
102        return EMPTY_MAP;
103    }
104
105    private Map<String, Object> _getProfileInfo(Query query, QueryProfile profile)
106    {
107        Map<String, Object> profileMap = new HashMap<>();
108        
109        profileMap.put("type", "profile");
110        profileMap.put("id", profile.toString());
111        profileMap.put("text", profile.getTitle());
112        profileMap.put("description", profile.getDescription());
113        
114        List<Map<String, Object>> entries = new LinkedList<>();
115         
116        if (query != null)
117        {
118            // Check shared visibility + author is current user
119            boolean addInfo = Visibility.SHARED.equals(query.getVisibility()) && query.getAuthor().equals(_userProvider.getUser());
120            
121            if (addInfo)
122            {
123                Set<UserIdentity> users = query.getGrantedUsers(profile);
124                for (UserIdentity user : users)
125                {
126                    _addUserInfo(entries, user);
127                }
128                
129                Set<GroupIdentity> groups = query.getGrantedGroups(profile);
130                for (GroupIdentity group : groups)
131                {
132                    _addGroupInfo(entries, group);
133                }
134            }
135        }
136        
137        if (entries.isEmpty())
138        {
139            profileMap.put("leaf", true);
140        }
141        else
142        {
143            profileMap.put("children", entries);
144        }
145        
146        return profileMap;
147    }
148    
149    private void _addUserInfo(List<Map<String, Object>> entries, UserIdentity userIdentity)
150    {
151        String populationId = userIdentity.getPopulationId();
152        User user = _userManager.getUser(populationId, userIdentity.getLogin());
153        
154        Map<String, Object> entry = new HashMap<>();
155        entry.put("type", "user");
156        entry.put("id", UserIdentity.userIdentityToString(userIdentity));
157        entry.put("login", userIdentity.getLogin());
158        entry.put("populationId", populationId);
159        entry.put("populationLabel", _userPopulationDAO.getUserPopulation(populationId).getLabel());
160        entry.put("leaf", true);
161        
162        if (user != null)
163        {
164            entry.put("name", user.getName());
165            entry.put("fullName", user.getFullName());
166        }
167        else
168        {
169            entry.put("not-found", true);
170        }
171        
172        entries.add(entry);
173    }
174
175    private void _addGroupInfo(List<Map<String, Object>> entries, GroupIdentity groupIdentity)
176    {
177        Group group = _groupManager.getGroup(groupIdentity.getDirectoryId(), groupIdentity.getId());
178        
179        Map<String, Object> entry = new HashMap<>();
180        entry.put("type", "group");
181        entry.put("id", GroupIdentity.groupIdentityToString(groupIdentity));
182        entry.put("groupId", groupIdentity.getId());
183        entry.put("groupDirectory", groupIdentity.getDirectoryId());
184        entry.put("leaf", true);
185        
186        if (group != null)
187        {
188            entry.put("label", group.getLabel());
189        }
190        else
191        {
192            entry.put("not-found", true);
193        }
194        
195        entries.add(entry);
196    }
197}