.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;
}
};
};
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;
+}])
+
/**
* Service for fetching fleshed user objects.
- * The last user retrieved is kept until replaced by a new user.
*/
angular.module('egUserMod', ['egCoreMod'])
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;
}]);