EditableTemplate.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2019 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.wcm.commons.util;

  21. import static com.day.cq.commons.jcr.JcrConstants.JCR_CONTENT;
  22. import static com.day.cq.commons.jcr.JcrConstants.JCR_PRIMARYTYPE;
  23. import static com.day.cq.wcm.api.NameConstants.NT_TEMPLATE;

  24. import org.apache.commons.lang3.StringUtils;
  25. import org.apache.sling.api.resource.Resource;
  26. import org.apache.sling.api.resource.ResourceResolver;
  27. import org.jetbrains.annotations.NotNull;

  28. import com.day.cq.wcm.api.Page;
  29. import com.day.cq.wcm.api.Template;
  30. import com.day.cq.wcm.api.components.ComponentContext;

  31. /**
  32.  * Helper methods for editable AEM templates.
  33.  */
  34. public final class EditableTemplate {

  35.   static final String NN_EDITABLE_TEMPLATE_INITIAL = "initial";
  36.   static final String NN_EDITABLE_TEMPLATE_STRUCTURE = "structure";
  37.   static final String NN_EDITABLE_TEMPLATE_POLICIES = "policies";

  38.   static final String PN_EDITABLE = "editable";

  39.   private EditableTemplate() {
  40.     // static methods only
  41.   }

  42.   /**
  43.    * Checks if editing of the given component in edit mode is forbidden due to a lock in the template editor
  44.    * for this component.
  45.    * @param wcmComponentContext WCM component context
  46.    * @return true if editing the component is forbidden
  47.    */
  48.   public static boolean isEditRestricted(@NotNull ComponentContext wcmComponentContext) {
  49.     Page page = wcmComponentContext.getPage();
  50.     if (page == null) {
  51.       return false;
  52.     }
  53.     ResourceResolver resourceResolver = wcmComponentContext.getResource().getResourceResolver();
  54.     Template template = page.getTemplate();

  55.     if (template == null || isPageInTemplateDefinition(page) || !isEditableTemplate(template)) {
  56.       return false;
  57.     }

  58.     // check if the current resource path already points to a structure path in the editable template
  59.     String resourcePath = wcmComponentContext.getResource().getPath();
  60.     String templateStructurePath = template.getPath() + "/" + NN_EDITABLE_TEMPLATE_STRUCTURE + "/" + JCR_CONTENT;
  61.     String resourcePathInStructure = null;
  62.     if (StringUtils.startsWith(resourcePath, templateStructurePath + "/")) {
  63.       resourcePathInStructure = resourcePath;
  64.     }
  65.     else {
  66.       // resource path points to the current page, build structure path from path in page
  67.       String relativePathInPage = StringUtils.substringAfter(resourcePath, page.getContentResource().getPath());
  68.       if (StringUtils.isNotEmpty(relativePathInPage)) {
  69.         resourcePathInStructure = templateStructurePath + relativePathInPage;
  70.       }
  71.     }
  72.     if (resourcePathInStructure == null) {
  73.       return false;
  74.     }

  75.     Resource resourceInStructure = resourceResolver.getResource(resourcePathInStructure);
  76.     if (resourceInStructure == null) {
  77.       return false;
  78.     }
  79.     return !resourceInStructure.getValueMap().get(PN_EDITABLE, false);
  80.   }

  81.   /**
  82.    * Checks if the given template is an editable template.
  83.    * @param template TEmplate
  84.    * @return true if template is editable
  85.    */
  86.   private static boolean isEditableTemplate(@NotNull Template template) {
  87.     Resource resource = template.adaptTo(Resource.class);
  88.     if (resource != null) {
  89.       return resource.getChild(NN_EDITABLE_TEMPLATE_INITIAL) != null
  90.           && resource.getChild(NN_EDITABLE_TEMPLATE_STRUCTURE) != null
  91.           && resource.getChild(NN_EDITABLE_TEMPLATE_POLICIES) != null;
  92.     }
  93.     return false;
  94.   }

  95.   /**
  96.    * Checks if the given page is part of the editable template definition itself.
  97.    * @param page Page
  98.    * @return true if page is part of template definition.
  99.    */
  100.   private static boolean isPageInTemplateDefinition(Page page) {
  101.     Resource resource = page.adaptTo(Resource.class);
  102.     if (resource != null) {
  103.       Resource parent = resource.getParent();
  104.       if (parent != null) {
  105.         return StringUtils.equals(NT_TEMPLATE, parent.getValueMap().get(JCR_PRIMARYTYPE, String.class));
  106.       }
  107.     }
  108.     return false;
  109.   }

  110. }