001/*
002 *  Copyright 2018 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.solrchecking;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.context.Context;
020import org.apache.avalon.framework.context.ContextException;
021import org.apache.avalon.framework.context.Contextualizable;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027
028import org.ametys.core.right.AllowedUsers;
029import org.ametys.core.right.RightManager;
030import org.ametys.plugins.repository.AmetysObject;
031
032/**
033 * Helper for Read ACL
034 */
035public class ReadAccessHelper implements Component, Serviceable, Contextualizable
036{
037    /** Avalon role. */
038    public static final String ROLE = ReadAccessHelper.class.getName();
039    
040    /** The right manager */
041    protected RightManager _rightManager;
042    /** The Extension Point for doing additonal operations before and after calling {@link RightManager#getReadAccessAllowedUsers(Object)} */
043    protected AllowedUsersActionAdditionalOperationsExtensionPoint _allowedUsersActionAdditionalOperationsEP;
044    
045    private Context _context;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
051        _allowedUsersActionAdditionalOperationsEP = (AllowedUsersActionAdditionalOperationsExtensionPoint) manager.lookup(AllowedUsersActionAdditionalOperationsExtensionPoint.ROLE);
052    }
053    
054    @Override
055    public void contextualize(Context context) throws ContextException
056    {
057        _context = context;
058    }
059    
060    /**
061     * Gets the allowed users for read-access to the given object
062     * @param ametysObject The object to test
063     * @return the allowed users for read-access to the given object
064     */
065    public AllowedUsers allowedUsers(AmetysObject ametysObject)
066    {
067        Request request = ContextHelper.getRequest(_context);
068        
069        _allowedUsersActionAdditionalOperationsEP.beforeGettingAllowedUsers(ametysObject, request);
070        AllowedUsers allowedUsersObj = _rightManager.getReadAccessAllowedUsers(ametysObject);
071        _allowedUsersActionAdditionalOperationsEP.afterGettingAllowedUsers(ametysObject, request);
072        
073        return allowedUsersObj;
074    }
075}