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 GmbH & Co. KG, 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.workplace.commons;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProperty;
032import org.opencms.file.CmsPropertyDefinition;
033import org.opencms.file.CmsResource;
034import org.opencms.file.CmsResourceFilter;
035import org.opencms.jsp.CmsJspActionElement;
036import org.opencms.main.CmsException;
037import org.opencms.main.CmsLog;
038import org.opencms.main.OpenCms;
039import org.opencms.security.CmsPermissionSet;
040import org.opencms.util.CmsStringUtil;
041import org.opencms.workplace.CmsDialog;
042import org.opencms.workplace.CmsWorkplaceSettings;
043
044import javax.servlet.http.HttpServletRequest;
045import javax.servlet.http.HttpServletResponse;
046import javax.servlet.jsp.JspException;
047import javax.servlet.jsp.PageContext;
048
049import org.apache.commons.logging.Log;
050
051/**
052 * Provides methods for building the security and export settings dialog.<p>
053 *
054 * The following files use this class:
055 * <ul>
056 * <li>/commons/secure.jsp
057 * </ul>
058 * <p>
059 *
060 * @since 6.0.0
061 */
062public class CmsSecure extends CmsDialog {
063
064    /** Value for the action: change the security and export setting. */
065    public static final int ACTION_CHSECEXP = 100;
066
067    /** The dialog type. */
068    public static final String DIALOG_TYPE = "secure";
069
070    /** The log object for this class. */
071    private static final Log LOG = CmsLog.getLog(CmsSecure.class);
072
073    /** Export parameter. */
074    private String m_paramExport;
075
076    /** Export name parameter. */
077    private String m_paramExportname;
078
079    /** Intern parameter. */
080    private String m_paramIntern;
081
082    /** Secure parameter. */
083    private String m_paramSecure;
084
085    /**
086     * Public constructor.<p>
087     *
088     * @param jsp an initialized JSP action element
089     */
090    public CmsSecure(CmsJspActionElement jsp) {
091
092        super(jsp);
093    }
094
095    /**
096     * Public constructor with JSP variables.<p>
097     *
098     * @param context the JSP page context
099     * @param req the JSP request
100     * @param res the JSP response
101     */
102    public CmsSecure(PageContext context, HttpServletRequest req, HttpServletResponse res) {
103
104        this(new CmsJspActionElement(context, req, res));
105    }
106
107    /**
108     * Performs the Security and Export Change.<p>
109     *
110     * @throws JspException if including a JSP sub element is not successful
111     */
112    public void actionChangeSecureExport() throws JspException {
113
114        // save initialized instance of this class in request attribute for included sub-elements
115        getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
116
117        String filename = getParamResource();
118
119        try {
120            // lock resource if auto lock is enabled
121            checkLock(getParamResource());
122
123            // write the properties
124            writeProperty(CmsPropertyDefinition.PROPERTY_EXPORT, getParamExport());
125            writeProperty(CmsPropertyDefinition.PROPERTY_EXPORTNAME, getParamExportname());
126            writeProperty(CmsPropertyDefinition.PROPERTY_SECURE, getParamSecure());
127
128            // change the flag of the resource so that it is internal
129            CmsResource resource = getCms().readResource(filename, CmsResourceFilter.IGNORE_EXPIRATION);
130            if (resource.isInternal() && !Boolean.valueOf(getParamIntern()).booleanValue()) {
131                getCms().chflags(filename, resource.getFlags() & (~CmsResource.FLAG_INTERNAL));
132            } else if (!resource.isInternal() && Boolean.valueOf(getParamIntern()).booleanValue()) {
133                getCms().chflags(filename, resource.getFlags() | CmsResource.FLAG_INTERNAL);
134            }
135
136            actionCloseDialog();
137        } catch (Throwable e) {
138            // error during change of secure settings, show error dialog
139            includeErrorpage(this, e);
140        }
141
142    }
143
144    /**
145     * Builds the radio input to set the export and secure property.
146     *
147     * @param propName the name of the property to build the radio input for
148     *
149     * @return html for the radio input
150     *
151     * @throws CmsException if the reading of a property fails
152     */
153    public String buildRadio(String propName) throws CmsException {
154
155        String propVal = readProperty(propName);
156        StringBuffer result = new StringBuffer("<table border=\"0\"><tr>");
157        result.append("<td><input type=\"radio\" value=\"true\" onClick=\"checkNoIntern()\" name=\"").append(
158            propName).append("\" ").append(Boolean.valueOf(propVal).booleanValue() ? "checked=\"checked\"" : "").append(
159                "/></td><td id=\"tablelabel\">").append(key(Messages.GUI_LABEL_TRUE_0)).append("</td>");
160        result.append("<td><input type=\"radio\" value=\"false\" onClick=\"checkNoIntern()\" name=\"").append(
161            propName).append("\" ").append(Boolean.valueOf(propVal).booleanValue() ? "" : "checked=\"checked\"").append(
162                "/></td><td id=\"tablelabel\">").append(key(Messages.GUI_LABEL_FALSE_0)).append("</td>");
163        result.append("<td><input type=\"radio\" value=\"\" onClick=\"checkNoIntern()\" name=\"").append(
164            propName).append("\" ").append(CmsStringUtil.isEmpty(propVal) ? "checked=\"checked\"" : "").append(
165                "/></td><td id=\"tablelabel\">").append(getPropertyInheritanceInfo(propName)).append(
166                    "</td></tr></table>");
167        return result.toString();
168    }
169
170    /**
171     * Returns the value of the export parameter.<p>
172     *
173     * @return the value of the export parameter
174     */
175    public String getParamExport() {
176
177        return m_paramExport;
178    }
179
180    /**
181     * Returns the value of the export name parameter.<p>
182     *
183     * @return the value of the export name parameter
184     */
185    public String getParamExportname() {
186
187        return m_paramExportname;
188    }
189
190    /**
191     * Returns the value of the intern parameter.<p>
192     *
193     * @return the value of the intern parameter
194     */
195    public String getParamIntern() {
196
197        return m_paramIntern;
198    }
199
200    /**
201     * Returns the value of the secure parameter.<p>
202     *
203     * @return the value of the secure parameter
204     */
205    public String getParamSecure() {
206
207        return m_paramSecure;
208    }
209
210    /**
211     * Returns the information from which the property is inherited.<p>
212     *
213     * @param propName the name of the property
214     * @return a String containing the information from which the property is inherited and inherited value
215     * @throws CmsException if the reading of the Property fails
216     */
217    public String getPropertyInheritanceInfo(String propName) throws CmsException {
218
219        String folderName = CmsResource.getParentFolder(getParamResource());
220        String folderPropVal = null;
221        while (CmsStringUtil.isNotEmpty(folderName)) {
222            CmsProperty prop = getCms().readPropertyObject(folderName, propName, false);
223            folderPropVal = prop.getValue();
224            if (CmsStringUtil.isNotEmpty(folderPropVal)) {
225                break;
226            }
227            folderName = CmsResource.getParentFolder(folderName);
228        }
229
230        if (CmsStringUtil.isNotEmpty(folderPropVal)) {
231            return key(Messages.GUI_SECURE_INHERIT_FROM_2, new Object[] {folderPropVal, folderName});
232        } else {
233            return key(Messages.GUI_SECURE_NOT_SET_0);
234        }
235    }
236
237    /**
238     * Returns the path under which the resource is accessible.<p>
239     *
240     * @return the path under which the resource is accessible
241     */
242    public String getResourceUrl() {
243
244        return OpenCms.getLinkManager().getOnlineLink(getCms(), getParamResource());
245    }
246
247    /**
248     * Returns true if the export user has read permission on a specified resource.<p>
249     *
250     * @return true, if the export user has the permission to read the resource
251     */
252    public boolean exportUserHasReadPermission() {
253
254        String vfsName = getParamResource();
255        CmsObject cms = getCms();
256        try {
257            // static export must always be checked with the export users permissions,
258            // not the current users permissions
259            CmsObject exportCms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport());
260            exportCms.getRequestContext().setSiteRoot(getCms().getRequestContext().getSiteRoot());
261            // let's look up if the export user has the permission to read
262            return exportCms.hasPermissions(
263                cms.readResource(vfsName, CmsResourceFilter.IGNORE_EXPIRATION),
264                CmsPermissionSet.ACCESS_READ);
265        } catch (CmsException e) {
266            // ignore this exception
267        }
268        return false;
269    }
270
271    /**
272     * Returns value of the the intern property of the resource.<p>
273     *
274     * @return the value of the intern property of the resource
275     */
276    public String readInternProp() {
277
278        boolean internProp = false;
279        try {
280            internProp = getCms().readResource(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION).isInternal();
281        } catch (CmsException e) {
282            if (LOG.isInfoEnabled()) {
283                LOG.info(e.getLocalizedMessage());
284            }
285        }
286        return String.valueOf(internProp);
287    }
288
289    /**
290     * Returns value of the property of the resource.<p>
291     *
292     * @param propertyName the name of the property to read
293     *
294     * @return the value of the secure property of the resource
295     */
296    public String readProperty(String propertyName) {
297
298        String propVal = null;
299        try {
300            propVal = getCms().readPropertyObject(getParamResource(), propertyName, false).getValue();
301        } catch (CmsException e) {
302            if (LOG.isInfoEnabled()) {
303                LOG.info(e.getLocalizedMessage());
304            }
305        }
306        if (CmsStringUtil.isEmpty(propVal)) {
307            propVal = "";
308        }
309        return propVal;
310    }
311
312    /**
313     * returns if the resource to be changed is a folder.<p>
314     *
315     * @return true if the resource is a folder
316     *
317     * @throws CmsException if the reading of the resource fails
318     */
319    public boolean resourceIsFolder() throws CmsException {
320
321        return getCms().readResource(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION).isFolder();
322    }
323
324    /**
325     * Sets the value of the export parameter.<p>
326     *
327     * @param value for the export parameter
328     */
329    public void setParamExport(String value) {
330
331        m_paramExport = value;
332    }
333
334    /**
335     * Sets the value of the export name parameter.<p>
336     *
337     * @param value for the export name parameter
338     */
339    public void setParamExportname(String value) {
340
341        m_paramExportname = value;
342    }
343
344    /**
345     * Sets the value of the intern parameter.<p>
346     *
347     * @param value for the intern parameter
348     */
349    public void setParamIntern(String value) {
350
351        m_paramIntern = value;
352    }
353
354    /**
355     * Sets the value of the secure parameter.<p>
356     *
357     * @param value for the secure parameter
358     */
359    public void setParamSecure(String value) {
360
361        m_paramSecure = value;
362    }
363
364    /**
365     * Determines whether to show the export settings dialog depending on the users settings.<p>
366     *
367     * @return true if dialogs should be shown, otherwise false
368     */
369    public boolean showExportSettings() {
370
371        return getSettings().getUserSettings().getDialogShowExportSettings();
372    }
373
374    /**
375     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
376     */
377    @Override
378    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
379
380        // fill the parameter values in the get/set methods
381        fillParamValues(request);
382
383        // check the required permissions to change the resource properties
384        if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
385            // no write permissions for the resource, set cancel action to close dialog
386            setParamAction(DIALOG_CANCEL);
387        }
388
389        // set the dialog type
390        setParamDialogtype(DIALOG_TYPE);
391        // set the action for the JSP switch
392        if (DIALOG_TYPE.equals(getParamAction())) {
393            setAction(ACTION_CHSECEXP);
394        } else if (DIALOG_LOCKS_CONFIRMED.equals(getParamAction())) {
395            setAction(ACTION_LOCKS_CONFIRMED);
396        } else if (DIALOG_CANCEL.equals(getParamAction())) {
397            setAction(ACTION_CANCEL);
398        } else {
399            setAction(ACTION_DEFAULT);
400            // build title for chnav dialog
401            setParamTitle(
402                key(Messages.GUI_SECURE_EXPORT_RESOURCE_1, new Object[] {CmsResource.getName(getParamResource())}));
403        }
404    }
405
406    /**
407     * Writes a property value for a resource.<p>
408     *
409     * @param propertyName the name of the property
410     * @param propertyValue the new value of the property
411     *
412     * @throws CmsException if something goes wrong
413     */
414    protected void writeProperty(String propertyName, String propertyValue) throws CmsException {
415
416        if (CmsStringUtil.isEmpty(propertyValue)) {
417            propertyValue = CmsProperty.DELETE_VALUE;
418        }
419
420        CmsProperty newProp = new CmsProperty();
421        newProp.setName(propertyName);
422        CmsProperty oldProp = getCms().readPropertyObject(getParamResource(), propertyName, false);
423        if (oldProp.isNullProperty()) {
424            // property value was not already set
425            if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) {
426                newProp.setStructureValue(propertyValue);
427            } else {
428                newProp.setResourceValue(propertyValue);
429            }
430        } else {
431            if (oldProp.getStructureValue() != null) {
432                newProp.setStructureValue(propertyValue);
433                newProp.setResourceValue(oldProp.getResourceValue());
434            } else {
435                newProp.setResourceValue(propertyValue);
436            }
437        }
438
439        newProp.setAutoCreatePropertyDefinition(true);
440
441        String oldStructureValue = oldProp.getStructureValue();
442        String newStructureValue = newProp.getStructureValue();
443        if (CmsStringUtil.isEmpty(oldStructureValue)) {
444            oldStructureValue = CmsProperty.DELETE_VALUE;
445        }
446        if (CmsStringUtil.isEmpty(newStructureValue)) {
447            newStructureValue = CmsProperty.DELETE_VALUE;
448        }
449
450        String oldResourceValue = oldProp.getResourceValue();
451        String newResourceValue = newProp.getResourceValue();
452        if (CmsStringUtil.isEmpty(oldResourceValue)) {
453            oldResourceValue = CmsProperty.DELETE_VALUE;
454        }
455        if (CmsStringUtil.isEmpty(newResourceValue)) {
456            newResourceValue = CmsProperty.DELETE_VALUE;
457        }
458
459        // change property only if it has been changed
460        if (!oldResourceValue.equals(newResourceValue) || !oldStructureValue.equals(newStructureValue)) {
461            getCms().writePropertyObject(getParamResource(), newProp);
462        }
463    }
464}