browser staff : egUser.hasPermHere()
authorBill Erickson <berick@esilibrary.com>
Thu, 24 Apr 2014 15:55:30 +0000 (11:55 -0400)
committerBill Erickson <berick@esilibrary.com>
Thu, 24 Apr 2014 15:55:30 +0000 (11:55 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/web/js/ui/default/staff/services/user.js

index 515b81c..bb10e76 100644 (file)
@@ -88,6 +88,53 @@ function($q,  $timeout,  egNet,  egAuth,  egOrg) {
         return deferred.promise;
     };
 
+    /**
+     * Returns a hash of perm => hasPermBool for each requested permission.
+     * If the authenticated user has no workstation, no checks are made
+     * and all permissions return false.
+     */
+    service.hasPermHere = function(permList) {
+        var response = {};
+
+        var isArray = true;
+        if (!angular.isArray(permList)) {
+            isArray = false;
+            permList = [permList];
+        }
+
+        // no workstation, all are false
+        if (egAuth.user().wsid() === null) {
+            console.warn("egUser.hasPermHere() called with no workstation");
+            if (isArray) {
+                response = permList.map(function(perm) {
+                    return response[perm] = false;
+                });
+            } else {
+                response = false;
+            }
+            return $q.when(response);
+        }
+
+        ws_ou = egAuth.user().ws_ou();
+
+        return service.hasPermAt(permList)
+        .then(function(orgMap) {
+            angular.forEach(orgMap, function(orgs, perm) {
+                // each permission is mapped to a flat list of org units,
+                // including descendants.  See if our workstation org unit
+                // is in the list.
+                angular.forEach(orgs, function(org) {
+                    if ((''+org.id()) == (''+ws_ou)) 
+                        response[perm] = true;
+                });
+                if (response[perm] !== true) 
+                    response[perm] = false;
+            });
+            if (!isArray) response = response[permList[0]];
+            return response;
+        });
+    }
+
     return service;
 }]);