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
/core/admin/assets/css
/core/admin/assets/sass/modules/bourbon
.sass-cache/
/core/admin/assets/sass/config.rb
/core/admin/assets/sass/layouts/config.rb

View File

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

View File

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

View File

@ -10,7 +10,6 @@
"use strict";
var Ghost = require('../ghost'),
when = require('when/node/function'),
_ = require('underscore'),
ghost = new Ghost(),
@ -24,50 +23,50 @@
// takes filter / pagination parameters
// returns a list of posts in a json response
browse: function (options) {
return when.call(ghost.dataProvider().posts.findAll, options);
return ghost.dataProvider().posts.findAll(options);
},
// takes an identifier (id or slug?)
// returns a single post in a json response
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
// returns the resulting post in a json response
edit: function (postData) {
return when.call(ghost.dataProvider().posts.edit, postData);
return ghost.dataProvider().posts.edit(postData);
},
// takes a json object representing a post,
// returns the resulting post in a json response
add: function (postData) {
return when.call(ghost.dataProvider().posts.add, postData);
return ghost.dataProvider().posts.add(postData);
},
// takes an identifier (id or slug?)
// returns a json response with the id of the deleted post
destroy: function (args) {
return when.call(ghost.dataProvider().posts.destroy, args.id);
return ghost.dataProvider().posts.destroy(args.id);
}
};
// # Users
users = {
add: function (postData) {
return when.call(ghost.dataProvider().users.add, postData);
return ghost.dataProvider().users.add(postData);
},
find: function (postData) {
return when.call(ghost.dataProvider().users.check, postData);
check: function (postData) {
return ghost.dataProvider().users.check(postData);
}
};
// # Settings
settings = {
browse: function (options) {
return when.call(ghost.dataProvider().settings.browse, options);
return ghost.dataProvider().settings.browse(options);
},
read: function (options) {
return when.call(ghost.dataProvider().settings.read, options.key);
return ghost.dataProvider().settings.read(options.key);
},
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
* @param args (optional)
* @param callback
*/
BookshelfBase.prototype.findAll = BookshelfBase.prototype.browse = function (args, callback) {
if (_.isFunction(args)) {
// Curry the optional args parameter
callback = args;
args = {};
}
this.collection.forge(args).fetch().then(function (results) {
callback(null, results);
}, callback);
BookshelfBase.prototype.findAll = BookshelfBase.prototype.browse = function (args) {
args || (args = {});
return this.collection.forge(args).fetch();
};
/**
* Naive find one where args match
* @param args
* @param callback
*/
BookshelfBase.prototype.findOne = BookshelfBase.prototype.read = function (args, callback) {
this.model.forge(args).fetch().then(function (result) {
callback(null, result);
}, callback);
BookshelfBase.prototype.findOne = BookshelfBase.prototype.read = function (args) {
return this.model.forge(args).fetch();
};
/**
* Naive edit
* @param editedObj
* @param callback
*/
BookshelfBase.prototype.edit = BookshelfBase.prototype.update = function (editedObj, callback) {
this.model.forge({id: editedObj.id}).fetch().then(function (foundObj) {
foundObj.set(editedObj).save().then(function (updatedObj) {
callback(null, updatedObj);
}, callback);
BookshelfBase.prototype.edit = BookshelfBase.prototype.update = function (editedObj) {
return this.model.forge({id: editedObj.id}).fetch().then(function (foundObj) {
return foundObj.set(editedObj).save();
});
};
/**
* Naive add
* @param newObj
* @param callback
*/
BookshelfBase.prototype.add = BookshelfBase.prototype.create = function (newObj, callback) {
this.model.forge(newObj).save().then(function (createdObj) {
callback(null, createdObj);
}, callback);
BookshelfBase.prototype.add = BookshelfBase.prototype.create = function (newObj) {
return this.model.forge(newObj).save();
};
/**
* Naive destroy
* @param _identifier
* @param callback
*/
BookshelfBase.prototype.destroy = BookshelfBase.prototype['delete'] = function (_identifier, callback) {
this.model.forge({id: _identifier}).destroy().then(function () {
callback(null);
});
BookshelfBase.prototype.destroy = BookshelfBase.prototype['delete'] = function (_identifier) {
return this.model.forge({id: _identifier}).destroy();
};
module.exports = BookshelfBase;

View File

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

View File

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

View File

@ -7,7 +7,7 @@
"use strict";
var _ = require('underscore'),
when = require('when'),
DataProvider,
blogData,
instance,
@ -23,25 +23,22 @@
if (!instance) {
instance = this;
}
return instance;
};
DataProvider.prototype.globals = {};
DataProvider.prototype.globals.data = [];
DataProvider.prototype.globals.findAll = function (callback) {
callback(null, this.data);
DataProvider.prototype.globals.findAll = function() {
return when(this.data);
};
DataProvider.prototype.globals.save = function (globals, callback) {
var self = this;
DataProvider.prototype.globals.save = function (globals) {
_.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 */