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.cms.rights;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.commons.lang3.StringUtils;
028
029import org.ametys.cms.contenttype.ContentType;
030import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
031import org.ametys.core.right.AbstractStaticRightAssignmentContext;
032import org.ametys.core.right.RightAssignmentContext;
033
034/**
035 * {@link RightAssignmentContext} for assign rights to a simple {@link ContentType} or all simple contents root
036 */
037public class SimpleContentRightAssignmentContext extends AbstractStaticRightAssignmentContext
038{
039    /** The content type extension point */
040    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
041    /** Once computed, lets remember it */
042    protected Boolean _hasAnySimpleContentType;
043
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        
049        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
050    }
051    
052    @Override
053    public Object convertJSContext(Object context)
054    {
055        if (context instanceof String)
056        {
057            return context;
058        }
059        return null;
060    }
061
062    @Override
063    public String getContextIdentifier(Object context)
064    {
065        if (context instanceof String)
066        {
067            return (String) context;
068        }
069        return null;
070    }
071    
072    @Override
073    public Set<Object> getParentContexts(Object context)
074    {
075        if (SimpleContentAccessController.CONTEXT.equals(context))
076        {
077            return null;
078        }
079        else if (context instanceof String)
080        {
081            Set<Object> parents = new HashSet<>();
082            
083            String ctypeId = StringUtils.substringAfter((String) context, Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX + "/");
084            ContentType contentType = _contentTypeExtensionPoint.getExtension(ctypeId);
085            
086            String[] supertypeIds = contentType.getSupertypeIds();
087            for (int i = 0; i < supertypeIds.length; i++)
088            {
089                String supertypeId = supertypeIds[i];
090                if (_contentTypeExtensionPoint.getExtension(supertypeId).isSimple())
091                {
092                    parents.add(Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX + "/" + supertypeId);
093                }
094            }
095            
096            return parents;
097        }
098        
099        return null;
100    }
101    
102    @Override
103    public List<Object> getRootContexts(Map<String, Object> contextParameters)
104    {
105        List<Object> rootContexts = new ArrayList<>();
106        if (matchWorkspace(contextParameters))
107        {
108            rootContexts.add(SimpleContentAccessController.CONTEXT);
109        }
110        return rootContexts;
111    }
112    
113    @Override
114    public final List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
115    {
116        if (!_hasAnySimpleContentType())
117        {
118            return Collections.EMPTY_LIST;
119        }
120        else
121        {
122            return super.getScripts(ignoreRights, contextParameters);
123        }
124    }
125    
126    private boolean _hasAnySimpleContentType()
127    {
128        if (_hasAnySimpleContentType != null)
129        {
130            return _hasAnySimpleContentType;
131        }
132        
133        for (String extensionId : _contentTypeExtensionPoint.getExtensionsIds())
134        {
135            ContentType contentType = _contentTypeExtensionPoint.getExtension(extensionId);
136            if (contentType.isSimple())
137            {
138                _hasAnySimpleContentType = true;
139                return true;
140            }
141        }
142        
143        _hasAnySimpleContentType = false;
144        return false;
145    }
146}