.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) {
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;
}]);