From b4b5e2a3f5015af62ebf2c5d47f384f86eb39aae Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 14 Apr 2015 16:04:16 +0100 Subject: [PATCH] Avoid infinite transition loop fixes #5136 - wrap notification fetch with a user role check to remove console error - move author transition down to local route for users/user so that there's no infinite loop - replace all store calls to fetch the current user with the session user instead --- core/client/app/mixins/current-user-settings.js | 4 ---- core/client/app/routes/application.js | 14 +++++++++----- core/client/app/routes/editor/edit.js | 2 +- core/client/app/routes/posts.js | 2 +- core/client/app/routes/posts/index.js | 2 +- core/client/app/routes/posts/post.js | 2 +- core/client/app/routes/settings/apps.js | 2 +- core/client/app/routes/settings/code-injection.js | 2 +- core/client/app/routes/settings/general.js | 2 +- core/client/app/routes/settings/index.js | 2 +- core/client/app/routes/settings/labs.js | 2 +- core/client/app/routes/settings/navigation.js | 3 ++- core/client/app/routes/settings/tags.js | 2 +- core/client/app/routes/settings/users.js | 8 +------- core/client/app/routes/settings/users/index.js | 10 ++++++++-- core/client/app/routes/settings/users/user.js | 5 +++-- 16 files changed, 33 insertions(+), 31 deletions(-) diff --git a/core/client/app/mixins/current-user-settings.js b/core/client/app/mixins/current-user-settings.js index d4ab113774..bdd3f5fb86 100644 --- a/core/client/app/mixins/current-user-settings.js +++ b/core/client/app/mixins/current-user-settings.js @@ -1,9 +1,5 @@ import Ember from 'ember'; var CurrentUserSettings = Ember.Mixin.create({ - currentUser: function () { - return this.store.find('user', 'me'); - }, - transitionAuthor: function () { var self = this; diff --git a/core/client/app/routes/application.js b/core/client/app/routes/application.js index 7e1c14b390..0d55bb2fcd 100644 --- a/core/client/app/routes/application.js +++ b/core/client/app/routes/application.js @@ -76,7 +76,7 @@ ApplicationRoute = Ember.Route.extend(SimpleAuth.ApplicationRouteMixin, Shortcut return; } - this.store.find('user', 'me').then(function (user) { + this.get('session.user').then(function (user) { self.send('signedIn', user); var attemptedTransition = self.get('session').get('attemptedTransition'); if (attemptedTransition) { @@ -138,10 +138,14 @@ ApplicationRoute = Ember.Route.extend(SimpleAuth.ApplicationRouteMixin, Shortcut var self = this; if (this.session.isAuthenticated) { - this.store.findAll('notification').then(function (serverNotifications) { - serverNotifications.forEach(function (notification) { - self.notifications.handleNotification(notification, isDelayed); - }); + this.get('session.user').then(function (user) { + if (!user.get('isAuthor') && !user.get('isEditor')) { + self.store.findAll('notification').then(function (serverNotifications) { + serverNotifications.forEach(function (notification) { + self.notifications.handleNotification(notification, isDelayed); + }); + }); + } }); } }, diff --git a/core/client/app/routes/editor/edit.js b/core/client/app/routes/editor/edit.js index d067bd37e2..fd580f3871 100644 --- a/core/client/app/routes/editor/edit.js +++ b/core/client/app/routes/editor/edit.js @@ -43,7 +43,7 @@ var EditorEditRoute = AuthenticatedRoute.extend(base, { afterModel: function (post) { var self = this; - return self.store.find('user', 'me').then(function (user) { + return self.get('session.user').then(function (user) { if (user.get('isAuthor') && !post.isAuthoredByUser(user)) { return self.replaceWith('posts.index'); } diff --git a/core/client/app/routes/posts.js b/core/client/app/routes/posts.js index faed2302af..10788978f1 100644 --- a/core/client/app/routes/posts.js +++ b/core/client/app/routes/posts.js @@ -22,7 +22,7 @@ PostsRoute = AuthenticatedRoute.extend(ShortcutsRoute, styleBody, loadingIndicat model: function () { var self = this; - return this.store.find('user', 'me').then(function (user) { + return this.get('session.user').then(function (user) { if (user.get('isAuthor')) { paginationSettings.author = user.get('slug'); } diff --git a/core/client/app/routes/posts/index.js b/core/client/app/routes/posts/index.js index 6d3f934377..7dc040466e 100644 --- a/core/client/app/routes/posts/index.js +++ b/core/client/app/routes/posts/index.js @@ -23,7 +23,7 @@ var PostsIndexRoute = MobileIndexRoute.extend(SimpleAuth.AuthenticatedRouteMixin posts = this.store.all('post'), post; - return this.store.find('user', 'me').then(function (user) { + return this.get('session.user').then(function (user) { post = posts.find(function (post) { // Authors can only see posts they've written if (user.get('isAuthor')) { diff --git a/core/client/app/routes/posts/post.js b/core/client/app/routes/posts/post.js index f91dad4ef8..9e70f6f500 100644 --- a/core/client/app/routes/posts/post.js +++ b/core/client/app/routes/posts/post.js @@ -42,7 +42,7 @@ var PostsPostRoute = AuthenticatedRoute.extend(loadingIndicator, ShortcutsRoute, afterModel: function (post) { var self = this; - return self.store.find('user', 'me').then(function (user) { + return self.get('session.user').then(function (user) { if (user.get('isAuthor') && !post.isAuthoredByUser(user)) { return self.replaceWith('posts.index'); } diff --git a/core/client/app/routes/settings/apps.js b/core/client/app/routes/settings/apps.js index b99e0fe9b1..143a64b1e0 100644 --- a/core/client/app/routes/settings/apps.js +++ b/core/client/app/routes/settings/apps.js @@ -12,7 +12,7 @@ var AppsRoute = AuthenticatedRoute.extend(styleBody, CurrentUserSettings, { return this.transitionTo('settings.general'); } - return this.currentUser() + return this.get('session.user') .then(this.transitionAuthor()) .then(this.transitionEditor()); }, diff --git a/core/client/app/routes/settings/code-injection.js b/core/client/app/routes/settings/code-injection.js index 2d1f4f3c1d..332d34ea38 100644 --- a/core/client/app/routes/settings/code-injection.js +++ b/core/client/app/routes/settings/code-injection.js @@ -7,7 +7,7 @@ var SettingsCodeInjectionRoute = AuthenticatedRoute.extend(styleBody, loadingInd classNames: ['settings-view-code'], beforeModel: function () { - return this.currentUser() + return this.get('session.user') .then(this.transitionAuthor()) .then(this.transitionEditor()); }, diff --git a/core/client/app/routes/settings/general.js b/core/client/app/routes/settings/general.js index 4d794a277f..db0644d2b1 100644 --- a/core/client/app/routes/settings/general.js +++ b/core/client/app/routes/settings/general.js @@ -9,7 +9,7 @@ var SettingsGeneralRoute = AuthenticatedRoute.extend(styleBody, loadingIndicator classNames: ['settings-view-general'], beforeModel: function () { - return this.currentUser() + return this.get('session.user') .then(this.transitionAuthor()) .then(this.transitionEditor()); }, diff --git a/core/client/app/routes/settings/index.js b/core/client/app/routes/settings/index.js index 03111bfce4..fcd49f3f58 100644 --- a/core/client/app/routes/settings/index.js +++ b/core/client/app/routes/settings/index.js @@ -10,7 +10,7 @@ var SettingsIndexRoute = MobileIndexRoute.extend(SimpleAuth.AuthenticatedRouteMi // is mobile beforeModel: function () { var self = this; - return this.currentUser() + return this.get('session.user') .then(this.transitionAuthor()) .then(this.transitionEditor()) .then(function () { diff --git a/core/client/app/routes/settings/labs.js b/core/client/app/routes/settings/labs.js index 727fb4bb30..c926103719 100644 --- a/core/client/app/routes/settings/labs.js +++ b/core/client/app/routes/settings/labs.js @@ -8,7 +8,7 @@ var LabsRoute = AuthenticatedRoute.extend(styleBody, loadingIndicator, CurrentUs classNames: ['settings'], beforeModel: function () { - return this.currentUser() + return this.get('session.user') .then(this.transitionAuthor()) .then(this.transitionEditor()); }, diff --git a/core/client/app/routes/settings/navigation.js b/core/client/app/routes/settings/navigation.js index d2690ecb4c..310f31f7ae 100644 --- a/core/client/app/routes/settings/navigation.js +++ b/core/client/app/routes/settings/navigation.js @@ -9,7 +9,8 @@ var NavigationRoute = AuthenticatedRoute.extend(styleBody, CurrentUserSettings, classNames: ['settings-view-navigation'], beforeModel: function () { - return this.currentUser().then(this.transitionAuthor()); + return this.get('session.user') + .then(this.transitionAuthor()); }, model: function () { diff --git a/core/client/app/routes/settings/tags.js b/core/client/app/routes/settings/tags.js index 862d043782..ce51180903 100644 --- a/core/client/app/routes/settings/tags.js +++ b/core/client/app/routes/settings/tags.js @@ -21,7 +21,7 @@ TagsRoute = AuthenticatedRoute.extend(CurrentUserSettings, PaginationRouteMixin, titleToken: 'Tags', beforeModel: function () { - return this.currentUser() + return this.get('session.user') .then(this.transitionAuthor()); }, diff --git a/core/client/app/routes/settings/users.js b/core/client/app/routes/settings/users.js index 02512394ce..d9a932e04c 100644 --- a/core/client/app/routes/settings/users.js +++ b/core/client/app/routes/settings/users.js @@ -1,11 +1,5 @@ import AuthenticatedRoute from 'ghost/routes/authenticated'; -import CurrentUserSettings from 'ghost/mixins/current-user-settings'; -var UsersRoute = AuthenticatedRoute.extend(CurrentUserSettings, { - beforeModel: function () { - return this.currentUser() - .then(this.transitionAuthor()); - } -}); +var UsersRoute = AuthenticatedRoute.extend(); export default UsersRoute; diff --git a/core/client/app/routes/settings/users/index.js b/core/client/app/routes/settings/users/index.js index 800993aaeb..d6142906e6 100644 --- a/core/client/app/routes/settings/users/index.js +++ b/core/client/app/routes/settings/users/index.js @@ -1,4 +1,5 @@ import AuthenticatedRoute from 'ghost/routes/authenticated'; +import CurrentUserSettings from 'ghost/mixins/current-user-settings'; import PaginationRouteMixin from 'ghost/mixins/pagination-route'; import styleBody from 'ghost/mixins/style-body'; @@ -11,7 +12,7 @@ paginationSettings = { status: 'active' }; -UsersIndexRoute = AuthenticatedRoute.extend(styleBody, PaginationRouteMixin, { +UsersIndexRoute = AuthenticatedRoute.extend(styleBody, CurrentUserSettings, PaginationRouteMixin, { titleToken: 'Users', classNames: ['settings-view-users'], @@ -21,11 +22,16 @@ UsersIndexRoute = AuthenticatedRoute.extend(styleBody, PaginationRouteMixin, { this.setupPagination(paginationSettings); }, + beforeModel: function () { + return this.get('session.user') + .then(this.transitionAuthor()); + }, + model: function () { var self = this; return self.store.find('user', {limit: 'all', status: 'invited'}).then(function () { - return self.store.find('user', 'me').then(function (currentUser) { + return self.get('session.user').then(function (currentUser) { if (currentUser.get('isEditor')) { // Editors only see authors in the list paginationSettings.role = 'Author'; diff --git a/core/client/app/routes/settings/users/user.js b/core/client/app/routes/settings/users/user.js index 418352f095..00a0538a8d 100644 --- a/core/client/app/routes/settings/users/user.js +++ b/core/client/app/routes/settings/users/user.js @@ -1,7 +1,8 @@ import AuthenticatedRoute from 'ghost/routes/authenticated'; +import CurrentUserSettings from 'ghost/mixins/current-user-settings'; import styleBody from 'ghost/mixins/style-body'; -var SettingsUserRoute = AuthenticatedRoute.extend(styleBody, { +var SettingsUserRoute = AuthenticatedRoute.extend(styleBody, CurrentUserSettings, { titleToken: 'User', classNames: ['settings-view-user'], @@ -25,7 +26,7 @@ var SettingsUserRoute = AuthenticatedRoute.extend(styleBody, { afterModel: function (user) { var self = this; - this.store.find('user', 'me').then(function (currentUser) { + return this.get('session.user').then(function (currentUser) { var isOwnProfile = user.get('id') === currentUser.get('id'), isAuthor = currentUser.get('isAuthor'), isEditor = currentUser.get('isEditor');