2021-07-08 16:37:31 +03:00
|
|
|
import ESASessionService from 'ember-simple-auth/services/session';
|
|
|
|
import RSVP from 'rsvp';
|
2022-09-24 14:25:00 +03:00
|
|
|
import {configureScope} from '@sentry/ember';
|
2021-04-12 15:21:57 +03:00
|
|
|
import {getOwner} from '@ember/application';
|
2024-02-14 00:23:03 +03:00
|
|
|
import {identifyUser, resetUser} from '../utils/analytics';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2021-04-12 15:21:57 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-03-10 14:53:30 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2021-07-08 16:37:31 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2015-10-18 21:17:02 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
export default class SessionService extends ESASessionService {
|
2022-11-03 14:14:36 +03:00
|
|
|
@service configManager;
|
2021-07-08 16:37:31 +03:00
|
|
|
@service('store') dataStore;
|
|
|
|
@service feature;
|
2023-10-04 19:36:24 +03:00
|
|
|
@service koenig;
|
2021-07-08 16:37:31 +03:00
|
|
|
@service notifications;
|
|
|
|
@service router;
|
2021-10-28 15:59:41 +03:00
|
|
|
@service frontend;
|
2021-07-08 16:37:31 +03:00
|
|
|
@service settings;
|
2021-08-31 16:21:00 +03:00
|
|
|
@service ui;
|
2021-07-08 16:37:31 +03:00
|
|
|
@service upgradeStatus;
|
|
|
|
@service whatsNew;
|
2022-10-27 12:44:19 +03:00
|
|
|
@service membersUtils;
|
2023-01-06 15:44:27 +03:00
|
|
|
@service themeManagement;
|
2015-10-18 21:17:02 +03:00
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
@inject config;
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
@tracked user = null;
|
2016-05-05 17:03:09 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
skipAuthSuccessHandler = false;
|
2019-03-25 15:58:14 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
async populateUser(options = {}) {
|
|
|
|
if (this.user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = options.id || 'me';
|
|
|
|
const user = await this.dataStore.queryRecord('user', {id});
|
|
|
|
this.user = user;
|
|
|
|
}
|
|
|
|
|
|
|
|
async postAuthPreparation() {
|
|
|
|
await RSVP.all([
|
2022-11-03 14:14:36 +03:00
|
|
|
this.configManager.fetchAuthenticated(),
|
2021-07-08 16:37:31 +03:00
|
|
|
this.feature.fetch(),
|
2022-10-27 12:44:19 +03:00
|
|
|
this.settings.fetch(),
|
|
|
|
this.membersUtils.fetch()
|
2021-07-08 16:37:31 +03:00
|
|
|
]);
|
2021-11-09 18:38:48 +03:00
|
|
|
|
2024-02-14 00:23:03 +03:00
|
|
|
// Identify the user to our analytics service upon successful login
|
|
|
|
await identifyUser(this.user);
|
|
|
|
|
2023-01-06 15:44:27 +03:00
|
|
|
// Theme management requires features to be loaded
|
|
|
|
this.themeManagement.fetch().catch(console.error); // eslint-disable-line no-console
|
|
|
|
|
2021-10-28 15:59:41 +03:00
|
|
|
await this.frontend.loginIfNeeded();
|
2021-11-09 18:38:48 +03:00
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
// update Sentry with the full Ghost version which we only get after authentication
|
2022-10-07 17:24:03 +03:00
|
|
|
if (this.config.sentry_dsn) {
|
2021-07-08 16:37:31 +03:00
|
|
|
configureScope((scope) => {
|
|
|
|
scope.addEventProcessor((event) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
resolve({
|
|
|
|
...event,
|
2022-12-05 20:15:49 +03:00
|
|
|
release: `ghost@${this.config.version}`,
|
2023-10-25 06:32:05 +03:00
|
|
|
user: {
|
|
|
|
role: this.user.role.name
|
|
|
|
}
|
2021-07-08 16:37:31 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loadServerNotifications();
|
|
|
|
this.whatsNew.fetchLatest.perform();
|
2023-10-04 19:36:24 +03:00
|
|
|
|
|
|
|
// pre-emptively load editor code in the background to avoid loading state when opening editor
|
|
|
|
this.koenig.fetch();
|
2021-07-08 16:37:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async handleAuthentication() {
|
2022-03-10 14:53:30 +03:00
|
|
|
if (this.handleAuthenticationTask.isRunning) {
|
|
|
|
return this.handleAuthenticationTask.last;
|
2021-07-14 15:15:59 +03:00
|
|
|
}
|
|
|
|
|
2022-03-10 14:53:30 +03:00
|
|
|
return this.handleAuthenticationTask.perform(() => {
|
|
|
|
if (this.skipAuthSuccessHandler) {
|
|
|
|
this.skipAuthSuccessHandler = false;
|
|
|
|
return;
|
|
|
|
}
|
2021-07-14 14:27:51 +03:00
|
|
|
|
2022-03-10 14:53:30 +03:00
|
|
|
super.handleAuthentication('home');
|
|
|
|
});
|
2021-07-08 16:37:31 +03:00
|
|
|
}
|
2021-04-12 15:21:57 +03:00
|
|
|
|
2022-10-21 23:03:12 +03:00
|
|
|
/**
|
|
|
|
* Always try to re-setup session & retry the original transition
|
|
|
|
* if user data is still available in session store although the
|
|
|
|
* ember-session is unauthenticated.
|
|
|
|
*
|
|
|
|
* If success, it will retry the original transition.
|
|
|
|
* If failed, it will be handled by the redirect to sign in.
|
|
|
|
*/
|
|
|
|
async requireAuthentication(transition, route) {
|
2024-02-14 00:23:03 +03:00
|
|
|
if (this.isAuthenticated && this.user) {
|
|
|
|
identifyUser(this.user);
|
|
|
|
}
|
|
|
|
|
2022-10-21 23:03:12 +03:00
|
|
|
// Only when ember session invalidated
|
|
|
|
if (!this.isAuthenticated) {
|
|
|
|
transition.abort();
|
|
|
|
|
|
|
|
if (this.user) {
|
|
|
|
await this.setup();
|
2024-02-14 00:23:03 +03:00
|
|
|
identifyUser(this.user);
|
2022-10-21 23:03:12 +03:00
|
|
|
this.notifications.clearAll();
|
|
|
|
transition.retry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super.requireAuthentication(transition, route);
|
|
|
|
}
|
|
|
|
|
2021-04-12 15:21:57 +03:00
|
|
|
handleInvalidation() {
|
|
|
|
let transition = this.appLoadTransition;
|
|
|
|
|
2024-02-14 00:23:03 +03:00
|
|
|
// Reset the PostHog user when the session is invalidated (e.g. signout, token expiry, etc.)
|
|
|
|
resetUser();
|
|
|
|
|
2021-04-12 15:21:57 +03:00
|
|
|
if (transition) {
|
|
|
|
transition.send('authorizationFailed');
|
|
|
|
} else {
|
|
|
|
run.scheduleOnce('routerTransitions', this, 'triggerAuthorizationFailed');
|
|
|
|
}
|
2021-07-08 16:37:31 +03:00
|
|
|
}
|
2021-04-12 15:21:57 +03:00
|
|
|
|
|
|
|
// TODO: this feels hacky, find a better way than using .send
|
|
|
|
triggerAuthorizationFailed() {
|
2022-11-03 14:14:36 +03:00
|
|
|
getOwner(this).lookup(`route:${this.router.currentRouteName}`)?.send('authorizationFailed');
|
2021-07-08 16:37:31 +03:00
|
|
|
}
|
2021-04-12 15:21:57 +03:00
|
|
|
|
|
|
|
loadServerNotifications() {
|
|
|
|
if (this.isAuthenticated) {
|
2021-07-08 16:37:31 +03:00
|
|
|
if (!this.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);
|
|
|
|
}
|
2021-04-12 15:21:57 +03:00
|
|
|
});
|
2021-07-08 16:37:31 +03:00
|
|
|
});
|
|
|
|
}
|
2021-04-12 15:21:57 +03:00
|
|
|
}
|
2016-05-05 17:03:09 +03:00
|
|
|
}
|
2022-03-10 14:53:30 +03:00
|
|
|
|
|
|
|
@task({drop: true})
|
|
|
|
*handleAuthenticationTask(callback) {
|
|
|
|
if (!this.user) {
|
|
|
|
try {
|
|
|
|
yield this.populateUser();
|
|
|
|
} catch (err) {
|
|
|
|
yield this.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
yield this.postAuthPreparation();
|
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
2021-07-08 16:37:31 +03:00
|
|
|
}
|