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.plugins.userdirectory.rights;
017
018import java.util.Collections;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.contenttype.ContentTypesHelper;
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.rights.ContentRightAssignmentContext;
027import org.ametys.core.right.RightAssignmentContext;
028import org.ametys.plugins.userdirectory.OrganisationChartPageHandler;
029import org.ametys.plugins.userdirectory.UserDirectoryHelper;
030
031/**
032 * {@link RightAssignmentContext} for assign rights to a user directory {@link Content}
033 */
034public class UserDirectoryRightAssignmentContext extends ContentRightAssignmentContext
035{
036    /** The user directory helper */
037    protected UserDirectoryHelper _userDirectoryHelper;
038    
039    /** The organisation chart page handler */
040    protected OrganisationChartPageHandler _organisationChartPageHandler;
041    
042    /** The content type helper */
043    protected ContentTypesHelper _contentTypeHelper;
044    
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        super.service(smanager);
049        _userDirectoryHelper = (UserDirectoryHelper) smanager.lookup(UserDirectoryHelper.ROLE);
050        _organisationChartPageHandler = (OrganisationChartPageHandler) smanager.lookup(OrganisationChartPageHandler.ROLE);
051        _contentTypeHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE);
052    }
053    
054    @Override
055    public Set<Object> getParentContexts(Object context)
056    {
057        if (context instanceof Content)
058        {
059            Content content = (Content) context;
060            if (_contentTypeHelper.isInstanceOf(content, OrganisationChartPageHandler.ORGUNIT_CONTENT_TYPE))
061            {
062                return Collections.singleton(_organisationChartPageHandler.getParentContent(content));
063            }
064        }
065        
066        return super.getParentContexts(context);
067    }
068    
069    @Override
070    protected Object getRootContent()
071    {
072        return _userDirectoryHelper.getUserDirectoryRootContent(true);
073    }
074
075}