001/*
002 *  Copyright 2010 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.repository;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.thread.ThreadSafe;
025import org.apache.cocoon.acting.ServiceableAction;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.SourceResolver;
028
029import org.ametys.core.user.CurrentUserProvider;
030import org.ametys.core.user.UserIdentity;
031import org.ametys.web.repository.site.Site;
032import org.ametys.web.repository.site.SiteManager;
033
034/**
035 * Determines if the given site exists.
036 */
037public class IsASiteAction extends ServiceableAction implements ThreadSafe
038{
039    private SiteManager _siteManager;
040    private CurrentUserProvider _userProvider;
041    
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
046        _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
047    }
048    
049    @Override
050    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
051    {
052        UserIdentity user = _userProvider.getUser();
053        if (_siteManager.hasSite(source))
054        {
055            if (!_siteManager.isGrantedSite(user, source))
056            {
057                getLogger().warn("The user '" + user + "' tried to access the site '" + source + "' without convenient right");
058                // user has not right
059                return null;
060            }
061            
062            // site exists
063            Site site = _siteManager.getSite(source);
064            String skin = site.getSkinId();
065            if (skin != null)
066            {
067                HashMap<String, String> result = new HashMap<>();
068                result.put("skin", skin);
069                
070                return result;
071            }
072            else
073            {
074                // Ignore and return null.
075            }
076        }
077        
078        // site does not exist
079        return null;
080    }
081}