View Javadoc
1   /**
2    * This file Copyright (c) 2012-2015 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.form.field.definition.SelectFieldOptionDefinition;
43  import info.magnolia.ui.form.field.factory.TwinColSelectFieldFactory;
44  import info.magnolia.ui.form.field.transformer.Transformer;
45  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
46  
47  import java.util.ArrayList;
48  import java.util.HashSet;
49  import java.util.List;
50  import java.util.Set;
51  
52  import javax.inject.Inject;
53  import javax.jcr.Node;
54  import javax.jcr.NodeIterator;
55  import javax.jcr.Property;
56  import javax.jcr.PropertyIterator;
57  import javax.jcr.RepositoryException;
58  
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  import com.vaadin.data.Item;
63  import com.vaadin.ui.AbstractSelect;
64  import com.vaadin.ui.TwinColSelect;
65  
66  /**
67   * GUI builder for the Role Management field.
68   */
69  public class RoleManagementFieldFactory extends TwinColSelectFieldFactory<RoleManagementFieldDefinition> {
70  
71      /**
72       * Internal bean to represent basic role data.
73       */
74      private static class Role {
75          public String name;
76          public String uuid;
77  
78          public Role(String name, String uuid) {
79              this.name = name;
80              this.uuid = uuid;
81          }
82      }
83  
84      private static final Logger log = LoggerFactory.getLogger(RoleManagementFieldFactory.class);
85      private ComponentProvider componentProvider;
86  
87      @Inject
88      public RoleManagementFieldFactory(RoleManagementFieldDefinition definition, Item relatedFieldItem, ComponentProvider componentProvider) {
89          super(definition, relatedFieldItem, componentProvider);
90          definition.setOptions(getSelectFieldOptionDefinition());
91          this.componentProvider = componentProvider;
92      }
93  
94      @Override
95      protected AbstractSelect createFieldComponent() {
96          super.createFieldComponent();
97          select.setMultiSelect(true);
98          select.setNullSelectionAllowed(true);
99          return select;
100     }
101 
102     @Override
103     protected AbstractSelect createSelectionField() {
104         return new TwinColSelect();
105     }
106 
107     /**
108      * Returns the available roles with those already assigned marked selected, according to the current node.
109      */
110     @Override
111     public List<SelectFieldOptionDefinition> getSelectFieldOptionDefinition() {
112         List<SelectFieldOptionDefinition> options = new ArrayList<SelectFieldOptionDefinition>();
113         List<Role> allRoles = getAllRoles(); // name,uuid
114         Set<String> assignedRoles = getAssignedRoles();
115         for (Role role : allRoles) {
116             SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
117             option.setValue(role.uuid);
118             option.setLabel(role.name);
119             if (assignedRoles.contains(role.uuid)) {
120                 option.setSelected(true);
121             }
122             options.add(option);
123         }
124         return options;
125     }
126 
127     private List<Role> getAllRoles() {
128         List<Role> roles = new ArrayList<Role>();
129         try {
130             NodeIterator ni = QueryUtil.search(RepositoryConstants.USER_ROLES, "SELECT * FROM [" + NodeTypes.Role.NAME + "] ORDER BY name()");
131             while (ni.hasNext()) {
132                 Node n = ni.nextNode();
133                 String name = n.getName();
134                 String uuid = n.getIdentifier();
135                 roles.add(new Role(name, uuid));
136             }
137         } catch (RepositoryException e) {
138             log.error("Cannot read roles from the [" + RepositoryConstants.USER_ROLES + "] workspace.", e);
139         }
140         return roles;
141     }
142 
143     private Set<String> getAssignedRoles() {
144         Set<String> roles = new HashSet<String>();
145         try {
146             Node mainNode = ((JcrNodeAdapter) item).getJcrItem();
147             if (mainNode.hasNode("roles")) {
148                 Node rolesNode = mainNode.getNode("roles");
149                 if (rolesNode == null) {
150                     // shouldn't happen, just in case
151                     return roles;
152                 }
153                 for (PropertyIterator iter = new FilteringPropertyIterator(rolesNode.getProperties(), new JCRMgnlPropertyHidingPredicate());  iter.hasNext();) {
154                     Property p = iter.nextProperty();
155                     roles.add(p.getString());
156                 }
157             }
158         } catch (RepositoryException re) {
159             log.error("Cannot read assigned roles.", re);
160         }
161         return roles;
162     }
163 
164     /**
165      * Create a new Instance of {@link Transformer}.
166      */
167     @Override
168     protected Transformer<?> initializeTransformer(Class<? extends Transformer<?>> transformerClass) {
169         return this.componentProvider.newInstance(transformerClass, item, definition, HashSet.class, getAssignedRoles(), "roles");
170     }
171 
172 }