View Javadoc
1   /**
2    * This file Copyright (c) 2016-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.security.app.tools.group;
35  
36  import info.magnolia.cms.security.AccessDeniedException;
37  import info.magnolia.cms.security.Group;
38  import info.magnolia.cms.security.SecuritySupport;
39  import info.magnolia.i18nsystem.SimpleTranslator;
40  import info.magnolia.security.app.tools.AbstractSecurityToolPresenter;
41  import info.magnolia.security.app.tools.SecurityToolView;
42  import info.magnolia.ui.api.context.UiContext;
43  import info.magnolia.ui.dialog.formdialog.FormBuilder;
44  import info.magnolia.ui.framework.tools.FormToolActionExecutor;
45  
46  import java.util.Collection;
47  
48  import javax.inject.Inject;
49  
50  import org.apache.commons.collections4.CollectionUtils;
51  import org.apache.commons.lang3.StringEscapeUtils;
52  
53  import com.vaadin.v7.data.Container;
54  import com.vaadin.v7.data.Item;
55  import com.vaadin.v7.data.util.BeanItem;
56  import com.vaadin.v7.data.util.HierarchicalContainer;
57  
58  /**
59   * The security tools help querying members in a group.
60   */
61  public class GroupMemberPresenter extends AbstractSecurityToolPresenter {
62  
63      private final SecuritySupport securitySupport;
64      private final SimpleTranslator i18n;
65      private GroupMemberViewOption viewOption;
66  
67      @Inject
68      public GroupMemberPresenter(SecurityToolView view, FormBuilder formBuilder, FormToolActionExecutor actionExecutor,
69                                  UiContext uiContext, SimpleTranslator i18n, SecuritySupport securitySupport) {
70          super(view, formBuilder, actionExecutor, uiContext);
71          this.securitySupport = securitySupport;
72          this.i18n = i18n;
73      }
74  
75      @Override
76      protected Item getItem() {
77          viewOption = new GroupMemberViewOption();
78          return new BeanItem<>(viewOption);
79      }
80  
81      @Override
82      protected Container.Hierarchical buildContainer() throws AccessDeniedException {
83          container = new HierarchicalContainer();
84          container.addContainerProperty(VALUE_ID, String.class, "");
85          container.addContainerProperty(TRANSITIVE_ID, String.class, "");
86  
87          Group existedGroup = securitySupport.getGroupManager().getGroup(viewOption.getName());
88          if (existedGroup == null) {
89              return null;
90          }
91  
92          Collection<String> directUsers, indirectUsers = null;
93          Collection<String> directGroups, indirectGroups = null;
94  
95          directGroups = securitySupport.getGroupManager().getDirectSubGroups(existedGroup.getName());
96          directUsers = securitySupport.getUserManager().getUsersWithGroup(existedGroup.getName());
97  
98          if (DumpMode.INCLUDE_TRANSITIVE.equals(viewOption.getDumpMode())) {
99              Collection<String> allUsers = securitySupport.getUserManager().getUsersWithGroup(existedGroup.getName(), true);
100             indirectUsers = CollectionUtils.isEmpty(directUsers) ? allUsers : CollectionUtils.disjunction(allUsers, directUsers);
101             indirectGroups = securitySupport.getGroupManager().getAllSuperGroups(existedGroup.getName());
102         }
103 
104         int numberOfGroups = (CollectionUtils.isEmpty(directGroups) ? 0 : directGroups.size()) + (CollectionUtils.isEmpty(indirectGroups) ? 0 : indirectGroups.size());
105         int numberOfUsers = (CollectionUtils.isEmpty(directUsers) ? 0 : directUsers.size()) + (CollectionUtils.isEmpty(indirectUsers) ? 0 : indirectUsers.size());
106 
107         addSection(i18n.translate("security.tools.results.userSection.title", numberOfUsers), directUsers, indirectUsers);
108         addSection(i18n.translate("security.tools.results.groupSection.title", numberOfGroups), directGroups, indirectGroups);
109 
110         return container;
111     }
112 
113     @Override
114     protected String getErrorMessage() {
115         return i18n.translate(i18n.translate("security.groupMembersTab.error"), StringEscapeUtils.escapeHtml4(viewOption.getName()));
116     }
117 
118     private Object addSection(String title, Collection<String> directItems, Collection<String> indirectItems) {
119         if (CollectionUtils.isEmpty(directItems) && CollectionUtils.isEmpty(indirectItems)) {
120             return null;
121         }
122         Object sectionItemId = addContainerItem(VALUE_ID, title, null);
123         container.setChildrenAllowed(sectionItemId, true);
124         if (CollectionUtils.isNotEmpty(directItems)) {
125             for (String item : directItems) {
126                 addSectionItem(item, i18n.translate("security.groupMembersTab.status.direct"), sectionItemId);
127             }
128         }
129         if (CollectionUtils.isNotEmpty(indirectItems)) {
130             for (String item : indirectItems) {
131                 addSectionItem(item, i18n.translate("security.groupMembersTab.status.indirect"), sectionItemId);
132             }
133         }
134         return sectionItemId;
135     }
136 
137     private Object addSectionItem(String value, String transitive, Object parentId) {
138         Object itemId = addContainerItem(VALUE_ID, value, parentId);
139         container.getItem(itemId).getItemProperty(TRANSITIVE_ID).setValue(transitive);
140         container.setChildrenAllowed(itemId, false);
141         return itemId;
142     }
143 
144     /**
145      * Options for querying members tools.
146      */
147     public static class GroupMemberViewOption extends AbstractSecurityToolPresenter.ViewOption {
148         private DumpMode dumpMode;
149 
150         public DumpMode getDumpMode() {
151             return dumpMode;
152         }
153 
154         public void setDumpMode(DumpMode dumpMode) {
155             this.dumpMode = dumpMode;
156         }
157     }
158 
159     /**
160      * Mode of querying members.
161      */
162     public enum DumpMode {
163         INCLUDE_TRANSITIVE, OMIT_TRANSITIVE
164     }
165 }