View Javadoc

1   /**
2    * This file Copyright (c) 2014 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.module.demoproject.setup;
35  
36  import info.magnolia.cms.security.Group;
37  import info.magnolia.cms.security.GroupManager;
38  import info.magnolia.cms.security.SecuritySupport;
39  import info.magnolia.jcr.wrapper.JCRMgnlPropertiesFilteringNodeWrapper;
40  import info.magnolia.module.InstallContext;
41  import info.magnolia.module.delta.AbstractRepositoryTask;
42  import info.magnolia.module.delta.TaskExecutionException;
43  import info.magnolia.repository.RepositoryConstants;
44  
45  import java.util.LinkedList;
46  import java.util.List;
47  
48  import javax.jcr.Node;
49  import javax.jcr.Property;
50  import javax.jcr.PropertyIterator;
51  import javax.jcr.RepositoryException;
52  import javax.jcr.Session;
53  
54  /**
55   * 'demo-project-publisher' ends up having an outdated reference to the publisher group, which got bootstrapped with 5.0
56   * fresh installs. This task takes care of removing it, in case it actually is a stale reference.
57   */
58  public class RemoveLegacyGroupReferenceTask extends AbstractRepositoryTask {
59  
60  
61      final private String PUBLISHER_GROUPS_PATH = "/demo-project-publishers/groups";
62      static final public String PUBLISHER_NEW_UUID = "14540bfe-4e6f-4920-88d5-534d16156ca3";
63      static final public String PUBLISHER_LEGACY_UUID = "ea7603eb-fa77-4a81-97e4-906f6da22588";
64  
65      final private List<String> PUBLISHER_UUIDs = new LinkedList<String>() {{
66          add(PUBLISHER_NEW_UUID);
67          add(PUBLISHER_LEGACY_UUID);
68      }};
69  
70      private SecuritySupport securitySupport;
71  
72      public RemoveLegacyGroupReferenceTask(SecuritySupport securitySupport) {
73          super("Remove group reference", "Remove legacy group reference from demo-project-publishers");
74          this.securitySupport = securitySupport;
75      }
76  
77      @Override
78      protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
79          GroupManager groupManager = securitySupport.getGroupManager();
80          Group group = groupManager.getGroup("publishers");
81          if (group == null) {
82              return;
83          }
84  
85          Session session = installContext.getJCRSession(RepositoryConstants.USER_GROUPS);
86          if (session.itemExists(PUBLISHER_GROUPS_PATH)) {
87              Node groupsNode = new JCRMgnlPropertiesFilteringNodeWrapper(session.getNode(PUBLISHER_GROUPS_PATH));
88  
89              PropertyIterator it = groupsNode.getProperties();
90              while (it.hasNext()) {
91                  Property groupProperty = it.nextProperty();
92                  String uuid = groupProperty.getValue().getString();
93                  if (PUBLISHER_UUIDs.contains(uuid) && !uuid.equals(group.getId())) {
94                      groupProperty.remove();
95                  }
96              }
97          }
98      }
99  }
100