2017-05-29 21:50:03 +03:00
|
|
|
import SessionService from 'ember-simple-auth/services/session';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
2021-04-12 15:21:57 +03:00
|
|
|
import {getOwner} from '@ember/application';
|
|
|
|
import {run} from '@ember/runloop';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2015-10-18 21:17:02 +03:00
|
|
|
|
|
|
|
export default SessionService.extend({
|
2019-01-02 12:58:55 +03:00
|
|
|
dataStore: service('store'), // SessionService.store already exists
|
2021-04-12 15:21:57 +03:00
|
|
|
notifications: service(),
|
|
|
|
router: service(),
|
|
|
|
upgradeStatus: service(),
|
2015-10-18 21:17:02 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
user: computed(function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.dataStore.queryRecord('user', {id: 'me'});
|
2016-05-05 17:03:09 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
authenticate() {
|
2019-03-25 15:58:14 +03:00
|
|
|
// ensure any cached this.user value is removed and re-fetched
|
|
|
|
this.notifyPropertyChange('user');
|
|
|
|
|
2019-08-12 11:11:10 +03:00
|
|
|
return this._super(...arguments);
|
2021-04-12 15:21:57 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-05-05 17:03:09 +03:00
|
|
|
}
|
2015-10-18 21:17:02 +03:00
|
|
|
});
|