API method: server.getAll().

This commit is contained in:
Julien Fontanet 2013-07-11 17:29:04 +02:00
parent bcc400c7d7
commit 9adfec28cf
2 changed files with 41 additions and 1 deletions

View File

@ -119,6 +119,10 @@ Api.err = {
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Helper functions that should be written:
// - checkParams(req.params, param1, ..., paramN).then(...)
// - isAuthorized(session, [permission]).then(...)
Api.fn = {}; Api.fn = {};
Api.fn.api = { Api.fn.api = {
@ -303,7 +307,6 @@ Api.fn.user = {
}); });
}, },
'getAll': function (session) { 'getAll': function (session) {
var user_id = session.get('user_id'); var user_id = session.get('user_id');
if (undefined === user_id) if (undefined === user_id)
@ -497,6 +500,30 @@ Api.fn.server = {
}); });
}, },
'getAll': function (session) {
var user_id = session.get('user_id');
if (undefined === user_id)
{
throw Api.err.UNAUTHORIZED;
}
var servers = this.servers;
return this.users.get(user_id).then(function (user) {
if (!user.hasPermission('admin'))
{
throw Api.err.UNAUTHORIZED;
}
return servers.where();
}).then(function (all_servers) {
_.each(all_servers, function (server, i) {
all_servers[i] = _.pick(server, 'id', 'host', 'username');
});
return all_servers;
});
},
'connect': function () { 'connect': function () {
throw Api.err.NOT_IMPLEMENTED; throw Api.err.NOT_IMPLEMENTED;
}, },

View File

@ -3,6 +3,19 @@ var Q = require('q');
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// @todo Maybe we should generalize the getter methods (get,
// findWhere, where) to two methods: get([properties]) &
// first([properties]).
//
// Each of these methods accept optionnal properties to filter its
// results.
//
// get() returns any models that match while first() returns only the
// one.
//
// These method should also accept a scalar value as a matching value
// for the “id” property.
// @todo Add events. // @todo Add events.
function Collection(models) function Collection(models)
{ {