001/*
002 *  Copyright 2017 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.web.rights;
017
018import org.apache.cocoon.components.ContextHelper;
019import org.apache.cocoon.environment.Request;
020import org.apache.commons.lang3.StringUtils;
021
022import org.ametys.cms.rights.ContentAccessController;
023import org.ametys.core.right.RightsException;
024import org.ametys.plugins.repository.RepositoryConstants;
025import org.ametys.plugins.repository.collection.AmetysObjectCollection;
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.web.WebHelper;
028import org.ametys.web.repository.content.WebContent;
029import org.ametys.web.repository.site.Site;
030
031/**
032 * The access controller for web contents only
033 */
034public class WebContentAccessController extends ContentAccessController
035{
036    @Override
037    public boolean isSupported(Object object)
038    {
039        return object instanceof WebContent
040                || object instanceof AmetysObjectCollection && (RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":contents").equals(((AmetysObjectCollection) object).getName()) && ((AmetysObjectCollection) object).getParent() instanceof Site;
041    }
042    
043    @Override
044    protected boolean _isSupportedStoredContext(Object storedObject)
045    {
046        if (isSupported(storedObject))
047        {
048            Request request = ContextHelper.getRequest(_context);
049            
050            String siteName = WebHelper.getSiteName(request);
051            
052            if (siteName != null && (storedObject instanceof WebContent c && StringUtils.equals(c.getSiteName(), siteName)
053                    || storedObject instanceof AmetysObjectCollection col && StringUtils.equals(col.getParent().getName(), siteName)))
054            {
055                return true;
056            }
057        }
058        return false;
059    }
060    
061    @Override
062    protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException
063    {
064        if (object instanceof AmetysObjectCollection)
065        {
066            return new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_ACCESS_CONTROLLER_ROOT_CONTEXT_EXPLANATION_LABEL");
067        }
068        return super.getObjectLabelForExplanation(object);
069    }
070
071    @Override
072    public I18nizableText getObjectLabel(Object object) throws RightsException
073    {
074        if (object instanceof AmetysObjectCollection)
075        {
076            return new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_ACCESS_CONTROLLER_ROOT_CONTEXT_LABEL");
077        }
078        return super.getObjectLabel(object);
079    }
080}