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';
|
2017-07-10 13:18:19 +03:00
|
|
|
import moment from 'moment';
|
2016-05-24 15:06:59 +03:00
|
|
|
import windowProxy from 'ghost-admin/utils/window-proxy';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {htmlSafe} from '@ember/string';
|
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(),
|
|
|
|
settings: service(),
|
|
|
|
tour: service(),
|
|
|
|
ui: service(),
|
2018-04-30 14:09:30 +03:00
|
|
|
// TODO: rename to `router` when we drop using of Route#router in our
|
|
|
|
// document-title util
|
|
|
|
routerService: service('router'),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
shortcuts,
|
|
|
|
|
|
|
|
routeAfterAuthentication: 'posts',
|
|
|
|
|
2016-10-28 16:07:50 +03:00
|
|
|
beforeModel() {
|
|
|
|
return this.get('config').fetch();
|
|
|
|
},
|
|
|
|
|
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-06-13 15:40:41 +03:00
|
|
|
transition.send('checkForOutdatedDesktopApp');
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2017-07-10 13:18:19 +03:00
|
|
|
// trigger a background token refresh to enable "infinite" sessions
|
|
|
|
// NOTE: we only do this if the last refresh was > 1 day ago to avoid
|
|
|
|
// potential issues with multiple tabs and concurrent admin loads/refreshes.
|
|
|
|
// see https://github.com/TryGhost/Ghost/issues/8616
|
2017-02-10 16:35:45 +03:00
|
|
|
let session = this.get('session.session');
|
2017-07-10 13:18:19 +03:00
|
|
|
let expiresIn = session.get('authenticated.expires_in') * 1000;
|
|
|
|
let expiresAt = session.get('authenticated.expires_at');
|
|
|
|
let lastRefresh = moment(expiresAt - expiresIn);
|
|
|
|
let oneDayAgo = moment().subtract(1, 'day');
|
|
|
|
|
|
|
|
if (lastRefresh.isBefore(oneDayAgo)) {
|
|
|
|
let authenticator = session._lookupAuthenticator(session.authenticator);
|
|
|
|
if (authenticator && authenticator.onOnline) {
|
|
|
|
authenticator.onOnline();
|
|
|
|
}
|
2017-02-10 16:35:45 +03:00
|
|
|
}
|
|
|
|
|
2018-01-11 17:16:42 +03:00
|
|
|
let featurePromise = this.get('feature').fetch();
|
2017-03-17 20:16:21 +03:00
|
|
|
let settingsPromise = this.get('settings').fetch();
|
2017-08-02 10:05:59 +03:00
|
|
|
let privateConfigPromise = this.get('config').fetchPrivate();
|
2017-06-08 18:00:10 +03:00
|
|
|
let tourPromise = this.get('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([
|
|
|
|
featurePromise,
|
2017-06-08 18:00:10 +03:00
|
|
|
settingsPromise,
|
2017-08-02 10:05:59 +03:00
|
|
|
privateConfigPromise,
|
2017-06-08 18:00:10 +03:00
|
|
|
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() {
|
2017-08-14 15:30:00 +03:00
|
|
|
this.get('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() {
|
2015-10-07 17:44:23 +03:00
|
|
|
this.get('notifications').clearAll();
|
2014-06-30 01:45:03 +04:00
|
|
|
this.send('loadServerNotifications', true);
|
2014-05-15 03:36:13 +04:00
|
|
|
},
|
|
|
|
|
2018-04-16 19:55:21 +03:00
|
|
|
// this is only called by the `signout` route at present.
|
|
|
|
// it's separate to the normal ESA session invalidadition because it will
|
|
|
|
// actually send the token revocation requests whereas we have to avoid
|
|
|
|
// those most of the time because they will fail if we have invalid tokens
|
|
|
|
logout() {
|
|
|
|
let session = this.get('session');
|
|
|
|
// revoke keys on the server
|
|
|
|
if (session.get('isAuthenticated')) {
|
|
|
|
let auth = session.get('data.authenticated');
|
|
|
|
let revokeEndpoint = `${this.get('ghostPaths.apiRoot')}/authentication/revoke`;
|
|
|
|
let authenticator = session.get('session')._lookupAuthenticator(session.get('session.authenticator'));
|
|
|
|
let requests = [];
|
|
|
|
['refresh_token', 'access_token'].forEach((tokenType) => {
|
|
|
|
let data = {
|
|
|
|
token_type_hint: tokenType,
|
|
|
|
token: auth[tokenType]
|
|
|
|
};
|
|
|
|
authenticator.makeRequest(revokeEndpoint, data);
|
|
|
|
});
|
|
|
|
RSVP.all(requests).finally(() => {
|
|
|
|
// remove local keys and refresh
|
|
|
|
session.invalidate();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// remove local keys and refresh
|
|
|
|
session.invalidate();
|
|
|
|
}
|
2014-07-30 00:49:26 +04:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
authorizationFailed() {
|
2015-11-04 18:20:11 +03:00
|
|
|
windowProxy.replaceLocation(AuthConfiguration.baseURL);
|
|
|
|
},
|
|
|
|
|
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')) {
|
2016-07-16 01:58:35 +03:00
|
|
|
this.get('notifications').handleNotification(notification, isDelayed);
|
2018-01-09 17:23:36 +03:00
|
|
|
} else {
|
|
|
|
this.get('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
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-09 01:10:56 +03:00
|
|
|
checkForOutdatedDesktopApp() {
|
|
|
|
// Check if the user is running an older version of Ghost Desktop
|
|
|
|
// that needs to be manually updated
|
|
|
|
// (yes, the desktop team is deeply ashamed of these lines 😢)
|
|
|
|
let ua = navigator && navigator.userAgent ? navigator.userAgent : null;
|
|
|
|
|
|
|
|
if (ua && ua.includes && ua.includes('ghost-desktop')) {
|
|
|
|
let updateCheck = /ghost-desktop\/0\.((5\.0)|((4|2)\.0)|((3\.)(0|1)))/;
|
|
|
|
let link = '<a href="https://dev.ghost.org/ghost-desktop-manual-update" target="_blank">click here</a>';
|
|
|
|
let msg = `Your version of Ghost Desktop needs to be manually updated. Please ${link} to get started.`;
|
|
|
|
|
|
|
|
if (updateCheck.test(ua)) {
|
2016-06-13 15:40:41 +03:00
|
|
|
this.get('notifications').showAlert(htmlSafe(msg), {
|
|
|
|
type: 'warn',
|
2016-06-09 01:10:56 +03:00
|
|
|
key: 'desktop.manual.upgrade'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
let routeInfo = transition.handlerInfos[transition.handlerInfos.length - 1];
|
2018-04-30 14:09:30 +03:00
|
|
|
let router = this.get('routerService');
|
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
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
this.get('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
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
this.get('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)) {
|
|
|
|
this.get('notifications').showAPIError(error);
|
|
|
|
// 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() {
|
|
|
|
let transition = this.get('appLoadTransition');
|
|
|
|
|
|
|
|
if (transition) {
|
|
|
|
transition.send('authorizationFailed');
|
|
|
|
} else {
|
|
|
|
run.scheduleOnce('routerTransitions', this, function () {
|
|
|
|
this.send('authorizationFailed');
|
|
|
|
});
|
|
|
|
}
|
2014-03-31 08:07:05 +04:00
|
|
|
}
|
|
|
|
});
|