001/*
002 *  Copyright 2021 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.extraction;
017
018import java.util.Map;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.excalibur.source.SourceResolver;
024import org.apache.excalibur.source.TraversableSource;
025import org.apache.excalibur.source.impl.FileSource;
026
027import org.ametys.cms.ObservationConstants;
028import org.ametys.core.ui.ClientSideElement;
029import org.ametys.core.ui.right.ProfileAssignmentsToolClientSideElement;
030import org.ametys.core.user.UserIdentity;
031import org.ametys.plugins.extraction.execution.ExtractionDAO;
032
033/**
034 * {@link ClientSideElement} for the tool displaying the profile assignments for queries
035 */
036public class ExtractionProfileAssignmentsToolClientSideElement extends ProfileAssignmentsToolClientSideElement
037{
038
039    /** The query DAO */
040    private ExtractionDAO _extractionDAO;
041    private SourceResolver _sourceResolver;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _extractionDAO = (ExtractionDAO) smanager.lookup(ExtractionDAO.ROLE);
048        _sourceResolver = (SourceResolver) smanager.lookup(SourceResolver.ROLE);
049    }
050    
051    @Override
052    protected Map<String, Object> _getEventParams(Object context, String contextIdentifier, Set<String> profileIds)
053    {
054        Map<String, Object> eventParams = super._getEventParams(context, contextIdentifier, profileIds);
055        eventParams.put(ObservationConstants.ARGS_ACL_SOLR_CACHE_UNINFLUENTIAL, true);
056        return eventParams;
057    }
058    
059    @Override
060    protected boolean hasRight(Map<String, String> rights, Object context)
061    {
062        try
063        {
064            UserIdentity user = _currentUserProvider.getUser();
065            String absoluteFilePath = _extractionDAO.getExtractionAbsolutePathFromContext((String) context);
066            TraversableSource source = (TraversableSource) _sourceResolver.resolveURI(absoluteFilePath);
067            if (source.isCollection())
068            {
069                return _extractionDAO.canAssignRights(user, source);
070            }
071            else
072            {
073                return _extractionDAO.canAssignRights(user, source, _extractionDAO.getUserIdentityByExtractionPath((FileSource) source));
074            }
075        }
076        catch (Exception e)
077        {
078            getLogger().error("Error while reading extraction file or container with following path: " + context);
079        }
080        return false;
081    }
082}