001/*
002 *  Copyright 2025 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.odf.restriction;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.model.restrictions.DefaultRestriction;
024import org.ametys.cms.model.restrictions.RestrictedModelItem;
025import org.ametys.cms.repository.Content;
026import org.ametys.odf.ODFHelper;
027import org.ametys.odf.ProgramItem;
028
029/**
030 * Restrictions for data that should be available only when the element is shared.
031 */
032public class SharedOnlyRestriction extends DefaultRestriction
033{
034    private boolean _sharedOnly;
035    private ODFHelper _odfHelper;
036
037    @Override
038    public void configure(Configuration configuration) throws ConfigurationException
039    {
040        Configuration classConfig = configuration.getChild("custom-restriction", true);
041        _sharedOnly = classConfig.getChild("shared-program-item-only").getValueAsBoolean(false);
042        super.configure(classConfig);
043    }
044    
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        super.service(smanager);
049        _odfHelper = (ODFHelper) smanager.lookup(ODFHelper.ROLE);
050    }
051    
052    @Override
053    public RestrictionResult canRead(Content content, RestrictedModelItem modelItem)
054    {
055        RestrictionResult canRead = super.canRead(content, modelItem);
056        
057        if (canRead == RestrictionResult.FALSE || _sharedOnly && content instanceof ProgramItem programItem && !_odfHelper.isShared(programItem))
058        {
059            return RestrictionResult.FALSE;
060        }
061        
062        return canRead;
063    }
064}