mirror of
https://github.com/mclueppers/xo-server.git
synced 2025-07-23 22:15:47 +02:00
Various updates.
This commit is contained in:
parent
a51d7663c6
commit
2134e3b4d1
72
src/api.js
72
src/api.js
@ -143,7 +143,10 @@ Api.fn.session = {
|
|||||||
throw Api.err.ALREADY_AUTHENTICATED;
|
throw Api.err.ALREADY_AUTHENTICATED;
|
||||||
}
|
}
|
||||||
|
|
||||||
var user = this.users.findWhere({'email': p_email});
|
return this.users.findWhere({'email': p_email}).then(function (user) {
|
||||||
|
|
||||||
|
console.log(user);
|
||||||
|
|
||||||
if (!user)
|
if (!user)
|
||||||
{
|
{
|
||||||
throw Api.err.INVALID_CREDENTIAL;
|
throw Api.err.INVALID_CREDENTIAL;
|
||||||
@ -158,6 +161,7 @@ Api.fn.session = {
|
|||||||
session.set('user_id', user.get('id'));
|
session.set('user_id', user.get('id'));
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
'signInWithToken': function (session, req) {
|
'signInWithToken': function (session, req) {
|
||||||
@ -236,8 +240,70 @@ Api.fn.user = {
|
|||||||
throw Api.err.NOT_IMPLEMENTED;
|
throw Api.err.NOT_IMPLEMENTED;
|
||||||
},
|
},
|
||||||
|
|
||||||
'set': function () {
|
'set': function (session, request) {
|
||||||
throw Api.err.NOT_IMPLEMENTED;
|
var user_id = session.get('user_id');
|
||||||
|
if (undefined === user_id)
|
||||||
|
{
|
||||||
|
throw Api.err.UNAUTHORIZED;
|
||||||
|
}
|
||||||
|
|
||||||
|
var p_email, p_password, p_permission;
|
||||||
|
|
||||||
|
var users = this.users;
|
||||||
|
|
||||||
|
return users.get(user_id).then(function (user) {
|
||||||
|
// Get the current user to check its permission.
|
||||||
|
|
||||||
|
if (!user.hasPermission('admin'))
|
||||||
|
{
|
||||||
|
throw Api.err.UNAUTHORIZED;
|
||||||
|
}
|
||||||
|
|
||||||
|
var p_id = request.params.id;
|
||||||
|
p_email = request.params.email;
|
||||||
|
p_password = request.params.password;
|
||||||
|
p_permission = request.params.permission;
|
||||||
|
|
||||||
|
/* jshint laxbreak: true */
|
||||||
|
if ((undefined === p_id)
|
||||||
|
|| ((undefined === p_email)
|
||||||
|
&& (undefined === p_password)
|
||||||
|
&& (undefined === p_permission)))
|
||||||
|
{
|
||||||
|
throw Api.err.INVALID_PARAMS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @todo Check there are no invalid parameter.
|
||||||
|
|
||||||
|
return users.get(p_id);
|
||||||
|
}).then(function (user) {
|
||||||
|
// Gets the user to update.
|
||||||
|
|
||||||
|
// @todo Check undefined value are ignored.
|
||||||
|
user.set({
|
||||||
|
'email': p_email,
|
||||||
|
'permission': p_permission,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (p_password)
|
||||||
|
{
|
||||||
|
return user.setPassword(p_password).then(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}).then(function (user) {
|
||||||
|
// Save the updated user.
|
||||||
|
|
||||||
|
return users.update(user);
|
||||||
|
}).then(
|
||||||
|
function () {
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
function () {
|
||||||
|
// @todo Find a better error.
|
||||||
|
return Api.err.INVALID_PARAMS;
|
||||||
|
}
|
||||||
|
);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
var _ = require('underscore');
|
var _ = require('underscore');
|
||||||
var Q = require('q');
|
var Q = require('q');
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// @todo Add events.
|
// @todo Add events.
|
||||||
function Collection(items)
|
function Collection(items)
|
||||||
{
|
{
|
||||||
@ -34,7 +36,7 @@ Collection.prototype.add = function (items) {
|
|||||||
_.each(items, function (item, i) {
|
_.each(items, function (item, i) {
|
||||||
if ( !(item instanceof this.model) )
|
if ( !(item instanceof this.model) )
|
||||||
{
|
{
|
||||||
item = new this.model(item);
|
item = new (this.model)(item);
|
||||||
items[i] = item;
|
items[i] = item;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,8 +61,8 @@ Collection.prototype.add = function (items) {
|
|||||||
return Q.reject('cannot add existing items!');
|
return Q.reject('cannot add existing items!');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.items[id] = item;
|
this.items[id] = item.properties;
|
||||||
});
|
}, this);
|
||||||
|
|
||||||
/* jshint newcap: false */
|
/* jshint newcap: false */
|
||||||
return Q(array ? items : items[0]);
|
return Q(array ? items : items[0]);
|
||||||
@ -77,7 +79,9 @@ Collection.prototype.exists = function (id) {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
Collection.prototype.findWhere = function (properties) {
|
Collection.prototype.findWhere = function (properties) {
|
||||||
return _.findWhere(this.items, properties);
|
/* jshint newcap: false */
|
||||||
|
|
||||||
|
return Q(_.findWhere(this.items, properties));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -155,5 +159,6 @@ Collection.prototype.set = function (/*items*/) {
|
|||||||
|
|
||||||
Collection.extend = require('extendable');
|
Collection.extend = require('extendable');
|
||||||
|
|
||||||
// Export.
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
module.exports = Collection;
|
module.exports = Collection;
|
||||||
|
@ -72,7 +72,8 @@ function json_api_call(session, message)
|
|||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// @todo Port should be configurable.
|
// @todo Port should be configurable.
|
||||||
require('socket.io').listen(8080).sockets.on('connection', function (socket) {
|
require('socket.io').listen(8080, {'log level': 0}).sockets
|
||||||
|
.on('connection', function (socket) {
|
||||||
var session = new Session(xo);
|
var session = new Session(xo);
|
||||||
session.once('close', function () {
|
session.once('close', function () {
|
||||||
socket.disconnect();
|
socket.disconnect();
|
||||||
|
34
src/model.js
34
src/model.js
@ -1,11 +1,13 @@
|
|||||||
var _ = require('underscore');
|
var _ = require('underscore');
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
function Model(properties)
|
function Model(properties)
|
||||||
{
|
{
|
||||||
// Parent constructor.
|
// Parent constructor.
|
||||||
Model.super_.call(this);
|
Model.super_.call(this);
|
||||||
|
|
||||||
this.properties = {};
|
this.properties = this['default'];
|
||||||
|
|
||||||
if (properties)
|
if (properties)
|
||||||
{
|
{
|
||||||
@ -37,12 +39,6 @@ Model.prototype.get = function (property, def) {
|
|||||||
return prop;
|
return prop;
|
||||||
}
|
}
|
||||||
|
|
||||||
prop = this['default'][property];
|
|
||||||
if (undefined !== prop)
|
|
||||||
{
|
|
||||||
return prop;
|
|
||||||
}
|
|
||||||
|
|
||||||
return def;
|
return def;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -64,17 +60,36 @@ Model.prototype.set = function (properties, value) {
|
|||||||
properties[property] = value;
|
properties[property] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var previous = {};
|
||||||
|
|
||||||
var model = this;
|
var model = this;
|
||||||
_.each(properties, function (value, key) {
|
_.each(properties, function (value, key) {
|
||||||
|
|
||||||
//model.properties =
|
var prev = model.get(key);
|
||||||
|
|
||||||
|
// New value.
|
||||||
|
if (value !== prev)
|
||||||
|
{
|
||||||
|
previous[key] = prev;
|
||||||
|
model.properties[key] = value;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!_.isEmpty(previous))
|
||||||
|
{
|
||||||
|
this.emit('change', previous);
|
||||||
|
|
||||||
|
_.each(previous, function (previous, property) {
|
||||||
|
this.emit('change:'+ property, previous);
|
||||||
|
}, this);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unsets properties.
|
* Unsets properties.
|
||||||
*/
|
*/
|
||||||
Model.prototype.unset = function (properties) {
|
Model.prototype.unset = function (properties) {
|
||||||
|
// @todo Events.
|
||||||
this.properties = _.omit(this.properties, properties);
|
this.properties = _.omit(this.properties, properties);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -87,5 +102,6 @@ Model.prototype['default'] = {};
|
|||||||
|
|
||||||
Model.extend = require('extendable');
|
Model.extend = require('extendable');
|
||||||
|
|
||||||
// Export.
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
module.exports = Model;
|
module.exports = Model;
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
var Model = require('./model');
|
var Model = require('./model');
|
||||||
|
|
||||||
module.exports = Model.extend({
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
var Session = Model.extend({
|
||||||
'constructor': function (xo) {
|
'constructor': function (xo) {
|
||||||
Model.call(this);
|
Model.call(this);
|
||||||
|
|
||||||
@ -45,3 +47,7 @@ module.exports = Model.extend({
|
|||||||
this.emit('close');
|
this.emit('close');
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
module.exports = Session;
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
var Q = require('q');
|
var Q = require('q');
|
||||||
var xmlrpc = require('xmlrpc');
|
var xmlrpc = require('xmlrpc');
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
function Xapi(host)
|
function Xapi(host)
|
||||||
{
|
{
|
||||||
// Parent constructor.
|
// Parent constructor.
|
||||||
@ -48,3 +50,7 @@ Xapi.prototype.connect = function (username, password) {
|
|||||||
self.sessionId = session_id;
|
self.sessionId = session_id;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
module.exports = Xapi;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user