PermissionsInfoProvider.java 3.96 KB
/*
 * 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);
            }
        }
    }
}