remove disallowed permission groups from the perm group selector in user registration...
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 19 Jan 2011 22:27:14 +0000 (22:27 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 19 Jan 2011 22:27:14 +0000 (22:27 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@19221 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/web/js/dojo/openils/widget/FilteringTreeSelect.js
Open-ILS/web/js/ui/default/actor/user/register.js

index 1833f95..6443f89 100644 (file)
@@ -42,7 +42,7 @@ if(!dojo._hasResource["openils.widget.FilteringTreeSelect"]){
                 if(this.dataList.length > 0) {
                     var storeData = fieldmapper[this.className].initStoreData();
                     storeData.items = this.dataList;
-                    this.store = new dojo.data.ItemFileReadStore({data:storeData});
+                    this.store = new dojo.data.ItemFileWriteStore({data:storeData});
                 }
                 this.inherited(arguments);
 
index b842033..fd05db7 100644 (file)
@@ -126,6 +126,7 @@ function load() {
     surveyTemplate = tbody.removeChild(dojo.byId('survey-row-template'));
     surveyQuestionTemplate = tbody.removeChild(dojo.byId('survey-question-row-template'));
 
+    checkGrpAppPerm(); // to do the initial load
     loadStaticFields();
     if(patron.isnew() && patron.addresses().length == 0) 
         uEditNewAddr(null, uEditAddrVirtId, true);
@@ -150,6 +151,56 @@ function load() {
     lock_ready = true;
 }
 
+var permGroups;
+var noPermGroups = [];
+// Returns true if the user is allowed to edit the selected group
+function checkGrpAppPerm(grpId) {
+
+    if(!permGroups) {
+
+        // get the groups
+        permGroups = new openils.PermaCrud().retrieveAll('pgt');
+        var permGroupPerms = []
+
+        // collect the group permissions
+        dojo.forEach(permGroups, 
+            function(grp) {
+                if(grp.application_perm())
+                    permGroupPerms.push(grp.application_perm());
+            }
+        );
+
+        // see which of the group application perms I do not have
+        var myPerms = fieldmapper.standardRequest(
+            ['open-ils.actor', 'open-ils.actor.user.has_work_perm_at.batch'],
+            [openils.User.authtoken, permGroupPerms]
+        );
+
+        var failedPerms = [];
+        for(var p in myPerms) { 
+            if(myPerms[p].length == 0) 
+                failedPerms.push(p); 
+        }
+
+        // identify which groups I cannot edit because I do not have permisssion
+
+        function checkTree(grp, failed) {
+            failed = failed || failedPerms.indexOf(grp.application_perm()) > -1;
+            if(failed) noPermGroups.push(grp.id()+'');
+            dojo.forEach(
+                permGroups.filter(function(g) { return g.parent() == grp.id() } ),
+                function(child) {
+                    checkTree(child, failed);
+                }
+            );
+        }
+
+        checkTree(permGroups.filter(function(g) { return g.parent() == null })[0]);
+    }
+
+    return noPermGroups.indexOf(grpId+'') == -1;
+}
+
 
 function drawAllCards() {
 
@@ -615,12 +666,18 @@ function fleshFMRow(row, fmcls, args) {
         // not overwritten when the profile groups arrive and update
         wargs.forceSync = true;
         wargs.disableQuery = {usergroup : 'f'};
+        if(!patron.isnew() && !checkGrpAppPerm(patron.profile()))
+            wargs.readOnly = true;
     } else {
         wargs.forceSync = false;
     }
 
     var widget = new openils.widget.AutoFieldWidget(wargs);
-    widget.build();
+    widget.build(
+        function(w, ww) {
+            if(fmfield == 'profile') { trimGrpTree(ww); }
+        }
+    );
 
     // now put it back before we register the widget
     if(isPasswd2) fmfield = 'passwd2';
@@ -633,6 +690,19 @@ function fleshFMRow(row, fmcls, args) {
     return widget;
 }
 
+function trimGrpTree(autoWidget) {
+    var store = autoWidget.widget.store;
+    if(!store) return;
+    // remove all groups that this user are not allowed to edit, 
+    // except the profile group of an existing user
+    store.fetch({onItem : 
+        function(item) {
+            if(!checkGrpAppPerm(item.id[0]) && patron.profile() != item.id[0])
+                store.deleteItem(item);
+        }
+    });
+}
+
 function findWidget(wtype, fmfield, callback) {
     return widgetPile.filter(
         function(i){