Merge branch 'promises' of github.com:tgriesser/Ghost into tgriesser-promises

Conflicts:
	core/shared/models/dataProvider.bookshelf.users.js
This commit is contained in:
Hannah Wolfe 2013-05-26 19:44:34 +01:00
commit 1590a424ef
8 changed files with 67 additions and 126 deletions

1
.gitignore vendored
View File

@ -24,6 +24,7 @@ projectFilesBackup
*.db *.db
/core/admin/assets/css /core/admin/assets/css
/core/admin/assets/sass/modules/bourbon
.sass-cache/ .sass-cache/
/core/admin/assets/sass/config.rb /core/admin/assets/sass/config.rb
/core/admin/assets/sass/layouts/config.rb /core/admin/assets/sass/layouts/config.rb

View File

@ -60,16 +60,14 @@
}); });
}, },
'auth': function (req, res) { 'auth': function (req, res) {
console.log(req.body); api.users.check({email: req.body.email, pw: req.body.password}).then(function (user) {
api.users.find({email: req.body.email, pw: req.body.password}).then(function (user) { console.log('user found: ', user);
if (user) { req.session.user = "ghostadmin";
console.log('user found: ', user); res.redirect(req.query.redirect || '/ghost/');
req.session.user = "ghostadmin"; }, function(err) {
res.redirect(req.query.redirect || '/ghost/'); // Do something here to signal the reason for an error
} else { console.log(err.stack);
res.redirect('/ghost/login/'); res.redirect('/ghost/login/');
}
}); });
}, },
'register': function (req, res) { 'register': function (req, res) {
@ -82,8 +80,10 @@
'doRegister': function (req, res) { 'doRegister': function (req, res) {
// console.log(req.body); // console.log(req.body);
if (req.body.email_address !== '' && req.body.password.length > 5) { if (req.body.email_address !== '' && req.body.password.length > 5) {
// console.log('okay, this is happening'); api.users.add({
api.users.add({email_address: req.body.email_address, password: req.body.password}).then(function (user) { email_address: req.body.email_address,
password: req.body.password
}).then(function (user) {
console.log('user added', user); console.log('user added', user);
res.redirect('/ghost/login/'); res.redirect('/ghost/login/');
}, function (error) { }, function (error) {

View File

@ -15,8 +15,6 @@
JsonDataProvider = require('./shared/models/dataProvider.json'), JsonDataProvider = require('./shared/models/dataProvider.json'),
jsonDataProvider = new JsonDataProvider(), jsonDataProvider = new JsonDataProvider(),
// JugglingDataProvider = require('./shared/models/dataProvider.juggling'),
// jugglingDataProvider = new JugglingDataProvider(),
BookshelfDataProvider = require('./shared/models/dataProvider.bookshelf'), BookshelfDataProvider = require('./shared/models/dataProvider.bookshelf'),
bookshelfDataProvider = new BookshelfDataProvider(), bookshelfDataProvider = new BookshelfDataProvider(),
Ghost, Ghost,

View File

@ -10,7 +10,6 @@
"use strict"; "use strict";
var Ghost = require('../ghost'), var Ghost = require('../ghost'),
when = require('when/node/function'),
_ = require('underscore'), _ = require('underscore'),
ghost = new Ghost(), ghost = new Ghost(),
@ -24,50 +23,50 @@
// takes filter / pagination parameters // takes filter / pagination parameters
// returns a list of posts in a json response // returns a list of posts in a json response
browse: function (options) { browse: function (options) {
return when.call(ghost.dataProvider().posts.findAll, options); return ghost.dataProvider().posts.findAll(options);
}, },
// takes an identifier (id or slug?) // takes an identifier (id or slug?)
// returns a single post in a json response // returns a single post in a json response
read: function (args) { read: function (args) {
return when.call(ghost.dataProvider().posts.findOne, args); return ghost.dataProvider().posts.findOne(args);
}, },
// takes a json object with all the properties which should be updated // takes a json object with all the properties which should be updated
// returns the resulting post in a json response // returns the resulting post in a json response
edit: function (postData) { edit: function (postData) {
return when.call(ghost.dataProvider().posts.edit, postData); return ghost.dataProvider().posts.edit(postData);
}, },
// takes a json object representing a post, // takes a json object representing a post,
// returns the resulting post in a json response // returns the resulting post in a json response
add: function (postData) { add: function (postData) {
return when.call(ghost.dataProvider().posts.add, postData); return ghost.dataProvider().posts.add(postData);
}, },
// takes an identifier (id or slug?) // takes an identifier (id or slug?)
// returns a json response with the id of the deleted post // returns a json response with the id of the deleted post
destroy: function (args) { destroy: function (args) {
return when.call(ghost.dataProvider().posts.destroy, args.id); return ghost.dataProvider().posts.destroy(args.id);
} }
}; };
// # Users // # Users
users = { users = {
add: function (postData) { add: function (postData) {
return when.call(ghost.dataProvider().users.add, postData); return ghost.dataProvider().users.add(postData);
}, },
find: function (postData) { check: function (postData) {
return when.call(ghost.dataProvider().users.check, postData); return ghost.dataProvider().users.check(postData);
} }
}; };
// # Settings // # Settings
settings = { settings = {
browse: function (options) { browse: function (options) {
return when.call(ghost.dataProvider().settings.browse, options); return ghost.dataProvider().settings.browse(options);
}, },
read: function (options) { read: function (options) {
return when.call(ghost.dataProvider().settings.read, options.key); return ghost.dataProvider().settings.read(options.key);
}, },
edit: function (options) { edit: function (options) {
return when.call(ghost.dataProvider().settings.edit, options); return ghost.dataProvider().settings.edit(options);
} }
}; };

View File

@ -20,64 +20,44 @@
/** /**
* Naive find all * Naive find all
* @param args (optional) * @param args (optional)
* @param callback
*/ */
BookshelfBase.prototype.findAll = BookshelfBase.prototype.browse = function (args, callback) { BookshelfBase.prototype.findAll = BookshelfBase.prototype.browse = function (args) {
if (_.isFunction(args)) { args || (args = {});
// Curry the optional args parameter return this.collection.forge(args).fetch();
callback = args;
args = {};
}
this.collection.forge(args).fetch().then(function (results) {
callback(null, results);
}, callback);
}; };
/** /**
* Naive find one where args match * Naive find one where args match
* @param args * @param args
* @param callback
*/ */
BookshelfBase.prototype.findOne = BookshelfBase.prototype.read = function (args, callback) { BookshelfBase.prototype.findOne = BookshelfBase.prototype.read = function (args) {
this.model.forge(args).fetch().then(function (result) { return this.model.forge(args).fetch();
callback(null, result);
}, callback);
}; };
/** /**
* Naive edit * Naive edit
* @param editedObj * @param editedObj
* @param callback
*/ */
BookshelfBase.prototype.edit = BookshelfBase.prototype.update = function (editedObj, callback) { BookshelfBase.prototype.edit = BookshelfBase.prototype.update = function (editedObj) {
this.model.forge({id: editedObj.id}).fetch().then(function (foundObj) { return this.model.forge({id: editedObj.id}).fetch().then(function (foundObj) {
foundObj.set(editedObj).save().then(function (updatedObj) { return foundObj.set(editedObj).save();
callback(null, updatedObj);
}, callback);
}); });
}; };
/** /**
* Naive add * Naive add
* @param newObj * @param newObj
* @param callback
*/ */
BookshelfBase.prototype.add = BookshelfBase.prototype.create = function (newObj, callback) { BookshelfBase.prototype.add = BookshelfBase.prototype.create = function (newObj) {
this.model.forge(newObj).save().then(function (createdObj) { return this.model.forge(newObj).save();
callback(null, createdObj);
}, callback);
}; };
/** /**
* Naive destroy * Naive destroy
* @param _identifier * @param _identifier
* @param callback
*/ */
BookshelfBase.prototype.destroy = BookshelfBase.prototype['delete'] = function (_identifier, callback) { BookshelfBase.prototype.destroy = BookshelfBase.prototype['delete'] = function (_identifier) {
this.model.forge({id: _identifier}).destroy().then(function () { return this.model.forge({id: _identifier}).destroy();
callback(null);
});
}; };
module.exports = BookshelfBase; module.exports = BookshelfBase;

View File

@ -17,25 +17,20 @@
util.inherits(SettingsProvider, BaseProvider); util.inherits(SettingsProvider, BaseProvider);
SettingsProvider.prototype.read = function(_key, callback) { SettingsProvider.prototype.read = function(_key) {
// Allow for just passing the key instead of attributes // Allow for just passing the key instead of attributes
if (_.isString(_key)) { if (_.isString(_key)) {
_key = { key: _key }; _key = { key: _key };
} }
return BaseProvider.prototype.read.call(this, _key);
BaseProvider.prototype.read.call(this, _key, callback);
}; };
SettingsProvider.prototype.edit = function(_data, callback) { SettingsProvider.prototype.edit = function(_data) {
var self = this; return when.all(_.map(_data, function (value, key) {
return this.model.forge({ key: key }).fetch().then(function (setting) {
when.all(_.map(_data, function (value, key) {
return self.model.forge({ key: key }).fetch().then(function (setting) {
return setting.set('value', value).save(); return setting.set('value', value).save();
}); });
})).then(function (settings) { }, this));
callback(null, settings);
}, callback);
}; };
module.exports = SettingsProvider; module.exports = SettingsProvider;

View File

@ -5,6 +5,8 @@
_ = require('underscore'), _ = require('underscore'),
bcrypt = require('bcrypt'), bcrypt = require('bcrypt'),
models = require('./models.js'), models = require('./models.js'),
when = require('when'),
nodefn = require('when/node/function'),
BaseProvider = require('./dataProvider.bookshelf.base.js'), BaseProvider = require('./dataProvider.bookshelf.base.js'),
UsersProvider; UsersProvider;
@ -20,65 +22,34 @@
/** /**
* Naive user add * Naive user add
* @param _user * @param _user
* @param callback
* *
* Hashes the password provided before saving to the database. * Hashes the password provided before saving to the database.
*/ */
UsersProvider.prototype.add = function (_user, callback) { UsersProvider.prototype.add = function (_user) {
var self = this, var self = this,
// Clone the _user so we don't expose the hashed password unnecessarily // Clone the _user so we don't expose the hashed password unnecessarily
userData = _.extend({}, _user); userData = _.extend({}, _user);
this._hashPassword(userData.password, function (err, hash) { return nodefn.call(bcrypt.hash, _user.password, 10).then(function(hash) {
if (err) {
return callback(err);
}
userData.password = hash; userData.password = hash;
return BaseProvider.prototype.add.call(self, userData);
BaseProvider.prototype.add.call(self, userData, function (err, createdUser) {
if (err) {
return callback(err);
}
callback(null, createdUser);
});
}); });
}; };
UsersProvider.prototype._hashPassword = function (password, callback) { /**
bcrypt.genSalt(10, function (err, salt) { * User check
if (err) { * @param _userdata
return callback(err); *
} * Finds the user by email, and check's the password
*/
bcrypt.hash(password, salt, function (err, hash) { UsersProvider.prototype.check = function (_userdata) {
if (err) { console.log('just checking', _userdata);
return callback(err); return this.model.forge({
}
callback(null, hash);
});
});
};
UsersProvider.prototype.check = function (_userdata, callback) {
var test = {
email_address: _userdata.email email_address: _userdata.email
}; }).fetch().then(function (user) {
return nodefn.call(bcrypt.compare, _userdata.pw, user.get('password')).then(function(matched) {
this.model.forge(test).fetch().then(function (user) { if (!matched) return when.reject(new Error('Password does not match'));
var _user; return user;
bcrypt.compare(_userdata.pw, user.attributes.password, function (err, res) {
if (err) {
return callback(err);
}
if (res) {
_user = user;
} else {
_user = false;
}
callback(null, _user);
}); });
}); });
}; };

View File

@ -7,7 +7,7 @@
"use strict"; "use strict";
var _ = require('underscore'), var _ = require('underscore'),
when = require('when'),
DataProvider, DataProvider,
blogData, blogData,
instance, instance,
@ -23,25 +23,22 @@
if (!instance) { if (!instance) {
instance = this; instance = this;
} }
return instance; return instance;
}; };
DataProvider.prototype.globals = {}; DataProvider.prototype.globals = {};
DataProvider.prototype.globals.data = []; DataProvider.prototype.globals.data = [];
DataProvider.prototype.globals.findAll = function (callback) { DataProvider.prototype.globals.findAll = function() {
callback(null, this.data); return when(this.data);
}; };
DataProvider.prototype.globals.save = function (globals, callback) { DataProvider.prototype.globals.save = function (globals) {
var self = this;
_.each(globals, function (global, key) { _.each(globals, function (global, key) {
self.data[key] = global; this.data[key] = global;
}); }, this);
callback(null, globals); return when(globals);
}; };
/* Lets bootstrap with dummy data */ /* Lets bootstrap with dummy data */