View Javadoc
1   /**
2    * This file Copyright (c) 2011-2018 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.rendering.template.assignment;
35  
36  import info.magnolia.cms.security.PermissionUtil;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.jcr.util.NodeUtil;
41  import info.magnolia.registry.RegistrationException;
42  import info.magnolia.rendering.template.TemplateAvailability;
43  import info.magnolia.rendering.template.TemplateDefinition;
44  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
45  
46  import java.util.ArrayList;
47  import java.util.Collection;
48  import java.util.Collections;
49  
50  import javax.inject.Inject;
51  import javax.inject.Singleton;
52  import javax.jcr.Node;
53  import javax.jcr.RepositoryException;
54  import javax.jcr.Session;
55  
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  /**
60   * Uses the template id stored in the node's meta data.
61   */
62  @Singleton
63  public class MetaDataBasedTemplateDefinitionAssignment implements TemplateDefinitionAssignment {
64  
65      private static final String DELETED_PAGE_TEMPLATE = "adminInterface:mgnlDeleted";
66  
67      private static final Logger log = LoggerFactory.getLogger(MetaDataBasedTemplateDefinitionAssignment.class);
68  
69      private TemplateDefinitionRegistry templateDefinitionRegistry;
70  
71      @Inject
72      public MetaDataBasedTemplateDefinitionAssignment(TemplateDefinitionRegistry templateDefinitionRegistry) {
73          this.templateDefinitionRegistry = templateDefinitionRegistry;
74      }
75  
76      /**
77       * @deprecated since 5.3.8 - please use again constructor {@link #MetaDataBasedTemplateDefinitionAssignment(TemplateDefinitionRegistry)}.
78       */
79      @Deprecated
80      public MetaDataBasedTemplateDefinitionAssignment(TemplateDefinitionRegistry templateDefinitionRegistry, SimpleTranslator simpleTranslator) {
81          this(templateDefinitionRegistry);
82      }
83  
84      @Override
85      public String getAssignedTemplate(Node content) throws RepositoryException {
86          return NodeTypes.Renderable.getTemplate(content);
87      }
88  
89      @Override
90      public TemplateDefinition getAssignedTemplateDefinition(Node content) throws RegistrationException {
91          final String templateId;
92          try {
93              templateId = getAssignedTemplate(content);
94          } catch (RepositoryException e) {
95              throw new RegistrationException("Could not determine assigned template", e);
96          }
97          if (templateId == null) {
98              throw new RegistrationException("No template definition assigned");
99          }
100         return templateDefinitionRegistry.getTemplateDefinition(templateId);
101     }
102 
103     /**
104      * Get the Template that could be used for the provided content as a default.
105      */
106     @Override
107     public TemplateDefinition getDefaultTemplate(Node content) {
108 
109         // try to use the same as the parent
110         TemplateDefinition parentTemplate = null;
111         try {
112             parentTemplate = getAssignedTemplateDefinition(content.getParent());
113         } catch (RepositoryException e) {
114             log.error("Failed to determine template assigned to parent of node: {}", NodeUtil.getNodePathIfPossible(content), e);
115         } catch (RegistrationException e) {
116             // No template assigned or the assigned template is missing
117         }
118 
119         if (parentTemplate != null && isAvailable(content, parentTemplate)) {
120             return parentTemplate;
121         }
122 
123         // otherwise use the first available template
124         Collection<TemplateDefinition> templates = getAvailableTemplates(content);
125         if (templates.isEmpty()) {
126             return null;
127         }
128 
129         return templates.iterator().next();
130     }
131 
132     @Override
133     public Collection<TemplateDefinition> getAvailableTemplates(Node content) {
134 
135         try {
136             if (content != null && NodeUtil.hasMixin(content, NodeTypes.Deleted.NAME)) {
137                 return Collections.singleton(templateDefinitionRegistry.getTemplateDefinition(DELETED_PAGE_TEMPLATE));
138             }
139         } catch (RepositoryException e) {
140             log.error("Failed to check node for deletion status.", e);
141         } catch (RegistrationException e) {
142             log.error("Deleted content template is not correctly registered.", e);
143         }
144 
145         final ArrayList<TemplateDefinition> availableTemplateDefinitions = new ArrayList<TemplateDefinition>();
146         final Collection<TemplateDefinition> templateDefinitions = templateDefinitionRegistry.getTemplateDefinitions();
147         for (TemplateDefinition templateDefinition : templateDefinitions) {
148             if (isTemplateAvailable(content, templateDefinition)) {
149                 availableTemplateDefinitions.add(templateDefinition);
150             }
151         }
152 
153         return availableTemplateDefinitions;
154     }
155 
156     protected boolean isTemplateAvailable(Node content, TemplateDefinition templateDefinition) {
157         return hasReadAccess(content) &&
158                 isVisible(templateDefinition) &&
159                 isAvailable(content, templateDefinition);
160     }
161 
162     protected boolean isVisible(TemplateDefinition templateDefinition) {
163         return templateDefinition.getVisible() == null || templateDefinition.getVisible();
164     }
165 
166     protected boolean isAvailable(Node content, TemplateDefinition templateDefinition) {
167         TemplateAvailability templateAvailability = templateDefinition.getTemplateAvailability();
168         return templateAvailability == null || templateAvailability.isAvailable(content, templateDefinition);
169     }
170 
171     protected boolean hasReadAccess(Node content) {
172         try {
173             // should not fact that we are able to get path already show that we can read this node???
174             // ... unless of course this "content" was created with system session ... so make sure we check using user session and not the node session
175             return PermissionUtil.isGranted(
176                     MgnlContext.getJCRSession(content.getSession().getWorkspace().getName()),
177                     content.getPath(),
178                     Session.ACTION_READ);
179         } catch (RepositoryException e) {
180             return false;
181         }
182     }
183 }