From 4d074c3e55b18d0e00145115e82349fa281fdbdc Mon Sep 17 00:00:00 2001 From: Alan Richards Date: Fri, 25 Jul 2014 00:52:17 -0700 Subject: [PATCH] Settings screens redirect for certain roles Closes #3291 - Adds redirects based on roles as defined in the case - Adds new mixin `CurrentUserSettings` - For authors, all settings pages redirect to `users/self` - For editors, all settings pages other than specific users redirect to `users`. Any user that is not self or an author redirects to `users` --- ghost/admin/mixins/current-user-settings.js | 31 ++++++++++++++++++ ghost/admin/routes/settings/apps.js | 11 +++++-- ghost/admin/routes/settings/general.js | 9 +++++- ghost/admin/routes/settings/index.js | 36 +++++++++++++-------- ghost/admin/routes/settings/users.js | 9 +++++- ghost/admin/routes/settings/users/user.js | 14 ++++++++ 6 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 ghost/admin/mixins/current-user-settings.js diff --git a/ghost/admin/mixins/current-user-settings.js b/ghost/admin/mixins/current-user-settings.js new file mode 100644 index 0000000000..ad70b311d4 --- /dev/null +++ b/ghost/admin/mixins/current-user-settings.js @@ -0,0 +1,31 @@ +var CurrentUserSettings = Ember.Mixin.create({ + currentUser: function () { + return this.store.find('user', 'me'); + }, + + transitionAuthor: function () { + var self = this; + + return function (user) { + if (user.get('isAuthor')) { + return self.transitionTo('settings.users.user', user); + } + + return user; + }; + }, + + transitionEditor: function () { + var self = this; + + return function (user) { + if (user.get('isEditor')) { + return self.transitionTo('settings.users'); + } + + return user; + }; + } +}); + +export default CurrentUserSettings; \ No newline at end of file diff --git a/ghost/admin/routes/settings/apps.js b/ghost/admin/routes/settings/apps.js index 9672c1bc54..aec0590975 100644 --- a/ghost/admin/routes/settings/apps.js +++ b/ghost/admin/routes/settings/apps.js @@ -1,9 +1,16 @@ -var AppsRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, { +import CurrentUserSettings from 'ghost/mixins/current-user-settings'; + +var AppsRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, CurrentUserSettings, { beforeModel: function () { if (!this.get('config.apps')) { - this.transitionTo('settings.general'); + return this.transitionTo('settings.general'); } + + return this.currentUser() + .then(this.transitionAuthor()) + .then(this.transitionEditor()); }, + model: function () { return this.store.find('app'); } diff --git a/ghost/admin/routes/settings/general.js b/ghost/admin/routes/settings/general.js index 5843e28e2e..83c25d0c77 100644 --- a/ghost/admin/routes/settings/general.js +++ b/ghost/admin/routes/settings/general.js @@ -1,6 +1,13 @@ import loadingIndicator from 'ghost/mixins/loading-indicator'; +import CurrentUserSettings from 'ghost/mixins/current-user-settings'; + +var SettingsGeneralRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, loadingIndicator, CurrentUserSettings, { + beforeModel: function () { + return this.currentUser() + .then(this.transitionAuthor()) + .then(this.transitionEditor()); + }, -var SettingsGeneralRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, loadingIndicator, { model: function () { return this.store.find('setting', { type: 'blog,theme' }).then(function (records) { return records.get('firstObject'); diff --git a/ghost/admin/routes/settings/index.js b/ghost/admin/routes/settings/index.js index 09b42bfeb8..c0db77bc32 100644 --- a/ghost/admin/routes/settings/index.js +++ b/ghost/admin/routes/settings/index.js @@ -1,24 +1,32 @@ import {mobileQuery} from 'ghost/utils/mobile'; +import CurrentUserSettings from 'ghost/mixins/current-user-settings'; -var SettingsIndexRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, { +var SettingsIndexRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, CurrentUserSettings, { // redirect to general tab, unless on a mobile phone beforeModel: function () { - if (!mobileQuery.matches) { - this.transitionTo('settings.general'); - } else { - //fill the empty {{outlet}} in settings.hbs if the user - //goes to fullscreen + var self = this; + this.currentUser() + .then(this.transitionAuthor()) + .then(this.transitionEditor()) + .then(function () { + if (!mobileQuery.matches) { + self.transitionTo('settings.general'); + } else { + //fill the empty {{outlet}} in settings.hbs if the user + //goes to fullscreen - //fillOutlet needs special treatment so that it is - //properly bound to this when called from a MQ event - this.set('fillOutlet', _.bind(function fillOutlet(mq) { - if (!mq.matches) { - this.transitionTo('settings.general'); + //fillOutlet needs special treatment so that it is + //properly bound to this when called from a MQ event + self.set('fillOutlet', _.bind(function fillOutlet(mq) { + if (!mq.matches) { + self.transitionTo('settings.general'); + } + }, self)); + mobileQuery.addListener(self.fillOutlet); } - }, this)); - mobileQuery.addListener(this.fillOutlet); - } + }); }, + deactivate: function () { if (this.get('fillOutlet')) { mobileQuery.removeListener(this.fillOutlet); diff --git a/ghost/admin/routes/settings/users.js b/ghost/admin/routes/settings/users.js index e449f30085..3ef2dc2776 100644 --- a/ghost/admin/routes/settings/users.js +++ b/ghost/admin/routes/settings/users.js @@ -1,3 +1,10 @@ -var UsersRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin); +import CurrentUserSettings from 'ghost/mixins/current-user-settings'; + +var UsersRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, CurrentUserSettings, { + beforeModel: function () { + return this.currentUser() + .then(this.transitionAuthor()); + } +}); export default UsersRoute; diff --git a/ghost/admin/routes/settings/users/user.js b/ghost/admin/routes/settings/users/user.js index 67dc4b246b..395fbf597a 100644 --- a/ghost/admin/routes/settings/users/user.js +++ b/ghost/admin/routes/settings/users/user.js @@ -9,6 +9,20 @@ var SettingsUserRoute = Ember.Route.extend({ }); }, + afterModel: function (user) { + var self = this; + this.store.find('user', 'me').then(function (currentUser) { + var isOwnProfile = user.get('id') === currentUser.get('id'), + isAuthor = currentUser.get('isAuthor'), + isEditor = currentUser.get('isEditor'); + if (isAuthor && !isOwnProfile) { + self.transitionTo('settings.users.user', currentUser); + } else if (isEditor && !isOwnProfile && !user.get('isAuthor')) { + self.transitionTo('settings.users'); + } + }); + }, + deactivate: function () { var model = this.modelFor('settings.users.user');