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.web.rights.solrchecking;
017
018import org.apache.cocoon.environment.Request;
019
020import org.ametys.cms.rights.solrchecking.AllowedUsersActionAdditionalOperations;
021import org.ametys.core.right.AccessController;
022import org.ametys.plugins.repository.AmetysObject;
023import org.ametys.web.repository.SiteAwareAmetysObject;
024
025/**
026 * {@link AllowedUsersActionAdditionalOperations} for adding the site of the current {@link AmetysObject} (if it is a {@link SiteAwareAmetysObject}) as a request attribute.
027 * Some {@link AccessController}s are based on this request attribute value to compute their results. 
028 */
029public class AddSiteNameRequestAttribute implements AllowedUsersActionAdditionalOperations
030{
031    private static final String __SITENAME_ATTR_NAME = "siteName";
032    
033    private Object _currentSiteName;
034    
035    @Override
036    public void beforeGettingAllowedUsers(AmetysObject ametysObject, Request request)
037    {
038        _currentSiteName = request.getAttribute(__SITENAME_ATTR_NAME);
039        
040        if (ametysObject instanceof SiteAwareAmetysObject)
041        {
042            String siteName = ((SiteAwareAmetysObject) ametysObject).getSiteName();
043            request.setAttribute(__SITENAME_ATTR_NAME, siteName);
044        }
045    }
046
047    @Override
048    public void afterGettingAllowedUsers(AmetysObject ametysObject, Request request)
049    {
050        // Restore previous site name
051        request.setAttribute(__SITENAME_ATTR_NAME, _currentSiteName);
052        _currentSiteName = null;
053    }
054}