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 java.util.Collection;
45  
46  import javax.jcr.RepositoryException;
47  
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  /**
52   * Forum tree for admin view.
53   *
54   * @author gjoseph
55   * @version $Revision: $ ($Author: $)
56   */
57  public class ForumTree extends Tree {
58  
59      private static final Logger log = LoggerFactory.getLogger(ForumTree.class);
60  
61      public ForumTree(String name, String repository) {
62          super(name, repository);
63      }
64  
65      @Override
66      public String getHtmlFooter() {
67          final String str = super.getHtmlFooter();
68          return str + FreemarkerUtil.process(this, "Footer", "html");
69      }
70  
71      @Override
72      protected void onGetHtmlOfSingleItem(StringBuffer html, Content parentNode, String itemType, Object item, String idPre) {
73          // could be a node data!
74          if (item instanceof Content) {
75              final Content node = (Content) item;
76              final String canModerateStr = canModerate(node);
77              // we use the same ugly mechanism as in the original tree class
78              html.append(new Hidden(idPre + "_PermissionModerate", canModerateStr).getHtml());
79          }
80      }
81  
82      public String getRootModerationAllowed() throws RepositoryException {
83          final Content root = getHierarchyManager().getContent(this.getPath());
84          return canModerate(root);
85      }
86  
87  
88      protected String canModerate(Content node) {
89          /*
90           * 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
91           * see MGNLFORUM-250, MGNLFORUM-254
92           */
93          User currentUser = MgnlContext.getUser();
94          Collection<String> allRoles = currentUser.getAllRoles();
95          if(allRoles.contains(DefaultForumManager.ROLE_FORUM_ALL_MODERATOR) || allRoles.contains(DefaultForumManager.ROLE_FORUM_ALL_ADMIN)){
96              return Boolean.toString(Boolean.TRUE);
97          }else{
98              return Boolean.toString(Boolean.FALSE);
99          }
100 
101     }
102 }