1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 13:19:43 +02:00
documize/gui/app/services/ajax.js

70 lines
2.2 KiB
JavaScript
Raw Normal View History

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
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(),
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;
}
}),
handleResponse(status, headers /*, payload*/) {
try {
2018-11-07 15:56:05 +00:00
// Handle user permission changes.
let user = this.get('session.session.content.authenticated.user');
let userUpdate = headers['x-documize-status'];
let appVersion = headers['x-documize-version'];
2018-11-07 15:56:05 +00:00
// Unauthorized local API AJAX calls redirect to app root.
if (status === 401 && !_.isUndefined(appVersion) && !_.includes(window.location.href, '/auth')) {
this.get('localStorage').clearAll();
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);
}
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) {
this.get('localStorage').clearAll();
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
return this._super(...arguments);
}
2016-08-16 13:34:09 +02:00
});