mirror of
https://github.com/mclueppers/xo-server.git
synced 2025-04-08 20:55:02 +02:00
Various updates.
This commit is contained in:
parent
d80ff58bb8
commit
bcc400c7d7
70
src/api.js
70
src/api.js
@ -298,7 +298,8 @@ Api.fn.user = {
|
||||
|
||||
return user.setPassword(p_new);
|
||||
}).then(function () {
|
||||
return users.update(user).then(true);
|
||||
/* jshint newcap:false */
|
||||
return users.update(user).thenResolve(true);
|
||||
});
|
||||
},
|
||||
|
||||
@ -384,10 +385,9 @@ Api.fn.user = {
|
||||
// Save the updated user.
|
||||
|
||||
return users.update(user);
|
||||
}).then(
|
||||
true,
|
||||
Q.reject(Api.err.INVALID_PARAMS)
|
||||
);
|
||||
}).thenResolve(true).fail(function () {
|
||||
throw Api.err.INVALID_PARAMS;
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
@ -420,7 +420,8 @@ Api.fn.token = {
|
||||
}
|
||||
|
||||
// @todo Returns NO_SUCH_OBJECT if the token does not exists.
|
||||
return tokens.remove(p_token).then(true);
|
||||
/* jshint newcap:false */
|
||||
return tokens.remove(p_token).thenResolve(true);
|
||||
});
|
||||
},
|
||||
};
|
||||
@ -430,7 +431,7 @@ Api.fn.server = {
|
||||
'add': function (session, req) {
|
||||
var p_host = req.params.host;
|
||||
var p_username = req.params.username;
|
||||
var p_password = req.params.username;
|
||||
var p_password = req.params.password;
|
||||
|
||||
if (!p_host || !p_username || !p_password)
|
||||
{
|
||||
@ -443,18 +444,20 @@ Api.fn.server = {
|
||||
throw Api.err.UNAUTHORIZED;
|
||||
}
|
||||
|
||||
var user = this.users.get(user_id);
|
||||
if (!user.hasPermission('admin'))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
// @todo We are storing passwords which is bad!
|
||||
// Can we use tokens instead?
|
||||
return this.servers.add({
|
||||
'host': p_host,
|
||||
'username': p_username,
|
||||
'password': p_password,
|
||||
// @todo We are storing passwords which is bad!
|
||||
// Can we use tokens instead?
|
||||
return servers.add({
|
||||
'host': p_host,
|
||||
'username': p_username,
|
||||
'password': p_password,
|
||||
});
|
||||
}).then(function (server) {
|
||||
// @todo Connect the server.
|
||||
|
||||
@ -465,26 +468,33 @@ Api.fn.server = {
|
||||
'remove': function (session, req) {
|
||||
var p_id = req.params.id;
|
||||
|
||||
if (undefined === p_id)
|
||||
{
|
||||
throw Api.err.INVALID_PARAMS;
|
||||
}
|
||||
|
||||
var user_id = session.get('user_id');
|
||||
if (undefined === user_id)
|
||||
{
|
||||
throw Api.err.UNAUTHORIZED;
|
||||
}
|
||||
|
||||
var user = this.users.get(user_id);
|
||||
if (!user.hasPermission('admin'))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
if (!this.servers.exists(p_id))
|
||||
{
|
||||
throw Api.err.NO_SUCH_OBJECT;
|
||||
}
|
||||
return servers.remove(p_id);
|
||||
}).then(function(success) {
|
||||
if (!success)
|
||||
{
|
||||
throw Api.err.NO_SUCH_OBJECT;
|
||||
}
|
||||
|
||||
// @todo Disconnect the server.
|
||||
|
||||
return this.servers.remove(p_id).then(true);
|
||||
return true;
|
||||
});
|
||||
},
|
||||
|
||||
'connect': function () {
|
||||
|
@ -33,7 +33,10 @@ Collection.prototype.add = function (models) {
|
||||
array = false;
|
||||
}
|
||||
|
||||
_.each(models, function (model, i) {
|
||||
for (var i = 0, n = models.length; i < n; ++i)
|
||||
{
|
||||
var model = models[i];
|
||||
|
||||
if ( !(model instanceof this.model) )
|
||||
{
|
||||
model = new this.model(model);
|
||||
@ -62,7 +65,7 @@ Collection.prototype.add = function (models) {
|
||||
}
|
||||
|
||||
this.models[id] = model.properties;
|
||||
}, this);
|
||||
}
|
||||
|
||||
/* jshint newcap: false */
|
||||
return Q(array ? models : models[0]);
|
||||
@ -112,16 +115,7 @@ Collection.prototype.where = function (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;
|
||||
}));
|
||||
return Q(_.where(this.models, properties));
|
||||
};
|
||||
|
||||
|
||||
@ -157,34 +151,28 @@ Collection.prototype.update = function (models) {
|
||||
}
|
||||
|
||||
// @todo Rewrite.
|
||||
_.each(models, function (properties, i) {
|
||||
if (properties instanceof this.model)
|
||||
for (var i = 0; i < models.length; i++)
|
||||
{
|
||||
var model = models[i];
|
||||
|
||||
if (model instanceof this.model)
|
||||
{
|
||||
properties = properties.properties;
|
||||
model = model.properties;
|
||||
}
|
||||
|
||||
// @todo
|
||||
// var error = model.validate();
|
||||
// if (undefined !== error)
|
||||
// {
|
||||
// // @todo Better system inspired by Backbone.js.
|
||||
// throw error;
|
||||
// }
|
||||
var id = model.id;
|
||||
|
||||
var id = properties.id;
|
||||
|
||||
var model = this.models[id];
|
||||
|
||||
// Missing models are ignored.
|
||||
if (!model)
|
||||
// Missing models should be added not updated.
|
||||
if (!this.models[id])
|
||||
{
|
||||
return Q.reject('missing model!');
|
||||
return Q.reject('missing model');
|
||||
}
|
||||
|
||||
_.extend(model.properties, model);
|
||||
// @todo Model validation.
|
||||
|
||||
models[i] = model;
|
||||
});
|
||||
// @todo Event handling.
|
||||
_.extend(this.models[id], model);
|
||||
}
|
||||
|
||||
/* jshint newcap: false */
|
||||
return Q(array ? models : models[0]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user