Clover icon

Magnolia REST Integration 2.0-rc1

  1. Project Clover database Mon Oct 30 2017 16:27:09 CET
  2. Package info.magnolia.rest.setup

File UpdateRestEditorRoleTask.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
19
4
1
102
49
6
0.32
4.75
4
1.5

Classes

Class Line # Actions
UpdateRestEditorRoleTask 56 19 0% 6 1
0.96296396.3%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017 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.rest.setup;
35   
36    import info.magnolia.jcr.predicate.AbstractPredicate;
37    import info.magnolia.jcr.util.NodeUtil;
38    import info.magnolia.jcr.util.PropertyUtil;
39    import info.magnolia.module.InstallContext;
40    import info.magnolia.module.delta.AbstractRepositoryTask;
41    import info.magnolia.module.delta.TaskExecutionException;
42    import info.magnolia.repository.RepositoryConstants;
43   
44    import java.util.Iterator;
45   
46    import javax.jcr.Node;
47    import javax.jcr.RepositoryException;
48    import javax.jcr.Session;
49   
50    import org.slf4j.Logger;
51    import org.slf4j.LoggerFactory;
52   
53    /**
54    * Task to update rest role node to rest-editor role node.
55    */
 
56    class UpdateRestEditorRoleTask extends AbstractRepositoryTask {
57    private static final Logger log = LoggerFactory.getLogger(UpdateRestEditorRoleTask.class);
58   
 
59  4 toggle UpdateRestEditorRoleTask(String name, String description) {
60  4 super(name, description);
61    }
62   
 
63  3 toggle @Override
64    protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
65  3 Session session = installContext.getJCRSession(RepositoryConstants.USER_ROLES);
66  3 Node rest = session.getNode("/rest");
67   
68    // First rename role node
69  3 NodeUtil.renameNode(rest, "rest-editor");
70    // Update acl_userroles to self
71  3 updateAcl(rest, "userroles", "path", "/rest", "/rest-editor");
72   
73    // Update acl_uri deny rule path
74  3 updateAcl(rest, "uri", "path", "/.rest*", "/.rest/*");
75   
76    // Update description and title
77  3 PropertyUtil.setProperty(rest, "description", "This role grants GET/POST permissions to REST services APIs (nodes, properties), for a limited set of workspaces.");
78  3 PropertyUtil.setProperty(rest, "title", "REST Editor");
79    }
80   
 
81  6 toggle private void updateAcl(Node roleNode, String workspace, String propertyName, String oldValue, String newValue) throws RepositoryException {
82  6 if (!roleNode.hasNode("acl_" + workspace)) {
83  2 log.warn(String.format("Cannot update acl_%s for role %s, ACL node was not found.", workspace, roleNode.getName()));
84  2 return;
85    }
86   
87  4 Node aclNode = roleNode.getNode("acl_" + workspace);
88  4 Iterable<Node> matchingChildNodes = NodeUtil.getNodes(aclNode, new AbstractPredicate<Node>() {
 
89  4 toggle @Override
90    public boolean evaluateTyped(Node node) {
91  4 String property = PropertyUtil.getString(node, propertyName);
92  4 return property != null && property.equals(oldValue);
93    }
94    });
95   
96  4 Iterator<Node> iterator = matchingChildNodes.iterator();
97  4 if (iterator.hasNext()) {
98  4 Node node = iterator.next();
99  4 node.setProperty(propertyName, newValue);
100    }
101    }
102    }