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 right assignement context id */
037    @SuppressWarnings("hiding")
038    public static final String ID = "org.ametys.plugins.userdirectory.right.assignment.context";
039    /** The user directory helper */
040    protected UserDirectoryHelper _userDirectoryHelper;
041    
042    /** The organisation chart page handler */
043    protected OrganisationChartPageHandler _organisationChartPageHandler;
044    
045    /** The content type helper */
046    protected ContentTypesHelper _contentTypeHelper;
047    
048    @Override
049    public void service(ServiceManager smanager) throws ServiceException
050    {
051        super.service(smanager);
052        _userDirectoryHelper = (UserDirectoryHelper) smanager.lookup(UserDirectoryHelper.ROLE);
053        _organisationChartPageHandler = (OrganisationChartPageHandler) smanager.lookup(OrganisationChartPageHandler.ROLE);
054        _contentTypeHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE);
055    }
056    
057    @Override
058    public Set<Object> getParentContexts(Object context)
059    {
060        if (context instanceof Content)
061        {
062            Content content = (Content) context;
063            if (_contentTypeHelper.isInstanceOf(content, UserDirectoryHelper.ORGUNIT_CONTENT_TYPE))
064            {
065                return Collections.singleton(_organisationChartPageHandler.getParentContent(content));
066            }
067        }
068        
069        return super.getParentContexts(context);
070    }
071    
072    @Override
073    protected Object getRootContent()
074    {
075        return _userDirectoryHelper.getUserDirectoryRootContent(true);
076    }
077
078}