2017-03-03 19:14:33 +03:00
|
|
|
import $ from 'jquery';
|
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';
|
|
|
|
import {inject as injectService} from '@ember/service';
|
|
|
|
import {isArray as isEmberArray} from '@ember/array';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {isUnauthorizedError} from 'ember-ajax/errors';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {observer} from '@ember/object';
|
|
|
|
import {run} from '@ember/runloop';
|
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
|
|
|
|
2015-05-27 17:39:56 +03:00
|
|
|
shortcuts.esc = {action: 'closeMenus', scope: 'all'};
|
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, {
|
|
|
|
shortcuts,
|
2014-06-30 16:58:10 +04:00
|
|
|
|
2017-02-06 18:53:04 +03:00
|
|
|
routeAfterAuthentication: 'posts',
|
|
|
|
|
2016-06-30 13:21:47 +03:00
|
|
|
config: injectService(),
|
|
|
|
feature: injectService(),
|
2017-03-03 19:14:33 +03:00
|
|
|
lazyLoader: injectService(),
|
2016-06-30 13:21:47 +03:00
|
|
|
notifications: injectService(),
|
2017-03-17 20:16:21 +03:00
|
|
|
settings: injectService(),
|
2016-07-16 01:58:35 +03:00
|
|
|
upgradeNotification: injectService(),
|
2017-06-08 18:00:10 +03:00
|
|
|
tour: injectService(),
|
2017-08-14 15:30:00 +03:00
|
|
|
ui: injectService(),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-17 20:16:21 +03:00
|
|
|
let featurePromise = this.get('feature').fetch().then(() => {
|
2017-03-03 19:14:33 +03:00
|
|
|
if (this.get('feature.nightShift')) {
|
|
|
|
return this._setAdminTheme();
|
|
|
|
}
|
|
|
|
});
|
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
|
2017-03-17 20:16:21 +03:00
|
|
|
]);
|
2014-08-06 09:01:00 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
title(tokens) {
|
|
|
|
return `${tokens.join(' - ')} - ${this.get('config.blogTitle')}`;
|
2014-11-25 23:56:08 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
sessionAuthenticated() {
|
2015-11-18 13:50:48 +03:00
|
|
|
if (this.get('session.skipAuthSuccessHandler')) {
|
2015-10-18 21:17:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-05 17:03:09 +03:00
|
|
|
// standard ESA post-sign-in redirect
|
2015-10-18 21:17:02 +03:00
|
|
|
this._super(...arguments);
|
2016-05-05 17:03:09 +03:00
|
|
|
|
|
|
|
// trigger post-sign-in background behaviour
|
2015-10-28 14:36:45 +03:00
|
|
|
this.get('session.user').then((user) => {
|
|
|
|
this.send('signedIn', user);
|
2015-10-18 21:17:02 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
sessionInvalidated() {
|
2016-05-13 14:46:49 +03:00
|
|
|
let transition = this.get('appLoadTransition');
|
|
|
|
|
|
|
|
if (transition) {
|
|
|
|
transition.send('authorizationFailed');
|
|
|
|
} else {
|
|
|
|
run.scheduleOnce('routerTransitions', this, function () {
|
|
|
|
this.send('authorizationFailed');
|
|
|
|
});
|
|
|
|
}
|
2015-11-04 18:20:11 +03:00
|
|
|
},
|
|
|
|
|
2017-03-03 19:14:33 +03:00
|
|
|
_nightShift: observer('feature.nightShift', function () {
|
|
|
|
this._setAdminTheme();
|
|
|
|
}),
|
|
|
|
|
|
|
|
_setAdminTheme() {
|
|
|
|
let nightShift = this.get('feature.nightShift');
|
|
|
|
|
|
|
|
return this.get('lazyLoader').loadStyle('dark', 'assets/ghost-dark.css', true).then(() => {
|
|
|
|
$('link[title=dark]').prop('disabled', !nightShift);
|
|
|
|
$('link[title=light]').prop('disabled', nightShift);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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);
|
2017-03-03 19:14:33 +03:00
|
|
|
|
|
|
|
if (this.get('feature.nightShift')) {
|
|
|
|
this._setAdminTheme();
|
|
|
|
}
|
2014-05-15 03:36:13 +04:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
invalidateSession() {
|
|
|
|
this.get('session').invalidate().catch((error) => {
|
2015-10-18 21:17:02 +03:00
|
|
|
this.get('notifications').showAlert(error.message, {type: 'error', key: 'session.invalidate.failed'});
|
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) => {
|
2015-04-14 18:04:16 +03:00
|
|
|
if (!user.get('isAuthor') && !user.get('isEditor')) {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.store.findAll('notification', {reload: true}).then((serverNotifications) => {
|
|
|
|
serverNotifications.forEach((notification) => {
|
2016-07-16 01:58:35 +03:00
|
|
|
if (notification.get('type') === 'upgrade') {
|
|
|
|
this.get('upgradeNotification').set('content', notification.get('message'));
|
|
|
|
} else {
|
|
|
|
this.get('notifications').handleNotification(notification, isDelayed);
|
|
|
|
}
|
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'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-18 13:50:48 +03:00
|
|
|
toggleMarkdownHelpModal() {
|
|
|
|
this.get('controller').toggleProperty('showMarkdownHelpModal');
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
if (error && isEmberArray(error.errors)) {
|
|
|
|
switch (error.errors[0].errorType) {
|
2016-11-14 16:16:51 +03:00
|
|
|
case 'NotFoundError': {
|
|
|
|
if (transition) {
|
|
|
|
transition.abort();
|
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-14 16:16:51 +03:00
|
|
|
let routeInfo = transition.handlerInfos[transition.handlerInfos.length - 1];
|
|
|
|
let router = this.get('router');
|
|
|
|
let params = [];
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-14 16:16:51 +03:00
|
|
|
for (let key of Object.keys(routeInfo.params)) {
|
|
|
|
params.push(routeInfo.params[key]);
|
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-14 16:16:51 +03:00
|
|
|
return this.transitionTo('error404', router.generate(routeInfo.name, ...params).replace('/ghost/', '').replace(/^\//g, ''));
|
|
|
|
}
|
|
|
|
case 'VersionMismatchError': {
|
|
|
|
if (transition) {
|
|
|
|
transition.abort();
|
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-14 16:16:51 +03:00
|
|
|
this.get('upgradeStatus').requireUpgrade();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
case 'Maintenance': {
|
|
|
|
if (transition) {
|
|
|
|
transition.abort();
|
|
|
|
}
|
2016-07-08 16:54:36 +03:00
|
|
|
|
2016-11-14 16:16:51 +03:00
|
|
|
this.get('upgradeStatus').maintenanceAlert();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
this.get('notifications').showAPIError(error);
|
|
|
|
// don't show the 500 page if we weren't navigating
|
|
|
|
if (!transition) {
|
2016-07-08 16:54:36 +03:00
|
|
|
return false;
|
2016-11-14 16:16:51 +03:00
|
|
|
}
|
|
|
|
}
|
2016-06-30 17:45:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback to 500 error page
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-31 08:07:05 +04:00
|
|
|
}
|
|
|
|
});
|