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.workflow;
017
018import java.util.List;
019
020import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.cocoon.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.cms.workflow.ContentCheckRightsCondition;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.odf.data.EducationalPath;
030import org.ametys.odf.rights.ODFRightHelper;
031import org.ametys.odf.rights.ODFRightHelper.ContextualizedContent;
032import org.ametys.runtime.i18n.I18nizableText;
033
034/**
035 * Check that user is allowed for current educational path as producer of the program item.
036 */
037public class ContextualizedContentCheckRightsCondition extends ContentCheckRightsCondition implements Contextualizable
038{
039    private Context _context;
040
041    public void contextualize(Context context) throws ContextException
042    {
043        _context = context;
044    }
045    
046    @Override
047    protected boolean hasRight(UserIdentity user, String right, Object context)
048    {
049        if (context instanceof Content content)
050        {
051            Request request = ContextHelper.getRequest(_context);
052            @SuppressWarnings("unchecked")
053            List<EducationalPath> paths = (List<EducationalPath>) request.getAttribute(ODFRightHelper.REQUEST_ATTR_EDUCATIONAL_PATHS);
054            
055            if (paths != null)
056            {
057                for (EducationalPath path : paths)
058                {
059                    if (super.hasRight(user, right, new ContextualizedContent(content, path)))
060                    {
061                        return true;
062                    }
063                }
064            }
065        }
066        
067        return false;
068    }
069    
070    @Override
071    public I18nizableText getLabel()
072    {
073        return new I18nizableText("plugin.odf", "PLUGINS_ODF_WORKFLOW_CONTEXTUALIZABLE_CONTENT_CHECK_RIGHTS_CONDITION_LABEL");
074    }
075    
076    @Override
077    protected I18nizableText _getSingleConditionDescriptionKey(List<String> parameters)
078    {
079        return new I18nizableText("plugin.odf", "PLUGINS_ODF_WORKFLOW_CONTEXTUALIZABLE_CONTENT_CHECK_RIGHTS_CONDITION_DESCRIPTION", parameters);
080    }
081}