web staff : added egUser.hasPermAt()
authorBill Erickson <berick@esilibrary.com>
Sun, 27 Oct 2013 14:04:09 +0000 (10:04 -0400)
committerBill Erickson <berick@esilibrary.com>
Sun, 27 Oct 2013 14:04:09 +0000 (10:04 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/web/js/ui/default/staff/services/user.js

index ea7aae8..61db10a 100644 (file)
@@ -10,8 +10,8 @@ angular.module('egUserMod', ['egCoreMod'])
 
 
 .factory('egUser', 
-['$q', '$timeout', 'egNet', 'egAuth', 'egUserCache', 
-function($q, $timeout, egNet, egAuth, egUserCache) {
+['$q', '$timeout', 'egNet', 'egAuth', 'egUserCache', 'egOrg',
+function($q, $timeout, egNet, egAuth, egUserCache, egOrg) {
 
     var service = {};
     service.get = function(userId) {
@@ -42,6 +42,42 @@ function($q, $timeout, egNet, egAuth, egUserCache) {
         return deferred.promise;
     };
 
+    /*
+     * Returns the full list of org unit objects at which the currently
+     * logged in user has the selected permissions.
+     * @permList - list or string.  If a list, the response object is a
+     * hash of perm => orgList maps.  If a string, the response is only
+     * the final org list.
+     */
+    service.hasPermAt = function(permList) {
+        var deferred = $q.defer();
+        var isArray = true;
+        if (!angular.isArray(permList)) {
+            isArray = false;
+            permList = [permList];
+        }
+        // as called, this method will return the top-most org unit of the
+        // sub-tree at which this user has the selected permission.
+        // From there, flesh the descendant orgs locally.
+        egNet.request(
+            'open-ils.actor',
+            'open-ils.actor.user.has_work_perm_at.batch',
+            egAuth.token(), permList
+        ).then(function(resp) {
+            var answer = {};
+            angular.forEach(permList, function(perm) {
+                var all = [];
+                angular.forEach(resp[perm], function(oneOrg) {
+                    all = all.concat(egOrg.descendants(oneOrg));
+                });
+                answer[perm] = all;
+            });
+            if (!isArray) answer = answer[permList[0]];
+            deferred.resolve(answer);
+        });
+        return deferred.promise;
+    };
+
     return service;
 }]);