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