AEM CQ 56 - Disable SideKick Activate, Deactivate for Non-Administrators

Goal


Disable the Sidekick Activate and Deactivate buttons if logged-in user is not in group administrators. Check the demo and Source code ( not package install )




Solution


1) Code a servlet apps.experienceaem.sidekick.GetUserGroups to return the logged-in user groups. Here is the source

package apps.experienceaem.sidekick;

import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Iterator;

@SlingServlet(
        paths = "/bin/experienceaem/getgroups",
        methods = "GET",
        metatype = false,
        label = "Get user groups"
)
public class GetUserGroups extends SlingAllMethodsServlet {
    private static final Logger log = LoggerFactory.getLogger(GetUserGroups.class);

    @Override
    protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");

        JSONWriter jw = new JSONWriter(response.getWriter());

        try {
            ResourceResolver resolver = request.getResourceResolver();
            User user = resolver.adaptTo(User.class);

            Iterator<Group> groups = user.memberOf();
            jw.object();
            jw.key(user.getID()).array();

            while(groups.hasNext()){
                jw.value(groups.next().getID());
            }

            jw.endArray();
            jw.endObject();
        } catch (Exception e) {
            log.error("Error getting groups", e);
            throw new ServletException(e);
        }
    }
}

2) Login to CRXDE Lite, create folder (nt:folder) /apps/skdisable

3) Create clientlib (type cq:ClientLibraryFolder/apps/skdisable/clientlib and set a property categories of String type to cq.widgets

4) Create file ( type nt:file ) /apps/skdisable/clientlib/js.txt, add the following

                         disable.js

5) Create file ( type nt:file ) /apps/skdisable/clientlib/disable.js, add the following code

(function(){
    if( ( window.location.pathname == "/cf" ) || ( window.location.pathname.indexOf("/content") == 0)){
        var SK_INTERVAL = setInterval(function(){
            var sk = CQ.WCM.getSidekick();

            if(sk && sk.panels){
                clearInterval(SK_INTERVAL);

                $.ajax({
                    url: '/bin/experienceaem/getgroups',
                    dataType: "json",
                    type: 'GET',
                    async: false,
                    success: function(data){
                        data = data[CQ.User.getCurrentUser().getUserID()];

                        if(data.indexOf("administrators") !== -1){
                            return;
                        }
                        var pagePanel = sk.panels["PAGE"];

                        var buttons = pagePanel.findBy(function(comp){
                            return comp["name"] == "PUBLISH" || comp["name"] == "DEACTIVATE";
                        }, pagePanel);

                        CQ.Ext.each(buttons, function(button){
                            button.setDisabled(true);
                        });
                    }
                });
            }
        }, 250);
    }
})();

No comments:

Post a Comment