View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.module.delta;
35  
36  import info.magnolia.jcr.util.NodeTypes;
37  import info.magnolia.module.InstallContext;
38  import info.magnolia.repository.RepositoryConstants;
39  import info.magnolia.repository.RepositoryManager;
40  
41  import java.util.ArrayList;
42  import java.util.Arrays;
43  import java.util.List;
44  
45  import javax.jcr.RepositoryException;
46  import javax.jcr.Session;
47  import javax.jcr.nodetype.NodeDefinition;
48  import javax.jcr.nodetype.NodeType;
49  import javax.jcr.nodetype.NodeTypeManager;
50  
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  /**
55   * Install condition to verify that Magnolia node types don't allow same name siblings.
56   * The check is performed against the {@link RepositoryConstants#WEBSITE} workspace.
57   */
58  public class NoSameNameSiblingsCondition extends AbstractCondition {
59  
60      private static final Logger log = LoggerFactory.getLogger(NoSameNameSiblingsCondition.class);
61  
62      public static final String[] NODETYPES = new String[]{
63              NodeTypes.Content.NAME,
64              NodeTypes.ContentNode.NAME,
65              NodeTypes.Folder.NAME,
66              NodeTypes.Page.NAME,
67              NodeTypes.Area.NAME,
68              NodeTypes.Component.NAME,
69              NodeTypes.User.NAME,
70              NodeTypes.Role.NAME,
71              NodeTypes.Group.NAME,
72              "mgnl:reserve"
73      };
74  
75      public static final List<String> WORKSPACES = Arrays.asList(
76              RepositoryConstants.CONFIG,
77              RepositoryConstants.WEBSITE,
78              RepositoryConstants.USERS,
79              RepositoryConstants.USER_GROUPS,
80              RepositoryConstants.USER_ROLES
81      );
82  
83      private final RepositoryManager repositoryManager;
84  
85      public NoSameNameSiblingsCondition(RepositoryManager repositoryManager) {
86          super("Same name siblings must be not allowed.", "Ensures that same name siblings are not allowed in Magnolia node types.");
87          this.repositoryManager = repositoryManager;
88      }
89  
90      @Override
91      public boolean check(InstallContext installContext) {
92          try {
93              List<String> workspaces = new ArrayList<>();
94              for (String workspaceName : repositoryManager.getWorkspaceNames()) {
95                  if (workspaceName.contains(RepositoryConstants.VERSION_STORE)) {
96                      workspaces.add(workspaceName);
97                  }
98              }
99              workspaces.addAll(WORKSPACES);
100             for (String workspaceName : workspaces) {
101                 Session session = installContext.getJCRSession(workspaceName);
102                 NodeTypeManager ntManager = session.getWorkspace().getNodeTypeManager();
103                 for (String nodetypeName : NODETYPES) {
104                     NodeType nodetype = ntManager.getNodeType(nodetypeName);
105                     for (NodeDefinition def : nodetype.getChildNodeDefinitions()) {
106                         if (def.allowsSameNameSiblings()) {
107                             log.error("Child definition of the [{}] node type in workspace [{}] allows same name siblings.", nodetypeName, workspaceName);
108                             return false;
109                         }
110                     }
111                 }
112             }
113         } catch (RepositoryException ex) {
114             log.error("Cannot verify that same name siblings are not allowed.", ex);
115             return false;
116         }
117         // everything OK
118         return true;
119     }
120 
121 }