Merge pull request #373 from javorszky/iss340

Current user added
This commit is contained in:
Hannah Wolfe 2013-08-18 09:01:12 -07:00
commit bbe5d935b2
10 changed files with 65 additions and 43 deletions

View File

@ -3,11 +3,11 @@
"use strict";
Ghost.Models.User = Backbone.Model.extend({
url: Ghost.settings.apiRoot + '/users/1'
url: Ghost.settings.apiRoot + '/users/me'
});
// Ghost.Collections.Users = Backbone.Collection.extend({
// url: Ghost.settings.apiRoot + '/users'
// });
}());
}());

View File

@ -60,7 +60,7 @@
error: function (obj, string, status) {
Ghost.notifications.addItem({
type: 'error',
message: 'Invalid username or password',
message: obj.responseText,
status: 'passive'
});
}
@ -102,4 +102,4 @@
});
}
});
}());
}());

View File

@ -184,6 +184,7 @@
'click .button-change-password': 'changePassword'
},
saveUser: function () {
this.model.save({
'full_name': this.$('#user-name').val(),
@ -203,7 +204,6 @@
event.preventDefault();
var self = this,
email = this.$('#user-email').val(),
oldPassword = this.$('#user-password-old').val(),
newPassword = this.$('#user-password-new').val(),
ne2Password = this.$('#user-new-password-verification').val();
@ -217,7 +217,6 @@
url: '/ghost/changepw/',
type: 'POST',
data: {
email: email,
password: oldPassword,
newpassword: newPassword,
ne2password: ne2Password

View File

@ -289,4 +289,4 @@ Ghost.prototype.initTheme = function (app) {
// TODO: Expose the defaults for other people to see/manipulate as a static value?
// Ghost.defaults = defaults;
module.exports = Ghost;
module.exports = Ghost;

View File

@ -75,6 +75,10 @@ users = {
// **takes:** an identifier (id or slug?)
read: function read(args) {
// **returns:** a promise for a single user in a json object
if (args.id === 'me') {
args = {id: this.user};
}
return dataProvider.User.read(args);
},
@ -83,6 +87,7 @@ users = {
// **takes:** a json object representing a user
edit: function edit(userData) {
// **returns:** a promise for the resulting user in a json object
userData.id = this.user;
return dataProvider.User.edit(userData);
},
@ -223,8 +228,12 @@ settings = {
// takes the API method and wraps it so that it gets data from the request and returns a sensible JSON response
requestHandler = function (apiMethod) {
return function (req, res) {
var options = _.extend(req.body, req.query, req.params);
return apiMethod(options).then(function (result) {
var options = _.extend(req.body, req.query, req.params),
apiContext = {
user: req.session && req.session.user
};
return apiMethod.call(apiContext, options).then(function (result) {
res.json(result || {});
}, function (error) {
res.json(400, {error: error});
@ -273,4 +282,4 @@ module.exports.users = users;
module.exports.notifications = notifications;
module.exports.settings = settings;
module.exports.requestHandler = requestHandler;
module.exports.cachedSettingsRequestHandler = cachedSettingsRequestHandler;
module.exports.cachedSettingsRequestHandler = cachedSettingsRequestHandler;

View File

@ -94,15 +94,15 @@ adminControllers = {
},
'auth': function (req, res) {
api.users.check({email: req.body.email, pw: req.body.password}).then(function (user) {
req.session.user = "ghostadmin";
req.session.user = user.id;
res.json(200, {redirect: req.query.r ? '/ghost/' + req.query.r : '/ghost/'});
}, function (error) {
res.send(401);
res.send(401, error.message);
});
},
changepw: function (req, res) {
api.users.changePassword({
email: req.body.email,
currentUser: req.session.user,
oldpw: req.body.password,
newpw: req.body.newpassword,
ne2pw: req.body.ne2password
@ -330,4 +330,4 @@ adminControllers = {
}
};
module.exports = adminControllers;
module.exports = adminControllers;

View File

@ -79,7 +79,7 @@ GhostBookshelf.Model = GhostBookshelf.Model.extend({
edit: function (editedObj, options) {
options = options || {};
return this.forge({id: editedObj.id}).fetch(options).then(function (foundObj) {
return foundObj.set(editedObj).save();
return foundObj.save(editedObj);
});
},
@ -117,4 +117,4 @@ GhostBookshelf.Model = GhostBookshelf.Model.extend({
});
module.exports = GhostBookshelf;
module.exports = GhostBookshelf;

View File

@ -91,18 +91,18 @@ User = GhostBookshelf.Model.extend({
* whether there's anyone registered at all. This is due to #138
* @author javorszky
*/
/**
return this.forge({email_address: userData.email_address}).fetch().then(function (user) {
if (!!user.attributes.email_address) {
return when.reject(new Error('A user with that email address already exists.'));
}
return nodefn.call(bcrypt.hash, _user.password, null, null).then(function (hash) {
userData.password = hash;
return GhostBookshelf.Model.add.call(User, userData);
});
});
*/
// return this.forge({email_address: userData.email_address}).fetch().then(function (user) {
// if (user !== null) {
// return when.reject(new Error('A user with that email address already exists.'));
// }
// return nodefn.call(bcrypt.hash, _user.password, null, null).then(function (hash) {
// userData.password = hash;
// GhostBookshelf.Model.add.call(UserRole, userRoles);
// return GhostBookshelf.Model.add.call(User, userData);
// }, errors.logAndThrowError);
// }, errors.logAndThrowError);
},
// Finds the user by email, and checks the password
@ -116,7 +116,9 @@ User = GhostBookshelf.Model.extend({
}
return user;
}, errors.logAndThrowError);
}, errors.logAndThrowError);
}, function (error) {
return when.reject(new Error('Email address or password is incorrect'));
});
},
/**
@ -125,7 +127,7 @@ User = GhostBookshelf.Model.extend({
*
*/
changePassword: function (_userdata) {
var email = _userdata.email,
var userid = _userdata.currentUser,
oldPassword = _userdata.oldpw,
newPassword = _userdata.newpw,
ne2Password = _userdata.ne2pw;
@ -135,7 +137,7 @@ User = GhostBookshelf.Model.extend({
}
return this.forge({
email_address: email
id: userid
}).fetch({require: true}).then(function (user) {
return nodefn.call(bcrypt.compare, oldPassword, user.get('password'))
.then(function (matched) {

View File

@ -9,8 +9,8 @@
<li id="usermenu" class="subnav">
<a href="#" data-toggle="ul" class="dropdown">
<img class="avatar" src="/public/img/user.jpg" alt="Avatar" />
<span class="name">Ghost v{{version}}</span>
<img class="avatar" src="{{#if currentUser.profile}}{{currentUser.profile}}{{else}}/public/img/user.jpg{{/if}}" alt="Avatar" />
<span class="name">{{#if currentUser.name}}{{currentUser.name}}{{else}}Ghost{{/if}} v{{version}}</span>
</a>
<ul class="overlay">
<li class="usermenu-profile"><a href="#">Your Profile</a></li>

View File

@ -40,10 +40,8 @@ function auth(req, res, next) {
req.flash('warn', "Please login");
redirect = '?r=' + encodeURIComponent(path);
}
return res.redirect('/ghost/login/' + redirect);
}
next();
}
@ -88,15 +86,29 @@ function ghostLocals(req, res, next) {
next();
});
} else {
_.extend(res.locals, {
// pass the admin flash messages, settings and paths
messages: ghost.notifications,
settings: ghost.settings(),
availableThemes: ghost.paths().availableThemes,
availablePlugins: ghost.paths().availablePlugins
api.users.read({id: req.session.user}).then(function (currentUser) {
_.extend(res.locals, {
// pass the admin flash messages, settings and paths
messages: ghost.notifications,
settings: ghost.settings(),
availableThemes: ghost.paths().availableThemes,
availablePlugins: ghost.paths().availablePlugins,
currentUser: {
name: currentUser.attributes.full_name,
profile: currentUser.attributes.profile_picture
}
});
next();
}).otherwise(function () {
_.extend(res.locals, {
// pass the admin flash messages, settings and paths
messages: ghost.notifications,
settings: ghost.settings(),
availableThemes: ghost.paths().availableThemes,
availablePlugins: ghost.paths().availablePlugins
});
next();
});
next();
}
}
@ -232,4 +244,4 @@ when.all([ghost.init(), filters.loadCoreFilters(ghost), helpers.loadCoreHelpers(
loading.resolve();
}
);
}, errors.logAndThrowError);
}, errors.logAndThrowError);