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.plugins.repository.AmetysRepositoryException;
032import org.ametys.web.repository.site.Site;
033import org.ametys.web.repository.site.SiteManager;
034
035/**
036 * Determines if the given site exists.
037 */
038public class IsASiteAction extends ServiceableAction implements ThreadSafe
039{
040    private SiteManager _siteManager;
041    private CurrentUserProvider _userProvider;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
047        _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
048    }
049    
050    @Override
051    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
052    {
053        UserIdentity user = _userProvider.getUser();
054        if (_siteManager.hasSite(source))
055        {
056            if (!_siteManager.isGrantedSite(user, source))
057            {
058                getLogger().warn("The user '" + user + "' tried to access the site '" + source + "' without convenient right");
059                // user has not right
060                return null;
061            }
062            
063            // site exists
064            Site site = _siteManager.getSite(source);
065            try
066            {
067                String skin = site.getSkinId();
068                
069                HashMap<String, String> result = new HashMap<>();
070                result.put("skin", skin);
071                
072                return result;
073            }
074            catch (AmetysRepositoryException e)
075            {
076                // Ignore and return null.
077            }
078        }
079        
080        // site does not exist
081        return null;
082    }
083}