View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.action;
35  
36  import info.magnolia.cms.security.Group;
37  import info.magnolia.cms.security.Security;
38  import info.magnolia.cms.security.SecuritySupport;
39  import info.magnolia.cms.security.User;
40  import info.magnolia.commands.CommandsManager;
41  import info.magnolia.event.EventBus;
42  import info.magnolia.i18nsystem.SimpleTranslator;
43  import info.magnolia.objectfactory.Components;
44  import info.magnolia.ui.api.action.ActionExecutionException;
45  import info.magnolia.ui.api.context.UiContext;
46  import info.magnolia.ui.api.event.AdmincentralEventBus;
47  import info.magnolia.ui.framework.action.DeleteAction;
48  import info.magnolia.ui.framework.action.DeleteActionDefinition;
49  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
50  
51  import java.util.ArrayList;
52  import java.util.Collection;
53  import java.util.List;
54  
55  import javax.inject.Inject;
56  import javax.inject.Named;
57  import javax.jcr.RepositoryException;
58  
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  /**
63   * Abstract common supertype for {@link DeleteGroupAction} and {@link DeleteRoleAction}.
64   *
65   * @param <D> the action definition type, must extend the {@link DeleteActionDefinition} class.
66   */
67  public abstract class AbstractDeleteGroupOrRoleAction<D extends DeleteActionDefinition> extends DeleteAction {
68  
69      private static final Logger log = LoggerFactory.getLogger(AbstractDeleteGroupOrRoleAction.class);
70  
71      private final JcrItemAdapter item;
72      private final SecuritySupport securitySupport;
73  
74      @Inject
75      public AbstractDeleteGroupOrRoleAction(D definition, JcrItemAdapter item, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n, SecuritySupport securitySupport) {
76          super(definition, item, commandsManager, eventBus, uiContext, i18n);
77          this.item = item;
78          this.securitySupport = securitySupport;
79      }
80  
81      /**
82       * @deprecated since 5.3.6 instead of use {@link #AbstractDeleteGroupOrRoleAction(info.magnolia.ui.framework.action.DeleteActionDefinition, info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter, info.magnolia.commands.CommandsManager, info.magnolia.event.EventBus, info.magnolia.ui.api.context.UiContext, info.magnolia.i18nsystem.SimpleTranslator, info.magnolia.cms.security.SecuritySupport)}
83       */
84      @Deprecated
85      public AbstractDeleteGroupOrRoleAction(D definition, JcrItemAdapter item, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n) {
86          this(definition, item, commandsManager, eventBus, uiContext, i18n, Security.getSecuritySupport());
87      }
88  
89      /**
90       * @deprecated since 5.2.2 instead of use {@link #AbstractDeleteGroupOrRoleAction(info.magnolia.ui.framework.action.DeleteActionDefinition, info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter, info.magnolia.commands.CommandsManager, info.magnolia.event.EventBus, info.magnolia.ui.api.context.UiContext, info.magnolia.i18nsystem.SimpleTranslator, info.magnolia.cms.security.SecuritySupport)}
91       */
92      @Deprecated
93      public AbstractDeleteGroupOrRoleAction(D definition, JcrItemAdapter item, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n) {
94          this(definition, item, Components.getComponent(CommandsManager.class), eventBus, uiContext, i18n, Security.getSecuritySupport());
95      }
96  
97      /**
98       * @deprecated since 5.2.2 instead of use {@link #getCurrentItem()}
99       */
100     @Deprecated
101     public JcrItemAdapter getItem() {
102         return this.item;
103     }
104 
105     /**
106      * @return the base for the error message shown to the user in case the item is already assigned; the list of users/groups the item is assigned to is added;
107      */
108     protected abstract String getBaseErrorMessage();
109 
110     /**
111      * @return the message to be shown to the user in case the verification ({@link #getUsersAndGroupsThisItemIsAssignedTo()} method) fails.
112      */
113     protected abstract String getVerificationErrorMessage();
114 
115     /**
116      * Gets a collection of group or role names (according to where it is implemented) assigned to the user.
117      */
118     protected abstract Collection<String> getGroupsOrRoles(User user);
119 
120     /**
121      * Gets a collection of group or role names (according to where it is implemented) assigned to the group.
122      */
123     protected abstract Collection<String> getGroupsOrRoles(Group group);
124 
125     /**
126      * @return Collection of users that have the group or role to delete assigned to
127      */
128     protected abstract Collection<String> getUsersWithGroupOrRoleToDelete(String groupOrRoleName);
129 
130     /**
131      * @return Collection of groups that have the group or role to delete assigned to
132      */
133     protected abstract Collection<String> getGroupsWithGroupOrRoleToDelete(String groupOrRoleName);
134 
135     /**
136      * @deprecated since 5.2.2 instead of use {@link #onPreExecute()}
137      */
138     @Deprecated
139     protected void executeAfterConfirmation() {
140         log.warn("This method was deprecated. Use #onPreExecute() method instead.");
141     }
142 
143     @Override
144     protected void onPreExecute() throws Exception {
145         super.onPreExecute();
146 
147         List<String> assignedTo;
148         try {
149             assignedTo = getUsersAndGroupsThisItemIsAssignedTo();
150         } catch (RepositoryException e) {
151             log.error("Cannot get the users/groups the group or role is assigned to.", e);
152             throw new ActionExecutionException(getVerificationErrorMessage() + e.getMessage());
153         }
154         if (assignedTo != null && !assignedTo.isEmpty()) {
155             throw new ActionExecutionException(getBaseErrorMessage() + getUserAndGroupListForErrorMessage(assignedTo));
156         }
157     }
158 
159     /**
160      * @return the list of user- and group-names this item (group or role) is directly assigned to.
161      */
162     private List<String> getUsersAndGroupsThisItemIsAssignedTo() throws RepositoryException {
163         List<String> assignedTo = new ArrayList<String>();
164 
165         final String groupOrRoleName = getCurrentItem().getJcrItem().getName();
166         final String translatedUserString = getI18n().translate("security.delete.userIdentifier");
167         // users
168         for (String user : getUsersWithGroupOrRoleToDelete(groupOrRoleName)) {
169             assignedTo.add(translatedUserString + ":" + user);
170         }
171         // groups
172         final String translatedGroupString = getI18n().translate("security.delete.groupIdentifier");
173         for (String group : getGroupsWithGroupOrRoleToDelete(groupOrRoleName)) {
174             assignedTo.add(translatedGroupString + ":" + group);
175         }
176 
177         return assignedTo;
178     }
179 
180     private static String getUserAndGroupListForErrorMessage(Collection<String> usersAndGroups) {
181         StringBuilder message = new StringBuilder("<ul>");
182         for (String name : usersAndGroups) {
183             message.append("<li>").append(name).append("</li>");
184         }
185         message.append("</ul>");
186         return message.toString();
187     }
188 
189     protected SecuritySupport getSecuritySupport() {
190         return securitySupport;
191     }
192 }