AEM 6 SP2 - Classic UI Restrict Moving Folder with N level Subfolders in Damadmin

Goal


Restrict moving folders with deep nesting and thousands of assets, triggering resource intensive processing in CRX

Demo | Package Install


Error when Subfolder Levels > 1 





Move in Tree Not Allowed




Solution


1) Login to CRXDE Lite, create folder (nt:folder) /apps/classicui-restrict-folder-move

2) Create clientlib (type cq:ClientLibraryFolder/apps/classicui-restrict-folder-move/clientlib and set a property categories of String type to cq.widgets

3) Create file ( type nt:file ) /apps/classicui-restrict-folder-move/clientlib/js.txt, add the following

                         restrict-move.js

4) Create file ( type nt:file ) /apps/classicui-restrict-folder-move/clientlib/restrict-move.js, add the following code

(function () {
    if (window.location.pathname !== "/damadmin") {
        return;
    }

    var NESTING_ALLOWED = 1, TREE = "cq-damadmin-tree";

    /*//the original move dialog fn
    var cqMoveDialog = CQ.wcm.Page.getMovePageDialog;

    //override ootb function
    CQ.wcm.Page.getMovePageDialog = function (path, isPage) {
        var dialog = cqMoveDialog(path, isPage);

        function handler(isNotAllowed) {
            if (isNotAllowed) {
                CQ.Ext.Msg.alert("Error", "Moving Folder with > " + NESTING_ALLOWED + " level subfolders, not allowed");
                dialog.close();
            }
        }

        isMoveAllowed(path).then(handler);

        return dialog;
    };*/

    //the original move dialog fn
    var cqMovePage = CQ.wcm.SiteAdmin.movePage;

    //override ootb function
    CQ.wcm.SiteAdmin.movePage = function () {
        var selections = this.getSelectedPages();

        if(selections.length == 0){
            return;
        }

        var that = this;

        function handler(isNotAllowed) {
            if (isNotAllowed) {
                CQ.Ext.Msg.alert("Error", "Moving Folder with > " + NESTING_ALLOWED + " level subfolders, not allowed");
                return;
            }

            cqMovePage.call(that);
        }

        isMoveAllowed(selections[0].id).then(handler);
    };

    var INTERVAL = setInterval(function(){
        var tree = CQ.Ext.getCmp(TREE);

        if(tree){
            clearInterval(INTERVAL);
            handleTreeNodeMove(tree);
        }
    }, 250);

    function getFolderJson(path) {
        return $.ajax( path + "." + (NESTING_ALLOWED + 1) + ".json" );
    }

    function isMoveAllowed(path) {
        function handler(data) {
            return reachedMaxNestedLevel(data, NESTING_ALLOWED);
        }

        function reachedMaxNestedLevel(folder, nestingNum) {
            for (var x in folder) {
                if (!folder.hasOwnProperty(x) || !isFolder(folder[x])) {
                    continue;
                }

                if (nestingNum == 0) {
                    return true;
                }

                if (reachedMaxNestedLevel(folder[x], nestingNum - 1)) {
                    return true;
                }
            }

            return false;
        }

        return getFolderJson(path).then(handler);
    }

    function handleTreeNodeMove(tree) {
        var listeners = tree.initialConfig.listeners;

        tree.removeListener("beforenodedrop", listeners.beforenodedrop, tree);

        tree.on("beforenodedrop", function (dropEvent) {
            CQ.Ext.Msg.alert("Error", "Moving tree nodes not allowed, use Move... in grid");
            return false;
        });
    }

    function isFolder(node) {
        return node["jcr:primaryType"] == "sling:OrderedFolder";
    }
}());


4 comments:

  1. Sreejanth, My Initial analysis shows that though this will restrict the user from moving the folder, it still makes the "/bin/wcm/heavymove" call which is very expensive if you are moving a folder with multi-level subfolders. I think a more appropriate solution would be to restrict that call first and only make it for positive scenarios

    ReplyDelete
    Replies
    1. Ashay, ok i see what you are saying... handled with CQ.wcm.SiteAdmin.movePage override

      Delete
    2. can you elaborate more on this? are u suggesting that we need to completely override - CQ.wcm.SiteAdmin.movePage ?

      Delete
    3. oh -- nm :) I see your solution above -- thanks a lot. I will test this :)

      Delete