001/*
002 *  Copyright 2016 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.blog.rights;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.logger.AbstractLogEnabled;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.cocoon.components.ContextHelper;
029import org.apache.cocoon.environment.Request;
030
031import org.ametys.core.right.RightContextConvertor;
032import org.ametys.plugins.blog.BlogConstants;
033import org.ametys.web.repository.site.Site;
034import org.ametys.web.repository.site.SiteManager;
035
036/**
037 * This implementation converts any context in a blog-type site to "/cms".
038 * Thus, having a right on the application context is considered having it on any context.
039 */
040public class BlogRightsContextConvertor extends AbstractLogEnabled implements RightContextConvertor, Serviceable, Contextualizable
041{
042    private SiteManager _siteManager;
043    
044    private Context _context;
045    
046    @Override
047    public void contextualize(Context context) throws ContextException
048    {
049        _context = context;
050    }
051    
052    @Override
053    public void service(ServiceManager smanager) throws ServiceException
054    {   
055        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
056    }
057    
058    @Override
059    public Set<Object> convert(Object object)
060    {
061        Set<Object> contexts = new HashSet<>();
062        
063        if (isBlogContext(object))
064        {
065            contexts.add("/cms");
066        }
067        
068        return contexts;
069    }
070    
071    /**
072     * Test if the context is a blog site's context.
073     * @param context the context to test.
074     * @return true if the context belongs to a blog site, false otherwise.
075     */
076    protected boolean isBlogContext(Object context)
077    {
078        if (context == null)
079        {
080            return false;
081        }
082        
083        Request request = ContextHelper.getRequest(_context);
084        String siteName = request.getParameter("siteName");
085        
086        if (siteName == null)
087        {
088            siteName = (String) request.getAttribute("siteName");
089        }
090        
091        if (siteName != null)
092        {
093            Site site = _siteManager.getSite(siteName);
094            
095            return site.getType().equals(BlogConstants.BLOG_SITE_TYPE);
096        }
097        
098        return false;
099    }
100    
101}