woodpecker/cmd/drone-server/static/scripts/services/users.js

89 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-04-08 22:43:59 +00:00
'use strict';
(function () {
/**
* Cached user object.
*/
var _user;
/**
* The UserService provides access to useer
* data using REST API calls.
*/
function UserService($http, $q) {
/**
* Gets a list of all users.
*/
this.list = function() {
return $http.get('/api/users');
};
/**
* Gets a user by login.
*/
this.get = function(login) {
return $http.get('/api/users/'+login);
};
/**
* Gets the currently authenticated user.
*/
this.getCurrent = function() {
return $http.get('/api/user');
};
/**
* Updates an existing user
*/
this.post = function(user) {
return $http.post('/api/users/'+user);
};
/**
* Updates an existing user
*/
this.put = function(user) {
return $http.patch('/api/users/'+user.login, user);
2015-04-08 22:43:59 +00:00
};
/**
* Deletes a user.
*/
this.delete = function(user) {
return $http.delete('/api/users/'+user.login);
};
/**
* Gets the currently authenticated user from
* the local cache. If not exists, it will fetch
* from the server.
*/
this.getCached = function() {
var defer = $q.defer();
// if the user is already authenticated
if (_user) {
defer.resolve(_user);
return defer.promise;
}
// else fetch the currently authenticated
// user using the REST API.
this.getCurrent().then(function(payload){
_user=payload;
defer.resolve(_user);
}).catch(function(){
defer.resolve(_user);
2015-04-08 22:43:59 +00:00
});
return defer.promise;
}
}
angular
.module('drone')
.service('users', UserService);
})();