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.cms.rights;
017
018import java.util.Collections;
019import java.util.Set;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.components.ContextHelper;
027import org.apache.cocoon.environment.Request;
028
029import org.ametys.cms.content.ContentHelper;
030import org.ametys.cms.content.RootContentHelper;
031import org.ametys.cms.content.archive.ArchiveConstants;
032import org.ametys.cms.repository.Content;
033import org.ametys.core.right.AccessController;
034import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
035import org.ametys.plugins.repository.AmetysObject;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037import org.ametys.plugins.repository.AmetysRepositoryException;
038import org.ametys.plugins.repository.RepositoryConstants;
039import org.ametys.plugins.repository.collection.AmetysObjectCollection;
040import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
041
042/**
043 * {@link AccessController} for a {@link Content}
044 */
045public class ContentAccessController extends AbstractHierarchicalAccessController<AmetysObject> implements Contextualizable
046{
047    /** The helper for root content */
048    protected RootContentHelper _rootContentHelper;
049    /** The helper for contents */
050    protected ContentHelper _contentHelper;
051    /** The avalon context */
052    protected Context _context;
053    /** Ametys Object Resolver */
054    protected AmetysObjectResolver _resolver;
055
056
057    public void contextualize(Context context) throws ContextException
058    {
059        _context = context;
060    }
061    
062    @Override
063    public void service(ServiceManager manager) throws ServiceException
064    {
065        super.service(manager);
066        _rootContentHelper = (RootContentHelper) manager.lookup(RootContentHelper.ROLE);
067        _contentHelper = (ContentHelper) manager.lookup(ContentHelper.ROLE);
068        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
069    }
070    
071    @Override
072    public boolean isSupported(Object object)
073    {
074        try
075        {
076            return object instanceof Content && _rootContentHelper.isChildOfRootContent((Content) object)
077                   || object != null && object.equals(_rootContentHelper.getRootContent());
078        }
079        catch (AmetysRepositoryException e)
080        {
081            getLogger().info("The contents root cannot be retrieved.", e);
082            return false;
083        }
084    }
085    
086    @Override
087    protected Set<AmetysObject> _getParents(AmetysObject object)
088    {
089        if (object instanceof Content)
090        {
091            AmetysObject parent = object.getParent();
092            if (parent instanceof AmetysObjectCollection && (RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":contents").equals(((AmetysObjectCollection) parent).getName()))
093            {
094                Request request = ContextHelper.getRequest(_context);
095                String originalWorkspace = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
096                if (ArchiveConstants.ARCHIVE_WORKSPACE.equals(originalWorkspace))
097                {
098                    try
099                    {
100                        RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE);
101                        AmetysObject parentFromDefault = _resolver.resolveByPath(parent.getPath());
102                        return Collections.singleton(parentFromDefault);
103                        
104                    }
105                    finally
106                    {
107                        RequestAttributeWorkspaceSelector.setForcedWorkspace(request, originalWorkspace);
108                    }
109                }
110            }
111            
112
113            return Collections.singleton(parent);
114        }
115        else
116        {
117            return null;
118        }
119    }
120    
121    @Override
122    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
123    {
124        if (workspacesContexts.contains("/cms"))
125        {
126            return Collections.singleton(_rootContentHelper.getRootContent());
127        }
128        return null;
129    }
130
131}