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 Role Management field.
70   */
71  public class RoleManagementFieldFactory extends TwinColSelectFieldFactory<RoleManagementFieldDefinition> {
72  
73      /**
74       * Internal bean to represent basic role data.
75       */
76      private static class Role {
77          public String name;
78          public String uuid;
79  
80          public Role(String name, String uuid) {
81              this.name = name;
82              this.uuid = uuid;
83          }
84      }
85  
86      private static final Logger log = LoggerFactory.getLogger(RoleManagementFieldFactory.class);
87      private ComponentProvider componentProvider;
88  
89      @Inject
90      public RoleManagementFieldFactory(RoleManagementFieldDefinition definition, Item relatedFieldItem, UiContext uiContext, I18NAuthoringSupport i18nAuthoringSupport, ComponentProvider componentProvider) {
91          super(definition, relatedFieldItem, uiContext, i18nAuthoringSupport, componentProvider);
92          definition.setOptions(getOptions());
93          this.componentProvider = componentProvider;
94      }
95  
96      /**
97       * @deprecated since 5.4.7 - use {@link #RoleManagementFieldFactory(RoleManagementFieldDefinition, Item, UiContext, I18NAuthoringSupport, ComponentProvider)} instead.
98       */
99      @Deprecated
100     public RoleManagementFieldFactory(RoleManagementFieldDefinition 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 
117     /**
118      * Returns the available roles with those already assigned marked selected, according to the current node.
119      */
120     @Override
121     public List<SelectFieldOptionDefinition> getOptions() {
122         List<SelectFieldOptionDefinition> options = new ArrayList<SelectFieldOptionDefinition>();
123         List<Role> allRoles = getAllRoles(); // name,uuid
124         Set<String> assignedRoles = getAssignedRoles();
125         for (Role role : allRoles) {
126             SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
127             option.setValue(role.uuid);
128             option.setLabel(role.name);
129             if (assignedRoles.contains(role.uuid)) {
130                 option.setSelected(true);
131             }
132             options.add(option);
133         }
134         return options;
135     }
136 
137     private List<Role> getAllRoles() {
138         List<Role> roles = new ArrayList<Role>();
139         try {
140             NodeIterator ni = QueryUtil.search(RepositoryConstants.USER_ROLES, "SELECT * FROM [" + NodeTypes.Role.NAME + "] ORDER BY name()");
141             while (ni.hasNext()) {
142                 Node n = ni.nextNode();
143                 String name = n.getName();
144                 String uuid = n.getIdentifier();
145                 roles.add(new Role(name, uuid));
146             }
147         } catch (RepositoryException e) {
148             log.error("Cannot read roles from the [" + RepositoryConstants.USER_ROLES + "] workspace.", e);
149         }
150         return roles;
151     }
152 
153     private Set<String> getAssignedRoles() {
154         Set<String> roles = new HashSet<String>();
155         try {
156             Node mainNode = ((JcrNodeAdapter) item).getJcrItem();
157             if (mainNode.hasNode("roles")) {
158                 Node rolesNode = mainNode.getNode("roles");
159                 if (rolesNode == null) {
160                     // shouldn't happen, just in case
161                     return roles;
162                 }
163                 for (PropertyIterator iter = new FilteringPropertyIterator(rolesNode.getProperties(), new JCRMgnlPropertyHidingPredicate());  iter.hasNext();) {
164                     Property p = iter.nextProperty();
165                     roles.add(p.getString());
166                 }
167             }
168         } catch (RepositoryException re) {
169             log.error("Cannot read assigned roles.", re);
170         }
171         return roles;
172     }
173 
174     /**
175      * Create a new Instance of {@link Transformer}.
176      */
177     @Override
178     protected Transformer<?> initializeTransformer(Class<? extends Transformer<?>> transformerClass) {
179         return this.componentProvider.newInstance(transformerClass, item, definition, HashSet.class, getAssignedRoles(), "roles");
180     }
181 
182 }