2015-10-18 21:17:02 +03:00
|
|
|
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
|
2017-05-29 21:50:03 +03:00
|
|
|
import AuthConfiguration from 'ember-simple-auth/configuration';
|
|
|
|
import RSVP from 'rsvp';
|
2017-08-22 10:53:26 +03:00
|
|
|
import Route from '@ember/routing/route';
|
2016-05-24 15:06:59 +03:00
|
|
|
import ShortcutsRoute from 'ghost-admin/mixins/shortcuts-route';
|
|
|
|
import ctrlOrCmd from 'ghost-admin/utils/ctrl-or-cmd';
|
|
|
|
import windowProxy from 'ghost-admin/utils/window-proxy';
|
2017-11-04 01:59:39 +03:00
|
|
|
import {
|
|
|
|
isAjaxError,
|
|
|
|
isNotFoundError,
|
|
|
|
isUnauthorizedError
|
|
|
|
} from 'ember-ajax/errors';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isArray as isEmberArray} from '@ember/array';
|
2017-11-04 01:59:39 +03:00
|
|
|
import {
|
|
|
|
isMaintenanceError,
|
|
|
|
isVersionMismatchError
|
|
|
|
} from 'ghost-admin/services/ajax';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2014-06-19 23:44:44 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
function K() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
let shortcuts = {};
|
2014-11-26 01:34:55 +03:00
|
|
|
|
2017-10-31 12:10:49 +03:00
|
|
|
shortcuts.esc = {action: 'closeMenus', scope: 'default'};
|
2015-10-28 14:36:45 +03:00
|
|
|
shortcuts[`${ctrlOrCmd}+s`] = {action: 'save', scope: 'all'};
|
2014-11-26 01:34:55 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Route.extend(ApplicationRouteMixin, ShortcutsRoute, {
|
2018-04-16 19:55:21 +03:00
|
|
|
ajax: service(),
|
2017-10-30 12:38:01 +03:00
|
|
|
config: service(),
|
|
|
|
feature: service(),
|
2018-04-16 19:55:21 +03:00
|
|
|
ghostPaths: service(),
|
2017-10-30 12:38:01 +03:00
|
|
|
notifications: service(),
|
2018-04-30 15:07:37 +03:00
|
|
|
router: service(),
|
2017-10-30 12:38:01 +03:00
|
|
|
settings: service(),
|
|
|
|
tour: service(),
|
|
|
|
ui: service(),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
shortcuts,
|
|
|
|
|
2019-03-21 12:33:14 +03:00
|
|
|
routeAfterAuthentication: 'home',
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2019-03-06 20:15:44 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.router.on('routeDidChange', () => {
|
|
|
|
this.notifications.displayDelayed();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-10-28 16:07:50 +03:00
|
|
|
beforeModel() {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.config.fetchUnauthenticated();
|
2016-10-28 16:07:50 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
afterModel(model, transition) {
|
2016-05-05 17:03:09 +03:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2015-10-18 21:17:02 +03:00
|
|
|
if (this.get('session.isAuthenticated')) {
|
2016-05-13 14:46:49 +03:00
|
|
|
this.set('appLoadTransition', transition);
|
2014-08-06 09:01:00 +04:00
|
|
|
transition.send('loadServerNotifications');
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
let configPromise = this.config.fetchAuthenticated();
|
|
|
|
let featurePromise = this.feature.fetch();
|
|
|
|
let settingsPromise = this.settings.fetch();
|
|
|
|
let tourPromise = this.tour.fetchViewed();
|
2017-03-17 20:16:21 +03:00
|
|
|
|
|
|
|
// return the feature/settings load promises so that we block until
|
|
|
|
// they are loaded to enable synchronous access everywhere
|
|
|
|
return RSVP.all([
|
2019-02-26 06:38:00 +03:00
|
|
|
configPromise,
|
2017-03-17 20:16:21 +03:00
|
|
|
featurePromise,
|
2017-06-08 18:00:10 +03:00
|
|
|
settingsPromise,
|
|
|
|
tourPromise
|
2018-01-12 19:11:46 +03:00
|
|
|
]).then((results) => {
|
|
|
|
this._appLoaded = true;
|
|
|
|
return results;
|
|
|
|
});
|
2014-08-06 09:01:00 +04:00
|
|
|
}
|
2018-01-12 19:11:46 +03:00
|
|
|
|
|
|
|
this._appLoaded = true;
|
2014-08-06 09:01:00 +04:00
|
|
|
},
|
|
|
|
|
2014-03-31 08:07:05 +04:00
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
closeMenus() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.ui.closeMenus();
|
2014-06-19 23:44:44 +04:00
|
|
|
},
|
2014-06-24 06:52:38 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
didTransition() {
|
2016-05-13 14:46:49 +03:00
|
|
|
this.set('appLoadTransition', null);
|
2015-08-20 17:44:52 +03:00
|
|
|
this.send('closeMenus');
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
signedIn() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.clearAll();
|
2014-06-30 01:45:03 +04:00
|
|
|
this.send('loadServerNotifications', true);
|
2014-05-15 03:36:13 +04:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
authorizationFailed() {
|
2019-01-08 15:36:16 +03:00
|
|
|
windowProxy.replaceLocation(AuthConfiguration.rootURL);
|
2015-11-04 18:20:11 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
loadServerNotifications(isDelayed) {
|
2015-10-18 21:17:02 +03:00
|
|
|
if (this.get('session.isAuthenticated')) {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.get('session.user').then((user) => {
|
2018-02-07 12:42:46 +03:00
|
|
|
if (!user.get('isAuthorOrContributor') && !user.get('isEditor')) {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.store.findAll('notification', {reload: true}).then((serverNotifications) => {
|
|
|
|
serverNotifications.forEach((notification) => {
|
2018-01-09 17:23:36 +03:00
|
|
|
if (notification.get('top') || notification.get('custom')) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.handleNotification(notification, isDelayed);
|
2018-01-09 17:23:36 +03:00
|
|
|
} else {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.upgradeStatus.handleUpgradeNotification(notification);
|
2016-07-16 01:58:35 +03:00
|
|
|
}
|
2015-04-14 18:04:16 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-06-30 01:45:03 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-11-26 01:34:55 +03:00
|
|
|
// noop default for unhandled save (used from shortcuts)
|
2016-06-30 17:45:02 +03:00
|
|
|
save: K,
|
|
|
|
|
|
|
|
error(error, transition) {
|
2017-03-10 20:36:48 +03:00
|
|
|
// unauthoirized errors are already handled in the ajax service
|
|
|
|
if (isUnauthorizedError(error)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
if (isNotFoundError(error)) {
|
|
|
|
if (transition) {
|
|
|
|
transition.abort();
|
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2019-03-07 20:24:35 +03:00
|
|
|
let routeInfo = transition.to;
|
2019-03-06 16:53:54 +03:00
|
|
|
let router = this.router;
|
2017-11-04 01:59:39 +03:00
|
|
|
let params = [];
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
for (let key of Object.keys(routeInfo.params)) {
|
|
|
|
params.push(routeInfo.params[key]);
|
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2018-04-30 14:09:30 +03:00
|
|
|
let url = router.urlFor(routeInfo.name, ...params)
|
|
|
|
.replace(/^#\//, '')
|
|
|
|
.replace(/^\//, '')
|
|
|
|
.replace(/^ghost\//, '');
|
|
|
|
|
|
|
|
return this.replaceWith('error404', url);
|
2017-11-04 01:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isVersionMismatchError(error)) {
|
|
|
|
if (transition) {
|
|
|
|
transition.abort();
|
2016-11-14 16:16:51 +03:00
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this.upgradeStatus.requireUpgrade();
|
2018-01-12 19:11:46 +03:00
|
|
|
|
|
|
|
if (this._appLoaded) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-04 01:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isMaintenanceError(error)) {
|
|
|
|
if (transition) {
|
|
|
|
transition.abort();
|
2016-11-14 16:16:51 +03:00
|
|
|
}
|
2016-07-08 16:54:36 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this.upgradeStatus.maintenanceAlert();
|
2018-01-12 19:11:46 +03:00
|
|
|
|
|
|
|
if (this._appLoaded) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-04 01:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isAjaxError(error) || error && error.payload && isEmberArray(error.payload.errors)) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(error);
|
2017-11-04 01:59:39 +03:00
|
|
|
// don't show the 500 page if we weren't navigating
|
|
|
|
if (!transition) {
|
2016-11-14 16:16:51 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fallback to 500 error page
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
title(tokens) {
|
|
|
|
return `${tokens.join(' - ')} - ${this.get('config.blogTitle')}`;
|
|
|
|
},
|
|
|
|
|
|
|
|
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() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let transition = this.appLoadTransition;
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
if (transition) {
|
|
|
|
transition.send('authorizationFailed');
|
|
|
|
} else {
|
|
|
|
run.scheduleOnce('routerTransitions', this, function () {
|
|
|
|
this.send('authorizationFailed');
|
|
|
|
});
|
|
|
|
}
|
2014-03-31 08:07:05 +04:00
|
|
|
}
|
|
|
|
});
|