From 9adfec28cfd7d3cffc3ee84f14bf72214d83250e Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 11 Jul 2013 17:29:04 +0200 Subject: [PATCH] API method: server.getAll(). --- src/api.js | 29 ++++++++++++++++++++++++++++- src/collection.js | 13 +++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/api.js b/src/api.js index 3a846de..cdf2f11 100644 --- a/src/api.js +++ b/src/api.js @@ -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 = { @@ -303,7 +307,6 @@ Api.fn.user = { }); }, - 'getAll': function (session) { var user_id = session.get('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 () { throw Api.err.NOT_IMPLEMENTED; }, diff --git a/src/collection.js b/src/collection.js index edf736d..037e92a 100644 --- a/src/collection.js +++ b/src/collection.js @@ -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. function Collection(models) {