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.cms.scripts;
017
018import java.io.File;
019import java.nio.file.Files;
020import java.nio.file.Path;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.SourceResolver;
030
031import org.ametys.core.user.CurrentUserProvider;
032import org.ametys.core.user.UserIdentity;
033import org.ametys.runtime.util.AmetysHomeHelper;
034
035/**
036 * Get a solr query string from a search tool.
037 */
038public class ReportLocationAction extends ServiceableAction
039{
040    /** The current user provider impl */
041    protected CurrentUserProvider _currentUserProvider;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
048    }
049
050    /**
051     * Get the script report directory
052     * @return The exiting report directory
053     */
054    public static final Path getScriptReportDirectory()
055    {
056        File ametysHomeTemp = AmetysHomeHelper.getAmetysHomeTmp();
057        File scriptReportDirectory = new File(ametysHomeTemp, "exports");
058        scriptReportDirectory.mkdirs();
059        
060        if (!scriptReportDirectory.isDirectory())
061        {
062            throw new RuntimeException("Cannot create the directory " + scriptReportDirectory.getAbsolutePath());
063        }
064        
065        return scriptReportDirectory.toPath();
066    }
067    
068    @Override
069    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
070    {
071        String populationId = parameters.getParameter("populationId");
072        String login = parameters.getParameter("login");
073        
074        UserIdentity currentUser = _currentUserProvider.getUser();
075        if (!currentUser.getPopulationId().equals(populationId) || !currentUser.getLogin().equals(login))
076        {
077            throw new IllegalAccessException(currentUser.toString() +  " tryed to download report of " + login + "#" + populationId);
078        }
079        
080        Path populationIdPath = getScriptReportDirectory().resolve(populationId);
081        //check if there is a folder for the requested population id
082        if (Files.isDirectory(populationIdPath))
083        {
084            Path loginPath = populationIdPath.resolve(login);
085            //check if there is a folder for the requested population id
086            if (Files.isDirectory(loginPath))
087            {
088                HashMap<String, Object> result = new HashMap<>();
089                result.put("location", loginPath.toUri().toString());
090                return result;
091            }
092        }
093        
094        return EMPTY_MAP;
095    }
096
097}
098