View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.dialog.field;
35  
36  import info.magnolia.cms.util.QueryUtil;
37  import info.magnolia.jcr.iterator.FilteringPropertyIterator;
38  import info.magnolia.jcr.predicate.JCRMgnlPropertyHidingPredicate;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.objectfactory.ComponentProvider;
41  import info.magnolia.repository.RepositoryConstants;
42  import info.magnolia.ui.api.context.UiContext;
43  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
44  import info.magnolia.ui.form.field.definition.SelectFieldOptionDefinition;
45  import info.magnolia.ui.form.field.factory.TwinColSelectFieldFactory;
46  import info.magnolia.ui.form.field.transformer.Transformer;
47  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
48  
49  import java.util.ArrayList;
50  import java.util.HashSet;
51  import java.util.List;
52  import java.util.Set;
53  
54  import javax.inject.Inject;
55  import javax.jcr.Node;
56  import javax.jcr.NodeIterator;
57  import javax.jcr.Property;
58  import javax.jcr.PropertyIterator;
59  import javax.jcr.RepositoryException;
60  
61  import org.slf4j.Logger;
62  import org.slf4j.LoggerFactory;
63  
64  import com.vaadin.v7.data.Item;
65  import com.vaadin.v7.ui.AbstractSelect;
66  import com.vaadin.v7.ui.TwinColSelect;
67  
68  /**
69   * GUI builder for the Group Management field.
70   */
71  public class GroupManagementFieldFactory extends TwinColSelectFieldFactory<GroupManagementFieldDefinition> {
72  
73      /**
74       * Internal bean to represent basic group data.
75       */
76      public static class Group {
77          public String name;
78          public String uuid;
79  
80          public Group(String name, String uuid) {
81              this.name = name;
82              this.uuid = uuid;
83          }
84      }
85  
86      private static final Logger log = LoggerFactory.getLogger(GroupManagementFieldFactory.class);
87      private ComponentProvider componentProvider;
88  
89      @Inject
90      public GroupManagementFieldFactory(GroupManagementFieldDefinition definition, Item relatedFieldItem, UiContext uiContext, I18NAuthoringSupport i18nAuthoringSupport, ComponentProvider componentProvider) {
91          super(definition, relatedFieldItem, uiContext, i18nAuthoringSupport, componentProvider);
92          this.definition.setOptions(getOptions());
93          this.componentProvider = componentProvider;
94      }
95  
96      /**
97       * @deprecated since 5.4.7 - use {@link #GroupManagementFieldFactory(GroupManagementFieldDefinition, Item, UiContext, I18NAuthoringSupport, ComponentProvider)} instead.
98       */
99      @Deprecated
100     public GroupManagementFieldFactory(GroupManagementFieldDefinition definition, Item relatedFieldItem, ComponentProvider componentProvider) {
101         this(definition, relatedFieldItem, null, componentProvider.getComponent(I18NAuthoringSupport.class), componentProvider);
102     }
103 
104     @Override
105     protected AbstractSelect createFieldComponent() {
106         super.createFieldComponent();
107         select.setMultiSelect(true);
108         select.setNullSelectionAllowed(true);
109         return select;
110     }
111 
112     @Override
113     protected AbstractSelect createSelectionField() {
114         return new TwinColSelect() {
115 
116             @Override
117             public String getConnectorId() {
118                 return super.getConnectorId();
119             }
120 
121             /* (non-Javadoc)
122              * @see com.vaadin.ui.AbstractComponent#isVisible()
123              */
124             @Override
125             public boolean isVisible() {
126                 return super.isVisible();
127             }
128         };
129     }
130 
131     /**
132      * Returns the available groups with those already assigned marked selected,
133      * according to the current node.
134      */
135     @Override
136     public List<SelectFieldOptionDefinition> getOptions() {
137         List<SelectFieldOptionDefinition> options = new ArrayList<SelectFieldOptionDefinition>();
138         List<Group> allGroups = getAllGroups(); // name,uuid
139         Set<String> assignedGroups = getAssignedGroups();
140         String currentUUID = null;
141         try {
142             currentUUID = ((JcrNodeAdapter) item).getJcrItem().getIdentifier();
143         } catch (RepositoryException e) {
144             // nothing to do
145         }
146         for (Group group : allGroups) {
147             SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
148             option.setValue(group.uuid);
149             option.setLabel(group.name);
150             if (assignedGroups.contains(group.uuid)) {
151                 option.setSelected(true);
152             }
153             if (!group.uuid.equals(currentUUID)) {
154                 // we don't want the group to be assigned to itself, do we?
155                 options.add(option);
156             }
157         }
158         return options;
159     }
160 
161     private List<Group> getAllGroups() {
162         List<Group> groups = new ArrayList<Group>();
163         try {
164             NodeIterator ni = QueryUtil.search(RepositoryConstants.USER_GROUPS, "SELECT * FROM [" + NodeTypes.Group.NAME + "] ORDER BY name()");
165 
166             while (ni.hasNext()) {
167                 Node n = ni.nextNode();
168                 String name = n.getName();
169                 String uuid = n.getIdentifier();
170                 groups.add(new Group(name, uuid));
171             }
172         } catch (RepositoryException e) {
173             log.error("Cannot read groups from the [" + RepositoryConstants.USER_GROUPS + "] workspace.", e);
174         }
175         return groups;
176     }
177 
178     private Set<String> getAssignedGroups() {
179         Set<String> groups = new HashSet<String>();
180         try {
181             Node mainNode = ((JcrNodeAdapter) item).getJcrItem();
182             if (mainNode.hasNode("groups")) {
183                 Node groupsNode = mainNode.getNode("groups");
184                 if (groupsNode == null) {
185                     // shouldn't happen, just in case
186                     return groups;
187                 }
188                 for (PropertyIterator iter = new FilteringPropertyIterator(groupsNode.getProperties(), new JCRMgnlPropertyHidingPredicate()); iter.hasNext();) {
189                     Property p = iter.nextProperty();
190                     groups.add(p.getString());
191 
192                 }
193             }
194         } catch (RepositoryException re) {
195             log.error("Cannot read assigned groups.", re);
196         }
197         return groups;
198     }
199 
200     /**
201      * Create a new Instance of {@link Transformer}.
202      */
203     @Override
204     protected Transformer<?> initializeTransformer(Class<? extends Transformer<?>> transformerClass) {
205         return this.componentProvider.newInstance(transformerClass, item, definition, HashSet.class, getAssignedGroups(), "groups");
206     }
207 
208 }