001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade.upload;
029
030import org.opencms.configuration.CmsParameterConfiguration;
031import org.opencms.file.CmsObject;
032import org.opencms.gwt.shared.CmsUploadRestrictionInfo;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.security.CmsRole;
036
037import java.util.Map;
038
039import org.apache.commons.logging.Log;
040
041/**
042 * Default implementation for upload restrictions uses restriction entries from opencms-workplace.xml.
043 *
044 * <p>This class directly takes parameters configured via param elements in opencms-workplace.xml and interprets them as restriction
045 * entries. The parameter name is interpreted as the path, and the parameter value
046 */
047public class CmsDefaultUploadRestriction implements I_CmsUploadRestriction {
048
049    /** Logger for this class. */
050    @SuppressWarnings("unused")
051    private static final Log LOG = CmsLog.getLog(CmsDefaultUploadRestriction.class);
052
053    /** The internal CmsObject. */
054    private CmsObject m_cms;
055
056    /** The configuration. */
057    private CmsParameterConfiguration m_config = new CmsParameterConfiguration();
058
059    public static I_CmsUploadRestriction unrestricted() {
060
061        CmsDefaultUploadRestriction result = new CmsDefaultUploadRestriction();
062        result.addConfigurationParameter("/", CmsUploadRestrictionInfo.UNRESTRICTED_UPLOADS);
063        return result;
064    }
065
066    /**
067     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String)
068     */
069    public void addConfigurationParameter(String paramName, String paramValue) {
070
071        m_config.add(paramName, paramValue);
072    }
073
074    /**
075     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration()
076     */
077    public CmsParameterConfiguration getConfiguration() {
078
079        return m_config;
080    }
081
082    /**
083     * @see org.opencms.ade.upload.I_CmsUploadRestriction#getUploadRestrictionInfo(org.opencms.file.CmsObject)
084     */
085    public CmsUploadRestrictionInfo getUploadRestrictionInfo(CmsObject cms) {
086
087        if ((m_cms != null) && !OpenCms.getRoleManager().hasRole(cms, CmsRole.ROOT_ADMIN)) { // root admins may have to upload arbitrary files for things like updates / administration stuff
088            CmsUploadRestrictionInfo.Builder builder = new CmsUploadRestrictionInfo.Builder();
089            for (Map.Entry<String, String> entry : m_config.entrySet()) {
090                String key = entry.getKey();
091                String value = entry.getValue();
092                if (key.startsWith("/")) {
093                    builder.add(key, value);
094                }
095            }
096            return builder.build();
097        }
098        return new CmsUploadRestrictionInfo.Builder().add("/", CmsUploadRestrictionInfo.UNRESTRICTED_UPLOADS).build();
099    }
100
101    /**
102     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
103     */
104    public void initConfiguration() {
105
106        // do nothing
107    }
108
109    /**
110     * @see org.opencms.configuration.I_CmsNeedsAdminCmsObject#setAdminCmsObject(org.opencms.file.CmsObject)
111     */
112    public void setAdminCmsObject(CmsObject adminCms) {
113
114        m_cms = adminCms;
115    }
116
117}