2016-07-07 18:54:16 -07:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
2016-08-16 13:34:09 +02:00
|
|
|
// This software (Documize Community Edition) is licensed under
|
2016-07-07 18:54:16 -07:00
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
2016-08-16 13:34:09 +02:00
|
|
|
// by contacting <sales@documize.com>.
|
2016-07-07 18:54:16 -07:00
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import { inject as service } from '@ember/service';
|
2016-07-07 18:54:16 -07:00
|
|
|
import config from '../config/environment';
|
2018-06-11 14:40:21 +01:00
|
|
|
import AjaxService from 'ember-ajax/services/ajax';
|
2016-07-07 18:54:16 -07:00
|
|
|
|
|
|
|
export default AjaxService.extend({
|
|
|
|
session: service(),
|
2018-02-15 17:10:29 +00:00
|
|
|
localStorage: service(),
|
2018-11-07 15:56:05 +00:00
|
|
|
appMeta: service(),
|
2016-07-07 18:54:16 -07:00
|
|
|
host: config.apiHost,
|
|
|
|
namespace: config.apiNamespace,
|
|
|
|
|
2016-08-16 13:34:09 +02:00
|
|
|
headers: computed('session.session.content.authenticated.token', {
|
2016-07-07 18:54:16 -07:00
|
|
|
get() {
|
|
|
|
let headers = {};
|
|
|
|
const token = this.get('session.session.content.authenticated.token');
|
|
|
|
if (token) {
|
|
|
|
headers['authorization'] = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
return headers;
|
|
|
|
}
|
2017-04-27 12:49:10 +01:00
|
|
|
}),
|
|
|
|
|
|
|
|
handleResponse(status, headers /*, payload*/) {
|
|
|
|
try {
|
2018-11-07 15:56:05 +00:00
|
|
|
// Handle user permission changes.
|
2017-04-27 12:49:10 +01:00
|
|
|
let user = this.get('session.session.content.authenticated.user');
|
|
|
|
let userUpdate = headers['x-documize-status'];
|
2017-12-08 14:34:21 +00:00
|
|
|
let appVersion = headers['x-documize-version'];
|
|
|
|
|
2018-11-07 15:56:05 +00:00
|
|
|
// Unauthorized local API AJAX calls redirect to app root.
|
2019-03-03 13:10:04 +00:00
|
|
|
if (status === 401 && !_.isUndefined(appVersion) && !_.includes(window.location.href, '/auth')) {
|
2018-04-05 20:01:10 +01:00
|
|
|
this.get('localStorage').clearAll();
|
2017-12-08 14:34:21 +00:00
|
|
|
window.location.href = 'auth/login';
|
|
|
|
}
|
|
|
|
|
2018-11-07 15:56:05 +00:00
|
|
|
// Handle billing/licensing issue.
|
|
|
|
if (status === 402 || headers['x-documize-subscription'] === 'false') {
|
|
|
|
this.set('appMeta.valid', false);
|
|
|
|
}
|
2017-04-27 12:49:10 +01:00
|
|
|
|
2019-03-03 13:10:04 +00:00
|
|
|
if (this.get('session.authenticated') && !_.isEmpty(userUpdate) && !_.isUndefined(userUpdate)) {
|
2018-11-07 15:56:05 +00:00
|
|
|
let latest = JSON.parse(userUpdate);
|
|
|
|
// Permission change means re-validation.
|
|
|
|
if (!latest.active || user.editor !== latest.editor || user.admin !== latest.admin ||
|
|
|
|
user.analytics !== latest.analytics || user.viewUsers !== latest.viewUsers) {
|
2018-04-05 20:01:10 +01:00
|
|
|
this.get('localStorage').clearAll();
|
2017-04-27 12:49:10 +01:00
|
|
|
window.location.href = 'auth/login';
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 14:40:21 +01:00
|
|
|
} catch(e) {
|
|
|
|
console.log(e); // eslint-disable-line no-console
|
|
|
|
} // eslint-disable-line no-empty
|
2017-04-27 12:49:10 +01:00
|
|
|
|
|
|
|
return this._super(...arguments);
|
|
|
|
}
|
2016-08-16 13:34:09 +02:00
|
|
|
});
|