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.HashSet;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.core.right.AccessController;
026import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
027import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO;
028import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
029import org.ametys.plugins.repository.UnknownAmetysObjectException;
030
031/**
032 * {@link AccessController} for a {@link Content}
033 */
034public class SynchronizedContentAccessController extends AbstractHierarchicalAccessController<Object>
035{
036    /** The synchronize collection DAO */
037    private SynchronizableContentsCollectionDAO _collectionsDAO;
038
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        super.service(manager);
043        _collectionsDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
044    }
045    
046    @Override
047    public boolean isSupported(Object object)
048    {
049        return object instanceof Content && ((Content) object).getInternalDataHolder().hasValue(SynchronizableContentsCollection.COLLECTION_ID_DATA_NAME)
050                || object instanceof String && ((String) object).startsWith(SynchronizeContentRightAssignmentContext.ROOT_CONTEXT_PREFIX);
051    }
052    
053    @Override
054    protected Set<Object> _getParents(Object object)
055    {
056        if (object instanceof Content)
057        {
058            String[] collectionIds = ((Content) object).getInternalDataHolder().getValue(SynchronizableContentsCollection.COLLECTION_ID_DATA_NAME);
059            return Set.of(SynchronizeContentRightAssignmentContext.ROOT_CONTEXT_PREFIX + collectionIds[0]);
060        }
061        
062        return null;
063    }
064    
065    @Override
066    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
067    {
068        try
069        {
070            if (workspacesContexts.contains("/cms"))
071            {
072                Set<Object> rootContexts = new HashSet<>();
073                
074                for (SynchronizableContentsCollection synchronizableContentsCollection : _collectionsDAO.getSynchronizableContentsCollections())
075                {
076                    if (synchronizableContentsCollection.handleRightAssignmentContext())
077                    {
078                        rootContexts.add(SynchronizeContentRightAssignmentContext.ROOT_CONTEXT_PREFIX + synchronizableContentsCollection.getId());
079                    }
080                }
081                return rootContexts;
082            }
083            return null;
084        }
085        catch (UnknownAmetysObjectException e)
086        {
087            getLogger().debug("Root node for synchronized contents does not exist. Could not determine the contents root node to obtain all permissions");
088            return null;
089        }
090        
091    }
092}