View Javadoc

1   /**
2    * This file Copyright (c) 2003-2012 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.forum.admin;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.gui.control.Hidden;
38  import info.magnolia.cms.gui.control.Tree;
39  import info.magnolia.cms.security.User;
40  import info.magnolia.context.MgnlContext;
41  import info.magnolia.freemarker.FreemarkerUtil;
42  import info.magnolia.module.forum.DefaultForumManager;
43  
44  import javax.jcr.RepositoryException;
45  
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   * Forum tree for admin view.
51   * 
52   * @author gjoseph
53   * @version $Revision: $ ($Author: $)
54   */
55  public class ForumTree extends Tree {
56  
57      private static final Logger log = LoggerFactory.getLogger(ForumTree.class);
58  
59      public ForumTree(String name, String repository) {
60          super(name, repository);
61      }
62  
63      @Override
64      public String getHtmlFooter() {
65          final String str = super.getHtmlFooter();
66          return str + FreemarkerUtil.process(this, "Footer", "html");
67      }
68  
69      @Override
70      protected void onGetHtmlOfSingleItem(StringBuffer html, Content parentNode, String itemType, Object item, String idPre) {
71          // could be a node data!
72          if (item instanceof Content) {
73              final Content node = (Content) item;
74              final String canModerateStr = canModerate(node);
75              // we use the same ugly mechanism as in the original tree class
76              html.append(new Hidden(idPre + "_PermissionModerate", canModerateStr).getHtml());
77          }
78      }
79  
80      public String getRootModerationAllowed() throws RepositoryException {
81          final Content root = getHierarchyManager().getContent(this.getPath());
82          return canModerate(root);
83      }
84  
85  
86      protected String canModerate(Content node) {
87          /*
88           * since we are using a more simplified security on forum shipped with M5.x-versions, this check here will be role- instead of ACL-based
89           * see MGNLFORUM-250, MGNLFORUM-254
90           */
91          User currentUser = MgnlContext.getUser();
92          if(currentUser.hasRole(DefaultForumManager.ROLE_FORUM_ALL_MODERATOR) || currentUser.hasRole(DefaultForumManager.ROLE_FORUM_ALL_ADMIN)){
93              return Boolean.toString(Boolean.TRUE);
94          }else{
95              return Boolean.toString(Boolean.FALSE);
96          }
97  
98      }
99  }