From 8c4722640f1f94f5b15c0c090d46d0abb933376d Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Wed, 30 Apr 2014 12:43:31 -0400 Subject: [PATCH] new egPerm mod Signed-off-by: Bill Erickson --- Open-ILS/web/js/ui/default/staff/services/auth.js | 115 +++++++++++++++++++++- Open-ILS/web/js/ui/default/staff/services/user.js | 84 ---------------- 2 files changed, 110 insertions(+), 89 deletions(-) diff --git a/Open-ILS/web/js/ui/default/staff/services/auth.js b/Open-ILS/web/js/ui/default/staff/services/auth.js index 3c4ad88065..0cc6c1fb88 100644 --- a/Open-ILS/web/js/ui/default/staff/services/auth.js +++ b/Open-ILS/web/js/ui/default/staff/services/auth.js @@ -13,17 +13,23 @@ angular.module('egCoreMod') .factory('egAuth', ['$q','$cookies','egNet','EG_AUTH_COOKIE', -function($q, $cookies, egNet, EG_AUTH_COOKIE) { +function($q , $cookies , egNet , EG_AUTH_COOKIE) { var service = { - // expose user and token via function, since we will eventually - // want to support multiple active logins, in which case user() - // and token() will return data for the currently active login. + // the currently active user object user : function() { return this._user; }, + + // the currently active auth token string token : function() { return $cookies[EG_AUTH_COOKIE]; + }, + + // the currently active workstation + workstation : function(ws) { + if (ws) this._workstation = ws; + return this._workstation; } }; @@ -100,5 +106,104 @@ function($q, $cookies, egNet, EG_AUTH_COOKIE) { }; return service; -}]); +}]) + + +/** + * Service for testing user permissions. + * Note: this cannot live within egAuth, because it creates a circular + * dependency of egOrg -> egEnv -> egAuth -> egOrg + */ +.factory('egPerm', + ['$q','$cookies','egNet','egAuth','egOrg', +function($q , $cookies , egNet , egAuth , egOrg) { + var service = {}; + + /* + * 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 the + * org list for the requested perm. + */ + 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; + }; + + + /** + * 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("egPerm.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; +}]) + diff --git a/Open-ILS/web/js/ui/default/staff/services/user.js b/Open-ILS/web/js/ui/default/staff/services/user.js index bb10e76961..0ed5cac038 100644 --- a/Open-ILS/web/js/ui/default/staff/services/user.js +++ b/Open-ILS/web/js/ui/default/staff/services/user.js @@ -1,6 +1,5 @@ /** * Service for fetching fleshed user objects. - * The last user retrieved is kept until replaced by a new user. */ angular.module('egUserMod', ['egCoreMod']) @@ -52,89 +51,6 @@ function($q, $timeout, egNet, egAuth, egOrg) { 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 the - * org list for the requested perm. - */ - 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; - }; - - /** - * 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; }]); -- 2.11.0