001/*
002 *  Copyright 2022 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 */
016
017package org.ametys.web.pageaccess;
018
019import java.io.UnsupportedEncodingException;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.cocoon.environment.Request;
023
024import org.ametys.cms.rights.CheckReadAccessAction;
025import org.ametys.plugins.repository.AmetysObject;
026import org.ametys.runtime.authentication.AccessDeniedException;
027import org.ametys.runtime.authentication.AuthorizationRequiredException;
028import org.ametys.web.repository.sitemap.Sitemap;
029
030/**
031 * Tests if the current sitemap has some access restrictions.<br>
032 * <ul>
033 * <li>If the sitemap is not restricted, returns EMPTY_MAP. The page can be cached.</li>
034 * <li>If the sitemap is restricted and the current user is allowed, return null. The page can be served but can't be cached.</li>
035 * <li>If the sitemap is restricted but the current user is not allowed, an {@link AccessDeniedException} is thrown.</li>
036 * <li>If the sitemap is restricted but no one is logged in, an {@link AuthorizationRequiredException} is thrown.</li>
037 * </ul>
038 */
039public class CheckSitemapReadAccessAction extends CheckReadAccessAction
040{
041    @Override
042    protected AmetysObject getAmetysObject(Parameters parameters, Request request) throws UnsupportedEncodingException
043    {
044        Sitemap sitemap = (Sitemap) request.getAttribute(Sitemap.class.getName());
045        if (sitemap != null)
046        {
047            return sitemap;
048        }
049
050        return super.getAmetysObject(parameters, request);
051    }
052}