API method: user.getAll().

This commit is contained in:
Chris Allard 2013-07-11 15:38:38 +02:00 committed by Julien Fontanet
parent 1c61ec3bd3
commit 93bb0e9097
2 changed files with 49 additions and 3 deletions

View File

@ -289,8 +289,28 @@ Api.fn.user = {
},
'getAll': function () {
throw Api.err.NOT_IMPLEMENTED;
'getAll': function (session) {
var user_id = session.get('user_id');
if (undefined === user_id)
{
throw Api.err.UNAUTHORIZED;
}
var users = this.users;
return users.get(user_id).then(function (user) {
if (!user.hasPermission('admin'))
{
throw Api.err.UNAUTHORIZED;
}
return users.where();
}).then(function (all_users) {
_.each(all_users, function (user, i) {
all_users[i] = _.pick(user, 'id', 'email', 'permission');
});
return all_users;
});
},
'set': function (session, request) {

View File

@ -89,7 +89,7 @@ Collection.prototype.exists = function (id) {
};
/**
*
* Find the first model which has a given set of properties.
*/
Collection.prototype.findWhere = function (properties) {
/* jshint newcap: false */
@ -99,6 +99,32 @@ Collection.prototype.findWhere = function (properties) {
return Q(model ? new this.model(model) : null);
};
/**
* Find all models which have a given set of properties.
*
* /!\: Does not return instance of this.model.
*/
Collection.prototype.where = function (properties) {
/* jshint newcap: false */
if (_.isEmpty(properties))
{
return Q(this.models.slice());
}
return Q(_.filter(this.models, function (model) {
for (var property in properties)
{
if (model[property] !== properties[property])
{
return false;
}
}
return true;
}));
};
/**
* Removes models from this collection.
*/