Client Permission Overhaul
authorphasefx <phasefx@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 4 Apr 2011 16:38:40 +0000 (16:38 +0000)
committerphasefx <phasefx@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 4 Apr 2011 16:38:40 +0000 (16:38 +0000)
Menu/Toolbar commands can have a perm= attribute added, containing a space-seperated list of permissions.
Without it they stay enabled at all times (or disabled if they are manually disabled in the file).
With it they disable unless the user has at least one permission listed.

In addition, made *all* new or active windows get UI updates when changing operators, including updated menu enable/disable.

Will need a check on the permissions from previous commit, and probably a pile more from those who understand what needs what.

Signed-off-by: Thomas Berezansky <tsbere@mvlc.org>

git-svn-id: svn://svn.open-ils.org/ILS/trunk@19953 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/xul/staff_client/chrome/content/auth/controller.js
Open-ILS/xul/staff_client/chrome/content/main/main.js
Open-ILS/xul/staff_client/chrome/content/main/menu.js
Open-ILS/xul/staff_client/chrome/content/util/network.js

index 9905185..51a0ad2 100644 (file)
@@ -548,6 +548,8 @@ auth.controller.prototype = {
         this.controller.render('ws_deck');
 
         this.session.close();
+        this.data.menu_perms = false;
+        this.data.stash('menu_perms');
 
         /* FIXME - need some locking or object destruction for the async tests */
         /* this.test_server( this.controller.view.server_prompt.value ); */
index f3de972..9aec40a 100644 (file)
@@ -187,6 +187,39 @@ opentabs:
     }
 }
 
+// Returns false if we can't get an actual perm list
+// Returns an array of perms with boolean has/hasn't flag
+function get_menu_perms(indocument) {
+    // If we don't have our static perm list, and we have a remote window, go looking.
+    // We never need to look twice unless a dev is manually editing their files.
+    // Shame on them, they can restart the entire client ;)
+    if(typeof(get_menu_perms.perm_list) == 'undefined' && indocument != null)
+    {
+        get_menu_perms.perm_list = [ ];
+        var commands = indocument.getElementById('universal_cmds').getElementsByTagName('command');
+        for (var i = 0; i < commands.length; i++) { 
+            if (commands[i].hasAttribute('perm')) {
+                get_menu_perms.perm_list = get_menu_perms.perm_list.concat(commands[i].getAttribute('perm').split(' '));
+            }           
+        }
+    }
+    // 
+    if(typeof(get_menu_perms.perm_list) == 'object') {
+        G.data.stash_retrieve();
+        if(!G.data.menu_perms) {
+            JSAN.use('util.network');
+            var network = new util.network();
+            var r = network.simple_request('BATCH_PERM_RETRIEVE_WORK_OU', [ G.data.session.key, get_menu_perms.perm_list ]);
+            for(p in r)
+                r[p] = (typeof(r[p][0]) == 'number');
+            G.data.menu_perms = r;
+            G.data.stash('menu_perms');
+        }
+        return G.data.menu_perms;
+    }
+    return false;
+}
+
 function main_init() {
     dump('entering main_init()\n');
     try {
index 970e327..9c03e9b 100644 (file)
@@ -69,6 +69,13 @@ main.menu.prototype = {
             if (y) y.setAttribute('hidden','true');
         }
 
+        var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
+                    getService(Components.interfaces.nsIWindowMediator);
+        wm.getMostRecentWindow('eg_main').get_menu_perms(document);
+        JSAN.use('util.network');
+        var network = new util.network();
+        network.set_user_status();
+
         function open_conify_page(path, labelKey, event) {
 
             // tab label
@@ -1072,18 +1079,17 @@ main.menu.prototype = {
                     try {
                         obj.data.stash_retrieve();
                         JSAN.use('util.network'); var network = new util.network();
-                        var x = document.getElementById('oc_menuitem');
-                        var x_label = x.getAttribute('label_orig');
                         var temp_au = js2JSON( obj.data.list.au[0] );
                         var temp_ses = js2JSON( obj.data.session );
                         if (obj.data.list.au.length > 1) {
                             obj.data.list.au = [ obj.data.list.au[1] ];
                             obj.data.stash('list');
                             network.reset_titlebars( obj.data );
-                            x.setAttribute('label', x_label );
                             network.simple_request('AUTH_DELETE', [ obj.data.session.key ] );
                             obj.data.session = obj.data.previous_session;
+                            obj.data.menu_perms = obj.data.previous_menu_perms;
                             obj.data.stash('session');
+                            obj.data.stash('menu_perms');
                             try {
                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                                 var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
@@ -1098,18 +1104,20 @@ main.menu.prototype = {
                             alert(offlineStrings.getFormattedString(main.session_cookie.error, [E]));
                         }
 
-                            removeCSSClass(document.getElementById('main_tabbox'),'operator_change');
                         } else {
                             if (network.get_new_session(offlineStrings.getString('menu.cmd_chg_session.label'),{'url_prefix':obj.url_prefix})) {
                                 obj.data.stash_retrieve();
                                 obj.data.list.au[1] = JSON2js( temp_au );
                                 obj.data.stash('list');
                                 obj.data.previous_session = JSON2js( temp_ses );
+                                obj.data.previous_menu_perms = obj.data.menu_perms;
+                                obj.data.menu_perms = false;
                                 obj.data.stash('previous_session');
-                                x.setAttribute('label', offlineStrings.getFormattedString('menu.cmd_chg_session.operator.label', [obj.data.list.au[1].usrname()]) );
-                                addCSSClass(document.getElementById('main_tabbox'),'operator_change');
+                                obj.data.stash('previous_menu_perms');
+                                obj.data.stash('menu_perms');
                             }
                         }
+                        network.set_user_status();
                     } catch(E) {
                         obj.error.standard_unexpected_error_alert('cmd_change_session',E);
                     }
@@ -1501,6 +1509,26 @@ main.menu.prototype = {
         }
     },
 
+    'set_menu_access' : function(perms) {
+        if(perms === false) return;
+        var commands = document.getElementById('universal_cmds').getElementsByTagName('command');
+        var commandperms;
+commands:
+        for (var i = 0; i < commands.length; i++) { 
+            if (commands[i].hasAttribute('perm')) {
+                commandperms = commands[i].getAttribute('perm').split(' ');
+                for (var j = 0; j < commandperms.length; j++) {
+                    if (perms[commandperms[j]]) {
+                        commands[i].setAttribute('disabled','false');
+                        continue commands;
+                    }
+                }
+                commands[i].setAttribute('disabled','true');
+            }           
+        }
+
+    },
+
     'page_meter' : {
         'node' : document.getElementById('page_progress'),
         'on' : function() {
index 5688f11..5cba385 100644 (file)
@@ -301,6 +301,36 @@ util.network.prototype = {
         }
     },
 
+    'set_user_status' : function() {
+        data.stash_retrieve();
+        try {
+            var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService();
+            var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
+            var permlist = windowManagerInterface.getMostRecentWindow('eg_main').get_menu_perms(null);
+            var offlinestrings;
+            var enumerator = windowManagerInterface.getEnumerator('eg_menu');
+
+            var w;
+            var x;
+            while ( w = enumerator.getNext() ) {
+                x = w.document.getElementById('oc_menuitem');
+
+                if(!offlinestrings) w.document.getElementById('offlineStrings');
+                if(permlist) w.g.menu.set_menu_access(permlist);
+                if(data.list.au.length > 1) {
+                    addCSSClass(w.document.getElementById('main_tabbox'),'operator_change');
+                    x.setAttribute('label', offlineStrings.getFormattedString('menu.cmd_chg_session.operator.label', [data.list.au[1].usrname()]) );
+                }
+                else {
+                    removeCSSClass(w.document.getElementById('main_tabbox'),'operator_change');
+                    x.setAttribute('label', x.getAttribute('label_orig'));
+                }
+            }
+        } catch(E) {
+            obj.error.standard_unexpected_error_alert(offlineStrings.getString('network.window_title.error'),E);
+        }
+    },
+
     'get_new_session' : function(name,xulG,text) {
         var obj = this;
         try {