Ghost/ghost/admin/app/routes/settings/integration.js

67 lines
2.3 KiB
JavaScript
Raw Normal View History

Refactored away CurrentUserSettings mixin (#2200) no issue Mixins are deprecated in Ember so we want to remove their usage. The `CurrentUserSettings` mixin was used in Route files to provide `transitionAuthor()` (that also transitions contributors) and `transitionEditor()` methods so the the consuming route could use them to prevent access to authors/editors. In practice the only reason this was used was to prevent access to admin-only routes. - added an `AdminRoute` class that inherits from our `AuthenticatedRoute` class - when any route inherits from this class it will only allow access to admins and owners, any other user will be redirected to the home screen (dashboard or site depending on permissions) - updated all of our admin-only routes to use the new `AdminRoute` - allowed for removal of `CurrentUserSettings` mixin usage - allowed for `beforeModel()` hooks to be removed from consuming routes in many cases - some admin-only routes were extending/inheriting directly from Ember's `Route` based on the assumption that the router hierarchy would have a parent route perform the redirect. Those have also been switched to `AdminRoute` for consistency and to prevent accidentally making them available if the router hierarchy changes - `/#/settings` does not use the `AdminRoute` so that it can redirect to the current user's setting page for non-admin users - removed `CurrentUserSettings` mixin file - cleaned up unnecessary computed property and function used for redirect-when-disabled in the Zapier route
2022-01-17 13:05:27 +03:00
import AdminRoute from 'ghost-admin/routes/admin';
import {inject as service} from '@ember/service';
Refactored away CurrentUserSettings mixin (#2200) no issue Mixins are deprecated in Ember so we want to remove their usage. The `CurrentUserSettings` mixin was used in Route files to provide `transitionAuthor()` (that also transitions contributors) and `transitionEditor()` methods so the the consuming route could use them to prevent access to authors/editors. In practice the only reason this was used was to prevent access to admin-only routes. - added an `AdminRoute` class that inherits from our `AuthenticatedRoute` class - when any route inherits from this class it will only allow access to admins and owners, any other user will be redirected to the home screen (dashboard or site depending on permissions) - updated all of our admin-only routes to use the new `AdminRoute` - allowed for removal of `CurrentUserSettings` mixin usage - allowed for `beforeModel()` hooks to be removed from consuming routes in many cases - some admin-only routes were extending/inheriting directly from Ember's `Route` based on the assumption that the router hierarchy would have a parent route perform the redirect. Those have also been switched to `AdminRoute` for consistency and to prevent accidentally making them available if the router hierarchy changes - `/#/settings` does not use the `AdminRoute` so that it can redirect to the current user's setting page for non-admin users - removed `CurrentUserSettings` mixin file - cleaned up unnecessary computed property and function used for redirect-when-disabled in the Zapier route
2022-01-17 13:05:27 +03:00
export default AdminRoute.extend({
router: service(),
init() {
this._super(...arguments);
this.router.on('routeWillChange', (transition) => {
this.showUnsavedChangesModal(transition);
if (this.controller) {
this.controller.set('selectedApiKey', null);
this.controller.set('isApiKeyRegenerated', false);
}
});
},
model(params, transition) {
// use the integrations controller to fetch all integrations and pick
// out the one we want. Allows navigation back to integrations screen
// without a loading state
return this
.controllerFor('settings.integrations')
.integrationModelHook('id', params.integration_id, this, transition);
},
deactivate() {
this._super(...arguments);
this.controller.set('leaveScreenTransition', null);
this.controller.set('showUnsavedChangesModal', false);
},
actions: {
save() {
this.controller.send('save');
}
},
showUnsavedChangesModal(transition) {
if (transition.from && transition.from.name.match(/^settings\.integration\./) && transition.targetName) {
let {controller} = this;
// check to see if we're navigating away from the custom integration
// route - we want to allow editing webhooks without showing the
// "unsaved changes" confirmation modal
let isExternalRoute =
// allow sub-routes of integration
!(transition.targetName || '').match(/^integration\./)
// do not allow changes in integration
// .to will be the index, so use .to.parent to get the route with the params
|| transition.to.parent.params.integration_id !== controller.integration.id;
if (isExternalRoute && !controller.integration.isDeleted && controller.integration.hasDirtyAttributes) {
transition.abort();
controller.send('toggleUnsavedChangesModal', transition);
return;
}
}
},
buildRouteInfoMetadata() {
return {
titleToken: 'Settings - Integrations'
};
}
});