Minor bug fixes. Complete MySQL backend support for User management

This commit is contained in:
Martin Dobrev 2013-10-25 08:58:55 +01:00
parent 7bc47a5ab9
commit 1a6aa64a1a
5 changed files with 75 additions and 26 deletions

View File

@ -99,4 +99,15 @@ mysql:
# If uri is specified the above configuration options are not in use
#
# Default: mysql://xoauser:xoapass@localhost/xoa
#uri: mysql://xoauser:xoapass@localhost/xoa
#uri: mysql://xoauser:xoapass@localhost/xoa
# Configuration of the userdb
userdb:
# Type of the backend?
#
# Syntax: mysql, redis, postgres, ldap
#
# /!\ Currently supported backends: redis, mysql
#
# Default: redis
#type: redis

View File

@ -29,18 +29,12 @@ function MySQL(options, models)
options = {};
}
if (!options.uri)
{
throw 'MySQL missing option: uri';
}
_.defaults(options, {
'indexes': [],
});
MySQL.super_.call(this, models);
this.uri = options.uri;
this.indexes = options.indexes;
this.prefix = options.prefix;
}
@ -159,8 +153,6 @@ MySQL.prototype._add = function (models, options)
};
_.each(models, function (model) {
var promise;
// Extend the schema adding missing values
var tday = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''); // @todo: use of nodejs library maybe?!
@ -175,11 +167,11 @@ MySQL.prototype._add = function (models, options)
activationtoken: null
};
console.log(data);
if (!replace) {
promises.push(knex('users').insert(data));
} else {
delete data.created;
//data.lastmodified(tday);
promises.push(knex('users').where('email', model.email).update(data));
}
});
@ -200,20 +192,16 @@ MySQL.prototype._remove = function (ids)
keys.push(ids[i]);
}
console.log(keys);
console.log(ids);
// @todo Handle indexes.
//promises.push(knex('users').whereIn('email', keys).del());
promises.push(true);
promises.push(knex('users').whereIn('email', keys).del());
return Q.all(promises);
};
MySQL.prototype._update = function (models)
{
console.info('Not yet implemented: _update');
// _add already handles variable sanitation
return this._add(models, {replace: true});
};

View File

@ -91,8 +91,6 @@ Redis.prototype._add = function (models, options) {
var promises = [];
console.log(models);
_.each(models, function (model) {
var promise;
@ -195,7 +193,6 @@ Redis.prototype._get = function (properties) {
ids = [id];
}
console.log(ids);
return self._extract(ids);
});
};

View File

@ -369,7 +369,7 @@ cfg.merge({
'binddn' : 'dc=example,dc=com',
},
'userdb': {
'type' : 'mysql' // Accepted values 'redis', 'mysql' and 'ldap'. More to come later on
'type' : 'redis' // Accepted values 'redis', 'mysql' and 'ldap'. More to come later on
},
// End of mod by Martin Dobrev
});
@ -422,6 +422,11 @@ read_file(__dirname +'/../config/local.yaml').then(
var username = cfg.get('mysql', 'username').toString();
var password = cfg.get('mysql', 'password').toString();
// Initialize Knex
//
// /!\ Initialize it only once
//
// on later stage use knex = require('knex').knex for DB queries
Knex.knex = Knex.initialize(
{
client: 'mysql',

View File

@ -5,7 +5,6 @@ var Q = require('q');
var MemoryCollection = require('./collection/memory');
var RedisCollection = require('./collection/redis');
var MySQLCollection = require('./collection/mysql');
var Model = require('./model');
var Xapi = require('./xapi');
@ -136,8 +135,12 @@ var User = Model.extend({
},
});
/* Mod by Martin Dobrev
This snippet is obsolete now
Take a look at the InitializeUsersBackend function
// @todo handle email uniqueness.
var Users = MySQLCollection.extend({
var Users = RedisCollection.extend({
'model': User,
'create': function (email, password, permission) {
@ -155,6 +158,47 @@ var Users = MySQLCollection.extend({
});
}
});
*/
function InitializeUsersBackend(backend)
{
var collection = null;
switch (backend)
{
case 'redis' : collection = require('./collection/redis'); break;
case 'mysql' : collection = require('./collection/mysql'); break;
case 'postgre' : break;
case 'sqlite' : break;
case 'ldap' : break;
default : throw "Userdb backend not supported: " + backend; break;
}
if (collection !== null) {
console.info('UserDB initialized with %s backend', backend);
return collection.extend({
'model': User,
'create': function (email, password, permission) {
var user = new User({
'email': email,
});
if (permission)
{
user.set('permission', permission);
}
var self = this;
return user.setPassword(password).then(function () {
return self.add(user);
});
}
});
} else {
throw "Unable to initialize UserDB Collection";
}
}
//--------------------------------------------------------------------
@ -230,6 +274,10 @@ Xo.prototype.computeStats = _.throttle(function () {
Xo.prototype.start = function (cfg) {
var xo = this;
var redis = require('then-redis').createClient(cfg.get('redis', 'uri'));
// Modified by Martin Dobrev @ 2013-10-25
var userdb = InitializeUsersBackend(cfg.get('userdb', 'type'));
// End of mod
//--------------------------------------
// Persistent collections.
@ -244,10 +292,10 @@ Xo.prototype.start = function (cfg) {
'prefix': 'xo:token',
'indexes': ['user_id'],
});
xo.users = new Users({
// Mod by Martin Dobrev - Replaced Users with the result of the UserDB initializer
xo.users = new userdb({
'connection': redis,
'prefix' : 'xo:user',
'uri' : cfg.get('mysql', 'uri'),
'indexes' : ['email'],
});