AEM 6510 - Authoring restrict MSM Rollout to specific User Groups

Goal


Provide MSM (Multi Site Manager) Rollout functionality in authoring to specific User Groups. In this sample, its available to users of group administrators

Demo | Package Install | Github




Solution


1) Login to CRXDE Lite (http://localhost:4502/crx/de), create folder /apps/eaem-msm-allow-rollout-to-specific-groups

2) Create node /apps/eaem-msm-allow-rollout-to-specific-groups/clientlib of type cq:ClientLibraryFolder, add String[] property categories with value [cq.authoring.editor.sites.page], String[] property dependencies with value lodash.

3) Create file (nt:file) /apps/eaem-msm-allow-rollout-to-specific-groups/clientlib/js.txt, add

                        rollout-for-groups.js

4) Create file (nt:file) /apps/eaem-msm-allow-rollout-to-specific-groups/clientlib/rollout-for-groups.js, add the following code

(function ($, $document) {
    var EDITOR_LOADED_EVENT = "cq-editor-loaded",
        allowedGroup = "administrators",
        extended = false;

    $document.on(EDITOR_LOADED_EVENT, extendMSMOpenDialog);

    function extendMSMOpenDialog(){
        if(!Granite.author || !Granite.author.MsmAuthoringHelper){
            console.log("Experience AEM - Granite.author.MsmAuthoringHelper not available");
            return;
        }

        var _origFn = Granite.author.MsmAuthoringHelper.openRolloutDialog;

        Granite.author.MsmAuthoringHelper.openRolloutDialog = function(dialogSource){
            var userGroups = getUserGroups();

            if(!userGroups.includes(allowedGroup)){
                showAlert("Rollout not allowed...", "Rollout");
                return;
            }

            _origFn.call(this, dialogSource);
        };

        handleEditableClick();
    }

    function handleEditableClick(){
        $document.on("cq-overlay-click", function(){
            if(extended){
                return;
            }

            extended = true;

            var _orignRolloutFn = MSM.Rollout.doRollout;

            MSM.Rollout.doRollout = function(commandPath, blueprint, $targets, isBackgroundRollout) {
                var userGroups = getUserGroups();

                if(!userGroups.includes(allowedGroup)){
                    showAlert("Rollout not allowed...", "Rollout");
                    return;
                }

                _orignRolloutFn.call(this, commandPath, blueprint, $targets, isBackgroundRollout);
            }
        });
    }

    function getUserGroups(){
        var userID = Granite.author.ContentFrame.getUserID(), userGroups;

        $.ajax( {
            url: "/bin/security/authorizables.json?filter=" + userID,
            async: false
        } ).done(handler);

        function handler(data){
            if(!data || !data.authorizables){
                return;
            }

            _.each(data.authorizables, function(authObj){
                if( (authObj.id !== userID) || _.isEmpty(authObj.memberOf)){
                    return;
                }

                userGroups = _.pluck(authObj.memberOf, "id");
            });
        }

        return userGroups;
    }

    function showAlert(message, title, callback){
        var fui = $(window).adaptTo("foundation-ui"),
            options = [{
                id: "ok",
                text: "OK",
                primary: true
            }];

        message = message || "Unknown Error";
        title = title || "Error";

        fui.prompt(title, message, "default", options, callback);
    }
}(jQuery, jQuery(document)));

No comments:

Post a Comment