Fix direct access to settings/user

closes #3162
- removes injection of user object in application route's beforeModel
- removes injection/cleanup of user object in signedIn/signedOut actions
- removes loading of user and passing to signedIn action in signup/setup controllers
- adds 'user' property to session object
- updates header nav to reference session.user
- sets model of settings/user route to session.user and forces reload
- on leaving settings/user, rollback any unsaved changes
This commit is contained in:
Kevin Ansfield 2014-07-01 17:58:26 +02:00
parent 087c483498
commit 7bfe6e9db7
7 changed files with 51 additions and 63 deletions

View File

@ -1,9 +1,12 @@
/*global alert */ /*global alert */
var SettingsUserController = Ember.Controller.extend({ var SettingsUserController = Ember.ObjectController.extend({
user: Ember.computed.alias('model'),
coverDefault: '/shared/img/user-cover.png', coverDefault: '/shared/img/user-cover.png',
cover: function () { cover: function () {
// @TODO: add {{asset}} subdir path // @TODO: add {{asset}} subdir path
var cover = this.user.get('cover'); var cover = this.get('user.cover');
if (typeof cover !== 'string') { if (typeof cover !== 'string') {
cover = this.get('coverDefault'); cover = this.get('coverDefault');
} }
@ -16,35 +19,36 @@ var SettingsUserController = Ember.Controller.extend({
image: function () { image: function () {
// @TODO: add {{asset}} subdir path // @TODO: add {{asset}} subdir path
return 'background-image: url(' + this.user.getWithDefault('image', '/shared/img/user-image.png') + ')'; return 'background-image: url(' + this.getWithDefault('user.image', '/shared/img/user-image.png') + ')';
}.property('user.image'), }.property('user.image'),
actions: { actions: {
save: function () { save: function () {
var self = this; var user = this.get('user'),
self = this;
self.notifications.closePassive(); self.notifications.closePassive();
alert('@TODO: Saving user...'); alert('@TODO: Saving user...');
if (this.user.validate().get('isValid')) { if (user.validate().get('isValid')) {
this.user.save().then(function (response) { user.save().then(function (response) {
alert('Done saving' + JSON.stringify(response)); alert('Done saving' + JSON.stringify(response));
}, function () { }, function () {
alert('Error saving.'); alert('Error saving.');
}); });
} else { } else {
alert('Errors found! ' + JSON.stringify(this.user.get('errors'))); alert('Errors found! ' + JSON.stringify(user.get('errors')));
} }
}, },
password: function () { password: function () {
alert('@TODO: Changing password...'); alert('@TODO: Changing password...');
var passwordProperties = this.getProperties('password', 'newPassword', 'ne2Password'); var user = this.get('user'),
passwordProperties = this.getProperties('password', 'newPassword', 'ne2Password');
if (this.user.validatePassword(passwordProperties).get('passwordIsValid')) { if (user.validatePassword(passwordProperties).get('passwordIsValid')) {
this.user.saveNewPassword(passwordProperties).then(function () { user.saveNewPassword(passwordProperties).then(function () {
alert('Success!'); alert('Success!');
// Clear properties from view // Clear properties from view
this.setProperties({ this.setProperties({
@ -56,7 +60,7 @@ var SettingsUserController = Ember.Controller.extend({
alert('Errors ' + JSON.stringify(errors)); alert('Errors ' + JSON.stringify(errors));
}); });
} else { } else {
alert('Errors found! ' + JSON.stringify(this.user.get('passwordErrors'))); alert('Errors found! ' + JSON.stringify(user.get('passwordErrors')));
} }
} }
} }

View File

@ -28,11 +28,8 @@ var SetupController = Ember.ObjectController.extend(ValidationEngine, {
identification: self.get('email'), identification: self.get('email'),
password: self.get('password') password: self.get('password')
}).then(function () { }).then(function () {
self.store.find('user', 'me').then(function (user) { self.send('signedIn');
self.send('signedIn', user); self.transitionToRoute(Ember.SimpleAuth.routeAfterAuthentication);
self.notifications.clear();
self.transitionToRoute(Ember.SimpleAuth.routeAfterAuthentication);
});
}); });
}, function (resp) { }, function (resp) {
self.toggleProperty('submitting'); self.toggleProperty('submitting');

View File

@ -27,11 +27,8 @@ var SignupController = Ember.ObjectController.extend(ValidationEngine, {
identification: self.get('email'), identification: self.get('email'),
password: self.get('password') password: self.get('password')
}).then(function () { }).then(function () {
self.store.find('user', 'me').then(function (user) { self.send('signedIn');
self.send('signedIn', user); self.transitionToRoute(Ember.SimpleAuth.routeAfterAuthentication);
self.notifications.clear();
self.transitionToRoute(Ember.SimpleAuth.routeAfterAuthentication);
});
}); });
}, function (resp) { }, function (resp) {
self.toggleProperty('submitting'); self.toggleProperty('submitting');

View File

@ -4,6 +4,11 @@ var AuthenticationInitializer = {
after: 'registerTrailingLocationHistory', after: 'registerTrailingLocationHistory',
initialize: function (container, application) { initialize: function (container, application) {
Ember.SimpleAuth.Session.reopen({
user: function () {
return container.lookup('store:main').find('user', 'me');
}.property()
});
Ember.SimpleAuth.Authenticators.OAuth2.reopen({ Ember.SimpleAuth.Authenticators.OAuth2.reopen({
serverTokenEndpoint: '/ghost/api/v0.1/authentication/token', serverTokenEndpoint: '/ghost/api/v0.1/authentication/token',
refreshAccessTokens: true, refreshAccessTokens: true,
@ -20,4 +25,4 @@ var AuthenticationInitializer = {
} }
}; };
export default AuthenticationInitializer; export default AuthenticationInitializer;

View File

@ -6,20 +6,6 @@ var ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin
shortcuts: { shortcuts: {
'esc': 'closePopups' 'esc': 'closePopups'
}, },
beforeModel: function () {
var self = this;
if (this.get('session').isAuthenticated) {
this.store.find('user', 'me').then(function (user) {
// Update the user on all routes and controllers
self.container.unregister('user:current');
self.container.register('user:current', user, { instantiate: false });
self.container.injection('route', 'user', 'user:current');
self.container.injection('controller', 'user', 'user:current');
});
}
},
mobileInteractions: function () { mobileInteractions: function () {
var responsiveAction = mobileUtils.responsiveAction; var responsiveAction = mobileUtils.responsiveAction;
@ -47,32 +33,10 @@ var ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin
this.send('closeModal'); this.send('closeModal');
}, },
signedIn: function (user) { signedIn: function () {
// Update the user on all routes and controllers
this.container.unregister('user:current');
this.container.register('user:current', user, { instantiate: false });
this.container.injection('route', 'user', 'user:current');
this.container.injection('controller', 'user', 'user:current');
this.set('user', user);
this.set('controller.user', user);
this.send('loadServerNotifications', true); this.send('loadServerNotifications', true);
}, },
signedOut: function () {
// Nullify the user on all routes and controllers
this.container.unregister('user:current');
this.container.register('user:current', null, { instantiate: false });
this.container.injection('route', 'user', 'user:current');
this.container.injection('controller', 'user', 'user:current');
this.set('user', null);
this.set('controller.user', null);
},
openModal: function (modalName, model, type) { openModal: function (modalName, model, type) {
modalName = 'modals/' + modalName; modalName = 'modals/' + modalName;
// We don't always require a modal to have a controller // We don't always require a modal to have a controller

View File

@ -0,0 +1,21 @@
var SettingsUserRoute = Ember.Route.extend({
model: function () {
return this.session.get('user').then(function (user) {
user.reload();
return user;
});
},
deactivate: function () {
this._super();
// we want to revert any unsaved changes on exit
this.session.get('user').then(function (user) {
if (user.get('isDirty')) {
user.rollback();
}
});
}
});
export default SettingsUserRoute;

View File

@ -10,12 +10,12 @@
<li id="usermenu" class="usermenu subnav"> <li id="usermenu" class="usermenu subnav">
{{#gh-popover-button popoverName="user-menu" tagName="a" href="#" classNames="dropdown"}} {{#gh-popover-button popoverName="user-menu" tagName="a" href="#" classNames="dropdown"}}
{{#if user.image}} {{#if session.user.image}}
<img class="avatar" {{bind-attr src="user.image"}} alt="Avatar" /> <img class="avatar" {{bind-attr src="session.user.image"}} alt="Avatar" />
{{else}} {{else}}
<img class="avatar" src="/shared/img/user-image.png" alt="Avatar" /> <img class="avatar" src="/shared/img/user-image.png" alt="Avatar" />
{{/if}} {{/if}}
<span class="name">{{user.name}}</span> <span class="name">{{session.user.name}}</span>
{{/gh-popover-button}} {{/gh-popover-button}}
{{#gh-popover tagName="ul" classNames="overlay" name="user-menu" closeOnClick="true"}} {{#gh-popover tagName="ul" classNames="overlay" name="user-menu" closeOnClick="true"}}
<li class="usermenu-profile">{{#link-to "settings.user"}}Your Profile{{/link-to}}</li> <li class="usermenu-profile">{{#link-to "settings.user"}}Your Profile{{/link-to}}</li>