001/*
002 *  Copyright 2016 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.contentio.synchronize.rights;
017
018import java.util.Collections;
019import java.util.HashSet;
020import java.util.Set;
021
022import javax.jcr.RepositoryException;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.repository.DefaultContent;
029import org.ametys.core.right.AccessController;
030import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
031import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO;
032import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
033import org.ametys.plugins.repository.UnknownAmetysObjectException;
034
035/**
036 * {@link AccessController} for a {@link Content}
037 */
038public class SynchronizedContentAccessController extends AbstractHierarchicalAccessController<Object>
039{
040    /** The synchronize collection DAO */
041    private SynchronizableContentsCollectionDAO _collectionsDAO;
042
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _collectionsDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
048    }
049    
050    @Override
051    public boolean isSupported(Object object)
052    {
053        try
054        {
055            return object instanceof DefaultContent && ((DefaultContent) object).getNode().hasProperty(SynchronizableContentsCollection.COLLECTION_ID_PROPERTY)
056                    || object instanceof String && ((String) object).startsWith(SynchronizeContentRightAssignmentContext.ROOT_CONTEXT_PREFIX);
057        }
058        catch (RepositoryException e)
059        {
060            throw new IllegalArgumentException("Cannot get collection property for content " + ((DefaultContent) object).getId(), e);
061        }
062    }
063    
064    @Override
065    protected Set<Object> _getParents(Object object)
066    {
067        if (object instanceof DefaultContent)
068        {
069            try
070            {
071                return Collections.singleton(SynchronizeContentRightAssignmentContext.ROOT_CONTEXT_PREFIX + ((DefaultContent) object).getNode().getProperty(SynchronizableContentsCollection.COLLECTION_ID_PROPERTY).getValues()[0].getString());
072            }
073            catch (RepositoryException e)
074            {
075                throw new IllegalStateException("Cannot get parent of content " + ((DefaultContent) object).getId(), e);
076            }
077        }
078        
079        return null;
080    }
081    
082    @Override
083    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
084    {
085        try
086        {
087            if (workspacesContexts.contains("/cms"))
088            {
089                Set<Object> rootContexts = new HashSet<>();
090                
091                for (SynchronizableContentsCollection synchronizableContentsCollection : _collectionsDAO.getSynchronizableContentsCollections())
092                {
093                    if (synchronizableContentsCollection.handleRightAssignmentContext())
094                    {
095                        rootContexts.add(SynchronizeContentRightAssignmentContext.ROOT_CONTEXT_PREFIX + synchronizableContentsCollection.getId());
096                    }
097                }
098                return rootContexts;
099            }
100            return null;
101        }
102        catch (UnknownAmetysObjectException e)
103        {
104            getLogger().debug("Root node for synchronized contents does not exist. Could not determine the contents root node to obtain all permissions");
105            return null;
106        }
107        
108    }
109}