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.odf.rights;
017
018import java.util.Collections;
019import java.util.HashSet;
020import java.util.List;
021import java.util.Set;
022
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.context.ContextException;
025import org.apache.avalon.framework.context.Contextualizable;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.cocoon.components.ContextHelper;
029import org.apache.cocoon.environment.Request;
030
031import org.ametys.cms.content.ContentHelper;
032import org.ametys.cms.content.archive.ArchiveConstants;
033import org.ametys.cms.repository.Content;
034import org.ametys.core.right.AccessController;
035import org.ametys.odf.ODFHelper;
036import org.ametys.odf.ProgramItem;
037import org.ametys.odf.course.Course;
038import org.ametys.odf.courselist.CourseList;
039import org.ametys.odf.coursepart.CoursePart;
040import org.ametys.odf.orgunit.OrgUnit;
041import org.ametys.odf.person.Person;
042import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
043import org.ametys.plugins.repository.AmetysObject;
044import org.ametys.plugins.repository.AmetysObjectResolver;
045import org.ametys.plugins.repository.RepositoryConstants;
046import org.ametys.plugins.repository.collection.AmetysObjectCollection;
047import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
048
049/**
050 * {@link AccessController} for a ODF {@link Content}
051 */
052public class ODFContentHierarchicalAccessController extends AbstractHierarchicalAccessController<AmetysObject> implements Contextualizable
053{
054    /** The helper for root content */
055    protected ODFHelper _odfHelper;
056    /** The helper for contents */
057    protected ContentHelper _contentHelper;
058    /** The avalon context */
059    protected Context _context;
060    /** Ametys Object Resolver */
061    protected AmetysObjectResolver _resolver;
062
063
064    public void contextualize(Context context) throws ContextException
065    {
066        _context = context;
067    }
068    
069    @Override
070    public void service(ServiceManager manager) throws ServiceException
071    {
072        super.service(manager);
073        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
074        _contentHelper = (ContentHelper) manager.lookup(ContentHelper.ROLE);
075        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
076    }
077    
078    @Override
079    public boolean isSupported(Object object)
080    {
081        return object instanceof ProgramItem || object instanceof OrgUnit || object instanceof Person || object instanceof CoursePart;
082    }
083    
084    @Override
085    protected Set<AmetysObject> _getParents(AmetysObject object)
086    {
087        if (object instanceof Content)
088        {
089            Set<AmetysObject> parents = new HashSet<>();
090            
091            if (object instanceof ProgramItem)
092            {
093                List<ProgramItem> parentProgramItems = _odfHelper.getParentProgramItems((ProgramItem) object);
094                if (parentProgramItems.size() > 0)
095                {
096                    parents.addAll(parentProgramItems);
097                }
098            }
099            else if (object instanceof Course)
100            {
101                List<CourseList> parentCls = ((Course) object).getParentCourseLists();
102                if (parentCls.size() > 0)
103                {
104                    parents.addAll(parentCls);
105                }
106            }
107            else if (object instanceof OrgUnit)
108            {
109                OrgUnit parentOrgUnit = ((OrgUnit) object).getParentOrgUnit();
110                if (parentOrgUnit != null)
111                {
112                    parents.add(parentOrgUnit);
113                }
114            }
115            else if (object instanceof CoursePart)
116            {
117                List<Course> parentCourses = ((CoursePart) object).getCourses();
118                if (!parentCourses.isEmpty())
119                {
120                    parents.addAll(parentCourses);
121                }
122            }
123            
124            // default
125            AmetysObject parent = object.getParent();
126            boolean parentAdded = false;
127            if (parent instanceof AmetysObjectCollection && (RepositoryConstants.NAMESPACE_PREFIX + ":contents").equals(((AmetysObjectCollection) parent).getName()))
128            {
129                Request request = ContextHelper.getRequest(_context);
130                String originalWorkspace = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
131                if (ArchiveConstants.ARCHIVE_WORKSPACE.equals(originalWorkspace))
132                {
133                    try
134                    {
135                        RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE);
136                        AmetysObject parentFromDefault = _resolver.resolveByPath(parent.getPath());
137                        parents.add(parentFromDefault);
138                        parentAdded = true;
139                    }
140                    finally
141                    {
142                        RequestAttributeWorkspaceSelector.setForcedWorkspace(request, originalWorkspace);
143                    }
144                }
145            }
146            if (!parentAdded)
147            {
148                parents.add(parent);
149            }
150            
151            return parents;
152        }
153        else
154        {
155            return null;
156        }
157    }
158    
159    @Override
160    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
161    {
162        if (workspacesContexts.contains("/cms"))
163        {
164            return Collections.singleton(_odfHelper.getRootContent());
165        }
166        return null;
167    }
168}