View Javadoc
1   /**
2    * This file Copyright (c) 2009-2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.site.templates;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.rendering.template.TemplateAvailability;
38  import info.magnolia.rendering.template.TemplateDefinition;
39  import info.magnolia.repository.RepositoryConstants;
40  
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import java.util.LinkedHashMap;
44  import java.util.List;
45  import java.util.Map;
46  
47  import javax.jcr.Node;
48  import javax.jcr.RepositoryException;
49  import javax.jcr.Workspace;
50  
51  import org.apache.commons.collections4.CollectionUtils;
52  import org.apache.commons.lang3.BooleanUtils;
53  import org.apache.commons.lang3.StringUtils;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  
58  /**
59   * This class provides a means to define available {@link info.magnolia.rendering.template.TemplateDefinition}s per
60   * {@link info.magnolia.module.site.Site}. To do so, list your templates (see
61   * {@link info.magnolia.module.site.templates.TemplateConfig}) in
62   * <code>/modules/site/config/site/templates/availability</code>.
63   *
64   * <p>This {@link info.magnolia.rendering.template.TemplateAvailability} is not to be confused with the
65   * {@link info.magnolia.module.site.templates.SiteAwareTemplateAvailability} that uses these configured
66   * templates to check whether a template is available or not.</p>
67   *
68   * @see info.magnolia.module.site.templates.TemplateSettings
69   * @see info.magnolia.module.site.templates.ConfiguredTemplateSettings
70   */
71  public class ConfiguredSiteTemplateAvailability implements TemplateAvailability {
72  
73      private static final Logger log = LoggerFactory.getLogger(ConfiguredSiteTemplateAvailability.class);
74  
75      private Map<String, TemplateConfig> templates = new LinkedHashMap<>();
76  
77      private boolean enableAll;
78  
79      private List<String> enableAllWithRenderType = new ArrayList<>();
80  
81      public Map<String, TemplateConfig> getTemplates() {
82          return this.templates;
83      }
84  
85      /**
86       * Sets all templates. Uses {@link info.magnolia.module.site.templates.TemplateConfig#getId()} as key rather than
87       * the name of the {@link javax.jcr.Node}. This is important as the actual templateId is indeed the identifier for
88       * a {@link info.magnolia.rendering.template.TemplateDefinition} in the
89       * {@link info.magnolia.rendering.template.registry.TemplateDefinitionRegistry}.
90       */
91      public void setTemplates(Map<String, TemplateConfig> templates) {
92          this.templates.clear();
93          for (TemplateConfig templateConfig : templates.values()) {
94              this.templates.put(templateConfig.getId(), templateConfig);
95          }
96      }
97  
98      /**
99       * Adds a {@link info.magnolia.module.site.templates.TemplateConfig} to the available templates.
100      */
101     public void addTemplate(TemplateConfig templateConfig) {
102         templates.put(templateConfig.getId(), templateConfig);
103     }
104 
105     public boolean isEnableAll() {
106         return enableAll;
107     }
108 
109     public void setEnableAll(boolean enableAll) {
110         this.enableAll = enableAll;
111     }
112 
113     public List<String> getEnableAllWithRenderType() {
114         return enableAllWithRenderType;
115     }
116 
117     public void setEnableAllWithRenderType(List<String> enableAllWithRenderType) {
118         this.enableAllWithRenderType = enableAllWithRenderType;
119     }
120 
121     @Override
122     public boolean isAvailable(Node node, TemplateDefinition templateDefinition) {
123         if (node == null || templateDefinition == null) {
124             return false;
125         }
126 
127         // Templates with visible property is set to false are not available
128         if (BooleanUtils.isFalse(templateDefinition.getVisible())) {
129             return false;
130         }
131 
132         try {
133             final Workspace workspace = node.getSession().getWorkspace();
134             if (!RepositoryConstants.WEBSITE.equals(workspace.getName())) {
135                 return false;
136             }
137         } catch (RepositoryException e) {
138             log.error("Not able to access the Node's session or workspace.", e);
139             return false;
140         }
141 
142         if ((isEnableAll() || getEnableAllWithRenderType().contains(templateDefinition.getRenderType())) && StringUtils.substringAfter(templateDefinition.getId(), ":").startsWith("pages/")) {
143             return true;
144         }
145 
146         if (!getTemplates().containsKey(templateDefinition.getId())) {
147             return false;
148         }
149 
150         // Check configured roles
151         final TemplateConfig templateConfig = getTemplates().get(templateDefinition.getId());
152         final Collection<String> roles = templateConfig.getRoles();
153         if (!roles.isEmpty()) {
154             if (!CollectionUtils.containsAny(MgnlContext.getUser().getAllRoles(), roles)) {
155                 return false;
156             }
157         }
158 
159         return true;
160     }
161 }