View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 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.templating.elements;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.cms.i18n.Messages;
38  import info.magnolia.cms.i18n.MessagesManager;
39  import info.magnolia.cms.security.User;
40  import info.magnolia.cms.security.operations.OperationPermissionDefinition;
41  import info.magnolia.context.MgnlContext;
42  import info.magnolia.context.WebContext;
43  import info.magnolia.jcr.inheritance.InheritanceNodeWrapper;
44  import info.magnolia.registry.RegistrationException;
45  import info.magnolia.rendering.context.RenderingContext;
46  import info.magnolia.rendering.engine.AppendableOnlyOutputProvider;
47  import info.magnolia.rendering.engine.RenderException;
48  import info.magnolia.rendering.engine.RenderingEngine;
49  import info.magnolia.rendering.template.AreaDefinition;
50  import info.magnolia.rendering.template.ComponentAvailability;
51  import info.magnolia.rendering.template.TemplateDefinition;
52  import info.magnolia.rendering.template.assignment.TemplateDefinitionAssignment;
53  import info.magnolia.templating.freemarker.AreaDirective;
54  
55  import java.io.IOException;
56  import java.util.HashMap;
57  import java.util.Map;
58  
59  import javax.inject.Inject;
60  import javax.jcr.Node;
61  import javax.jcr.RepositoryException;
62  import javax.jcr.Session;
63  
64  import org.apache.commons.lang.StringUtils;
65  import org.slf4j.Logger;
66  import org.slf4j.LoggerFactory;
67  
68  /**
69   * Renders a piece of content.
70   */
71  public class ComponentElement extends AbstractContentTemplatingElement {
72  
73      private static final Logger log = LoggerFactory.getLogger(ComponentElement.class);
74  
75      private Map<String, Object> contextAttributes = new HashMap<String, Object>();
76      private final RenderingEngine renderingEngine;
77      private Node content;
78      private final TemplateDefinitionAssignment templateDefinitionAssignment;
79      private TemplateDefinition componentDefinition;
80  
81      private boolean renderEditbar = true;
82      private String dialog;
83      private Boolean editable;
84      private Boolean moveable, writable, deletable;
85  
86      @Inject
87      public ComponentElement(ServerConfiguration server, RenderingContext renderingContext, RenderingEngine renderingEngine, TemplateDefinitionAssignment templateDefinitionAssignment ) {
88          super(server, renderingContext);
89          this.renderingEngine = renderingEngine;
90          this.templateDefinitionAssignment = templateDefinitionAssignment;
91      }
92  
93      @Override
94      public void begin(Appendable out) throws IOException, RenderException {
95  
96          content = getPassedContent();
97  
98          if(content == null) {
99              throw new RenderException("The 'content' or 'workspace' and 'path' attribute have to be set to render a component.");
100         }
101 
102         if(isAdmin() && hasPermission(content)){
103 
104             try {
105                 this.componentDefinition = templateDefinitionAssignment.getAssignedTemplateDefinition(content);
106             } catch (RegistrationException e) {
107                 throw new RenderException("No template definition found for the current content", e);
108             }
109 
110             final Messages messages = MessagesManager.getMessages(componentDefinition.getI18nBasename());
111 
112             if (isRenderEditbar()) {
113                 MarkupHelper helper = new MarkupHelper(out);
114 
115                 helper.openComment("cms:component");
116 
117                 helper.attribute(AreaDirective.CONTENT_ATTRIBUTE, getNodePath(content));
118 
119                 if(content instanceof InheritanceNodeWrapper) {
120                     if (((InheritanceNodeWrapper) content).isInherited()) {
121                         helper.attribute("inherited", "true");
122                     }
123                 }
124 
125                 this.editable = resolveEditable();
126                 if (this.editable != null) {
127                     helper.attribute("editable", String.valueOf(this.editable));
128                 }
129 
130                 final AreaDefinition areaDefinition = this.getRenderingContext().getParentAreaDefinition();
131                 OperationPermissionDefinition permissions = null;
132                 User user = null;
133 
134                 if (areaDefinition != null) {
135                     String name = componentDefinition.getName();
136                     ComponentAvailability componentAvailability = areaDefinition.getAvailableComponents().get(name);
137                     if (componentAvailability != null && componentAvailability.getPermissions() != null) {
138                         permissions = componentAvailability.getPermissions();
139                         user = MgnlContext.getUser();
140                     }
141                 }
142 
143                 this.moveable = this.resolveMoveable(permissions, user);
144                 if (this.moveable != null) {
145                     helper.attribute(OperationPermissionDefinition.MOVEABLE, String.valueOf(this.moveable));
146                 }
147 
148                 this.writable = this.resolveWritable(permissions, user);
149                 if (this.writable != null) {
150                     helper.attribute(OperationPermissionDefinition.WRITABLE, String.valueOf(this.writable));
151                 }
152 
153                 this.deletable = this.resolveDeletable(permissions, user);
154                 if (this.deletable != null) {
155                     helper.attribute(OperationPermissionDefinition.DELETABLE, String.valueOf(this.deletable));
156                 }
157 
158                 if(StringUtils.isEmpty(dialog)) {
159                     dialog = resolveDialog();
160                 }
161                 helper.attribute("dialog", dialog);
162 
163                 String label = StringUtils.defaultIfEmpty(componentDefinition.getTitle(),componentDefinition.getName());
164                 helper.attribute("label", messages.getWithDefault(label, label));
165 
166                 if(StringUtils.isNotEmpty(componentDefinition.getDescription())){
167                     helper.attribute("description", componentDefinition.getDescription());
168                 }
169                 helper.append(" -->\n");
170             }
171         }
172 
173         // TODO not sure how to pass editable
174 
175         WebContext webContext = MgnlContext.getWebContext();
176         webContext.push(webContext.getRequest(), webContext.getResponse());
177         setAttributesInWebContext(contextAttributes, WebContext.LOCAL_SCOPE);
178 
179 
180         try {
181             if(componentDefinition != null) {
182                 renderingEngine.render(content, componentDefinition, new HashMap<String, Object>(), new AppendableOnlyOutputProvider(out));
183             } else {
184                 renderingEngine.render(content, new AppendableOnlyOutputProvider(out));
185             }
186         } finally {
187             webContext.pop();
188             webContext.setPageContext(null);
189             restoreAttributesInWebContext(contextAttributes, WebContext.LOCAL_SCOPE);
190         }
191     }
192 
193     private boolean hasPermission(Node node) {
194         try {
195             return node.getSession().hasPermission(node.getPath(), Session.ACTION_SET_PROPERTY);
196         } catch (RepositoryException e) {
197             log.error("Could not determine permission for node {}", node);
198         }
199         return false;
200     }
201 
202     private Boolean resolveEditable() {
203         return editable != null ? editable : componentDefinition != null && componentDefinition.getEditable() != null ? componentDefinition.getEditable() : null;
204     }
205 
206     private Boolean resolveMoveable(final OperationPermissionDefinition permissions, User user) {
207 
208         Boolean moveable = this.moveable != null ? this.moveable : componentDefinition != null && componentDefinition.getMoveable() != null ? componentDefinition.getMoveable() : null;
209         if (moveable != null || permissions == null) {
210             return moveable;
211         }
212 
213         // try to get the permissions from template availability
214         if (!permissions.canMove(user)) {
215             return false;
216         }
217         return true; // true by default
218     }
219 
220     private Boolean resolveWritable(final OperationPermissionDefinition permissions, User user) {
221 
222         Boolean writable = this.writable != null ? this.writable : componentDefinition != null && componentDefinition.getWritable() != null ? componentDefinition.getWritable() : null;
223         if (writable != null || permissions == null) {
224             return writable;
225         }
226         // try to get the permissions from template availability
227         if (!permissions.canWrite(user)) {
228             return false;
229         }
230         return true; // true by default
231     }
232 
233     private Boolean resolveDeletable(final OperationPermissionDefinition permissions, User user) {
234 
235         Boolean deletable = this.deletable != null ? this.deletable : componentDefinition != null && componentDefinition.getDeletable() != null ? componentDefinition.getDeletable() : null;
236         if (deletable != null || permissions == null) {
237             return deletable;
238         }
239         // try to get the permissions from template availability
240         if (!permissions.canDelete(user)) {
241             return false;
242         }
243         return true; // true by default
244     }
245 
246     @Override
247     public void end(Appendable out) throws IOException, RenderException {
248         if(isAdmin()){
249             if (renderEditbar) {
250                 new MarkupHelper(out).closeComment("cms:component");
251             }
252         }
253     }
254 
255     public Map<String, Object> getContextAttributes() {
256         return contextAttributes;
257     }
258 
259     public void setContextAttributes(Map<String, Object> contextAttributes) {
260         this.contextAttributes = contextAttributes;
261     }
262 
263     private String resolveDialog() {
264         if (StringUtils.isNotEmpty(this.dialog)) {
265             return this.dialog;
266         }
267         String dialog = componentDefinition.getDialog();
268         if (StringUtils.isNotEmpty(dialog)) {
269             return dialog;
270         }
271         return null;
272     }
273 
274     public void setDialog(String dialog) {
275         this.dialog = dialog;
276     }
277 
278     public void setEditable(Boolean editable) {
279         this.editable = editable;
280     }
281 
282     public Boolean getEditable() {
283         return editable;
284     }
285 
286     public boolean isRenderEditbar() {
287         return renderEditbar;
288     }
289 
290     public void setRenderEditbar(boolean renderEditbar) {
291         this.renderEditbar = renderEditbar;
292     }
293 }