PermissionsInfoProvider.java
3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.security.util.CqActions
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageInfoProvider
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.api.JackrabbitSession
* org.apache.jackrabbit.api.security.principal.PrincipalIterator
* org.apache.jackrabbit.api.security.principal.PrincipalManager
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.core.impl;
import com.day.cq.security.util.CqActions;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageInfoProvider;
import java.security.Principal;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.principal.PrincipalIterator;
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Properties(value={@Property(name="service.description", value={"Export user permissions for a page"})})
@Service
public class PermissionsInfoProvider
implements PageInfoProvider {
private static final Logger log = LoggerFactory.getLogger(PermissionsInfoProvider.class);
public void updatePageInfo(SlingHttpServletRequest request, JSONObject info, Resource resource) throws JSONException {
Page page = (Page)resource.adaptTo(Page.class);
if (page != null) {
String pagePath = page.getPath();
ResourceResolver resolver = resource.getResourceResolver();
Session session = (Session)resolver.adaptTo(Session.class);
try {
Authorizable authorizable = (Authorizable)resolver.adaptTo(Authorizable.class);
LinkedHashSet<Principal> principals = new LinkedHashSet<Principal>();
Principal principal = authorizable.getPrincipal();
principals.add(principal);
PrincipalIterator it = ((JackrabbitSession)session).getPrincipalManager().getGroupMembership(principal);
while (it.hasNext()) {
principals.add(it.nextPrincipal());
}
CqActions cqActions = new CqActions(session);
HashSet<String> cqActionSet = new HashSet<String>(Arrays.asList(CqActions.ACTIONS));
Collection allowedActions = cqActions.getAllowedActions(pagePath, principals);
JSONObject permissions = new JSONObject();
info.put("permissions", (Object)permissions);
for (String action : cqActionSet) {
permissions.put(action, allowedActions.contains(action));
}
}
catch (RepositoryException e) {
log.error("Unable to retrieve allowed user actions", (Throwable)e);
}
}
}
}