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.cms.rights;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.cms.contenttype.ContentType;
026import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
027import org.ametys.core.right.RightAssignmentContext;
028import org.ametys.plugins.core.impl.right.StringRightAssignmentContext;
029
030/**
031 * {@link RightAssignmentContext} for assign rights to a {@link ContentType}
032 */
033public class ContentTypeRightAssignmentContext extends StringRightAssignmentContext
034{
035    /** The content type extension point */
036    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
037
038    @Override
039    public void service(ServiceManager smanager) throws ServiceException
040    {
041        super.service(smanager);
042        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
043    }
044    
045    @Override
046    public Set<Object> getParentContexts(Object context)
047    {
048        if (Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX.equals(context))
049        {
050            return null;
051        }
052        else
053        {
054            Set<Object> parents = new HashSet<>();
055            
056            String ctypeId = StringUtils.substringAfter((String) context, Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX + "/");
057            ContentType contentType = _contentTypeExtensionPoint.getExtension(ctypeId);
058            
059            String[] supertypeIds = contentType.getSupertypeIds();
060            for (int i = 0; i < supertypeIds.length; i++)
061            {
062                parents.add(Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX + "/" + supertypeIds[i]);
063            }
064            
065            if (parents.size() == 0)
066            {
067                parents.add(Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX);
068            }
069            
070            return parents;
071        }
072    }
073}