0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

Removed usage of deprecated EmberSimpleAuth mixins (#1910)

refs https://github.com/TryGhost/Admin/pull/1901

Ember has deprecated mixins in preparation for 4.0 and `ember-simple-auth` has now done the same in 3.1.0.

- removed all imports of Ember Simple Auth mixins
- moved authenticated and invalidated handling from application route to session service
- moved server-notification loading from application route to session service
- updated `AuthenticatedRoute` to use the session service directly rather than authenticated route mixin
- added `UnauthenticatedRoute` that incorporates the behaviour from our overridden `UnauthenticatedRouteMixin` and switches to using the session service directly
This commit is contained in:
Kevin Ansfield 2021-04-12 13:21:57 +01:00 committed by GitHub
parent eb28e42ad4
commit cf7a763199
10 changed files with 107 additions and 120 deletions

View file

@ -1,10 +1,9 @@
import AjaxServiceSupport from 'ember-ajax/mixins/ajax-support';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
import RESTAdapter from '@ember-data/adapter/rest';
import ghostPaths from 'ghost-admin/utils/ghost-paths';
import {inject as service} from '@ember/service';
export default RESTAdapter.extend(DataAdapterMixin, AjaxServiceSupport, {
export default RESTAdapter.extend(AjaxServiceSupport, {
host: window.location.origin,
namespace: ghostPaths().apiRoot.slice(1),

View file

@ -20,6 +20,7 @@ const {Errors} = DS;
export default Controller.extend({
two: controller('setup/two'),
notifications: service(),
session: service(),
users: '',
@ -123,7 +124,7 @@ export default Controller.extend({
},
skipInvite() {
this.send('loadServerNotifications');
this.session.loadServerNotifications();
this.transitionToRoute('home');
}
},
@ -176,7 +177,7 @@ export default Controller.extend({
this._showNotifications(invites);
run.schedule('actions', this, function () {
this.send('loadServerNotifications');
this.session.loadServerNotifications();
this._transitionAfterSubmission();
});
} else if (users.length === 0) {

View file

@ -1,35 +0,0 @@
import Mixin from '@ember/object/mixin';
import {inject as service} from '@ember/service';
export default Mixin.create({
ajax: service(),
ghostPaths: service(),
session: service(),
routeIfAlreadyAuthenticated: 'home',
beforeModel() {
let authUrl = this.get('ghostPaths.url').api('authentication', 'setup');
// check the state of the setup process via the API
return this.ajax.request(authUrl).then((result) => {
let [setup] = result.setup;
if (setup.status !== true) {
this.transitionTo('setup');
} else {
// NOTE: this is the same as ESA's UnauthenticatedRouteMixin,
// adding that mixin to this and calling _super wasn't calling
// the ESA mixin's beforeModel method
if (this.session.get('isAuthenticated')) {
let routeIfAlreadyAuthenticated = this.routeIfAlreadyAuthenticated;
return this.transitionTo(routeIfAlreadyAuthenticated);
} else {
return this._super(...arguments);
}
}
});
}
});

View file

@ -1,4 +1,3 @@
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
import AuthConfiguration from 'ember-simple-auth/configuration';
import RSVP from 'rsvp';
import Route from '@ember/routing/route';
@ -15,7 +14,6 @@ import {
isMaintenanceError,
isVersionMismatchError
} from 'ghost-admin/services/ajax';
import {run} from '@ember/runloop';
import {inject as service} from '@ember/service';
function K() {
@ -27,13 +25,14 @@ let shortcuts = {};
shortcuts.esc = {action: 'closeMenus', scope: 'default'};
shortcuts[`${ctrlOrCmd}+s`] = {action: 'save', scope: 'all'};
export default Route.extend(ApplicationRouteMixin, ShortcutsRoute, {
export default Route.extend(ShortcutsRoute, {
ajax: service(),
config: service(),
feature: service(),
ghostPaths: service(),
notifications: service(),
router: service(),
session: service(),
settings: service(),
ui: service(),
whatsNew: service(),
@ -57,8 +56,8 @@ export default Route.extend(ApplicationRouteMixin, ShortcutsRoute, {
this._super(...arguments);
if (this.get('session.isAuthenticated')) {
this.set('appLoadTransition', transition);
transition.send('loadServerNotifications');
this.session.appLoadTransition = transition;
this.session.loadServerNotifications();
// return the feature/settings load promises so that we block until
// they are loaded to enable synchronous access everywhere
@ -87,37 +86,14 @@ export default Route.extend(ApplicationRouteMixin, ShortcutsRoute, {
},
didTransition() {
this.set('appLoadTransition', null);
this.session.appLoadTransition = null;
this.send('closeMenus');
},
signedIn() {
this.notifications.clearAll();
this.send('loadServerNotifications', true);
},
authorizationFailed() {
windowProxy.replaceLocation(AuthConfiguration.rootURL);
},
loadServerNotifications(isDelayed) {
if (this.get('session.isAuthenticated')) {
this.get('session.user').then((user) => {
if (!user.get('isAuthorOrContributor')) {
this.store.findAll('notification', {reload: true}).then((serverNotifications) => {
serverNotifications.forEach((notification) => {
if (notification.get('top') || notification.get('custom')) {
this.notifications.handleNotification(notification, isDelayed);
} else {
this.upgradeStatus.handleUpgradeNotification(notification);
}
});
});
}
});
}
},
// noop default for unhandled save (used from shortcuts)
save: K,
@ -183,29 +159,5 @@ export default Route.extend(ApplicationRouteMixin, ShortcutsRoute, {
// fallback to 500 error page
return true;
}
},
sessionAuthenticated() {
if (this.get('session.skipAuthSuccessHandler')) {
return;
}
// standard ESA post-sign-in redirect
this._super(...arguments);
// trigger post-sign-in background behaviour
this.get('session.user').then((user) => {
this.send('signedIn', user);
});
},
sessionInvalidated() {
let transition = this.appLoadTransition;
if (transition) {
transition.send('authorizationFailed');
} else {
run.scheduleOnce('routerTransitions', this, 'send', 'authorizationFailed');
}
}
});

View file

@ -1,6 +1,10 @@
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
import Route from '@ember/routing/route';
import {inject as service} from '@ember/service';
export default Route.extend(AuthenticatedRouteMixin, {
authenticationRoute: 'signin'
export default Route.extend({
session: service(),
beforeModel(transition) {
this.session.requireAuthentication(transition, 'signin');
}
});

View file

@ -1,8 +1,7 @@
import Route from '@ember/routing/route';
import UnauthenticatedRouteMixin from 'ghost-admin/mixins/unauthenticated-route-mixin';
import UnauthenticatedRoute from 'ghost-admin/routes/unauthenticated';
import {inject as service} from '@ember/service';
export default Route.extend(UnauthenticatedRouteMixin, {
export default UnauthenticatedRoute.extend({
notifications: service(),
session: service(),
@ -22,11 +21,5 @@ export default Route.extend(UnauthenticatedRouteMixin, {
deactivate() {
this._super(...arguments);
this.controller.clearData();
},
buildRouteInfoMetadata() {
return {
bodyClasses: ['unauthenticated-route']
};
}
});

View file

@ -2,8 +2,7 @@
// eslint-disable-next-line
import DS from 'ember-data';
import EmberObject from '@ember/object';
import Route from '@ember/routing/route';
import UnauthenticatedRouteMixin from 'ghost-admin/mixins/unauthenticated-route-mixin';
import UnauthenticatedRoute from 'ghost-admin/routes/unauthenticated';
const {Errors} = DS;
@ -15,7 +14,7 @@ const defaultModel = function defaultModel() {
});
};
export default Route.extend(UnauthenticatedRouteMixin, {
export default UnauthenticatedRoute.extend({
model() {
return defaultModel();
},
@ -31,9 +30,8 @@ export default Route.extend(UnauthenticatedRouteMixin, {
},
buildRouteInfoMetadata() {
return {
titleToken: 'Sign In',
bodyClasses: ['unauthenticated-route']
};
return Object.assign(this._super(), {
titleToken: 'Sign In'
});
}
});

View file

@ -3,15 +3,14 @@
import DS from 'ember-data';
import EmberObject from '@ember/object';
import RSVP from 'rsvp';
import Route from '@ember/routing/route';
import UnauthenticatedRouteMixin from 'ghost-admin/mixins/unauthenticated-route-mixin';
import UnauthenticatedRoute from 'ghost-admin/routes/unauthenticated';
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
import {inject as service} from '@ember/service';
const {Promise} = RSVP;
const {Errors} = DS;
export default Route.extend(UnauthenticatedRouteMixin, {
export default UnauthenticatedRoute.extend({
ghostPaths: service(),
notifications: service(),
session: service(),
@ -81,11 +80,5 @@ export default Route.extend(UnauthenticatedRouteMixin, {
// clear the properties that hold the sensitive data from the controller
this.controllerFor('signup').get('signupDetails').setProperties({email: '', password: '', token: ''});
},
buildRouteInfoMetadata() {
return {
bodyClasses: ['unauthenticated-route']
};
}
});

View file

@ -0,0 +1,29 @@
import Route from '@ember/routing/route';
import {inject as service} from '@ember/service';
export default class UnauthenticatedRoute extends Route {
@service ajax;
@service ghostPaths;
@service session;
beforeModel() {
let authUrl = this.ghostPaths.url.api('authentication', 'setup');
// check the state of the setup process via the API
return this.ajax.request(authUrl).then((result) => {
let [setup] = result.setup;
if (setup.status !== true) {
this.transitionTo('setup');
} else {
return this.session.prohibitAuthentication('home');
}
});
}
buildRouteInfoMetadata() {
return {
bodyClasses: ['unauthenticated-route']
};
}
}

View file

@ -1,9 +1,14 @@
import SessionService from 'ember-simple-auth/services/session';
import {computed} from '@ember/object';
import {getOwner} from '@ember/application';
import {run} from '@ember/runloop';
import {inject as service} from '@ember/service';
export default SessionService.extend({
dataStore: service('store'), // SessionService.store already exists
notifications: service(),
router: service(),
upgradeStatus: service(),
user: computed(function () {
return this.dataStore.queryRecord('user', {id: 'me'});
@ -14,5 +19,53 @@ export default SessionService.extend({
this.notifyPropertyChange('user');
return this._super(...arguments);
},
handleAuthentication() {
if (this.skipAuthSuccessHandler) {
return;
}
// standard ESA post-sign-in redirect
this._super('home');
// trigger post-sign-in background behaviour
this.user.then(() => {
this.notifications.clearAll();
this.loadServerNotifications();
});
},
handleInvalidation() {
let transition = this.appLoadTransition;
if (transition) {
transition.send('authorizationFailed');
} else {
run.scheduleOnce('routerTransitions', this, 'triggerAuthorizationFailed');
}
},
// TODO: this feels hacky, find a better way than using .send
triggerAuthorizationFailed() {
getOwner(this).lookup(`route:${this.router.currentRouteName}`).send('authorizationFailed');
},
loadServerNotifications() {
if (this.isAuthenticated) {
this.user.then((user) => {
if (!user.isAuthorOrContributor) {
this.dataStore.findAll('notification', {reload: true}).then((serverNotifications) => {
serverNotifications.forEach((notification) => {
if (notification.top || notification.custom) {
this.notifications.handleNotification(notification);
} else {
this.upgradeStatus.handleUpgradeNotification(notification);
}
});
});
}
});
}
}
});