mirror of
https://github.com/documize/community.git
synced 2025-08-07 22:45:24 +02:00
Update session stub
This commit is contained in:
parent
0ddbe70935
commit
fd42473f99
2 changed files with 47 additions and 172 deletions
|
@ -1,200 +1,63 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// 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
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
import Ember from 'ember';
|
||||
import models from 'documize/utils/model';
|
||||
import encodingUtil from 'documize/utils/encoding';
|
||||
import netUtil from 'documize/utils/net';
|
||||
import models from 'documize/utils/model';
|
||||
import SimpleAuthSession from 'ember-simple-auth/services/session';
|
||||
|
||||
const Session = Ember.Service.extend({
|
||||
const {
|
||||
inject: { service },
|
||||
computed: { oneWay, or },
|
||||
computed
|
||||
} = Ember;
|
||||
|
||||
const Session = SimpleAuthSession.extend({
|
||||
ajax: service(),
|
||||
appMeta: service(),
|
||||
|
||||
authenticated: oneWay('isAuthenticated'),
|
||||
isAdmin: oneWay('user.admin'),
|
||||
isEditor: or('user.admin', 'user.editor'),
|
||||
|
||||
user: computed('session.content.authenticated.user', function(){
|
||||
let user = this.get('session.content.authenticated.user');
|
||||
if (user) {
|
||||
return models.UserModel.create(user);
|
||||
}
|
||||
}),
|
||||
|
||||
ready: false,
|
||||
appMeta: null,
|
||||
isMac: false,
|
||||
isMobile: false,
|
||||
previousTransition: null,
|
||||
user: null,
|
||||
authenticated: false,
|
||||
folderPermissions: null,
|
||||
currentFolder: null,
|
||||
ajax: Ember.inject.service(),
|
||||
|
||||
isAdmin: function() {
|
||||
if (this.authenticated && is.not.null(this.user) && this.user.id !== "") {
|
||||
return this.user.admin;
|
||||
}
|
||||
return false;
|
||||
}.property('user'),
|
||||
|
||||
isEditor: function() {
|
||||
if (this.authenticated && is.not.null(this.user) && this.user.id !== "") {
|
||||
return this.user.editor || this.user.admin;
|
||||
}
|
||||
return false;
|
||||
}.property('user'),
|
||||
|
||||
// Boot up
|
||||
init: function() {
|
||||
this.set('user', models.UserModel.create());
|
||||
this.appMeta = models.AppMeta.create();
|
||||
|
||||
this.set('isMac', is.mac());
|
||||
this.set('isMobile', is.mobile());
|
||||
},
|
||||
|
||||
login: function(credentials) {
|
||||
let url = this.appMeta.getUrl('public/authenticate');
|
||||
let domain = netUtil.getSubdomain();
|
||||
|
||||
this.clearSession();
|
||||
|
||||
if (is.empty(credentials.email) || is.empty(credentials.password)) {
|
||||
return Ember.RSVP.reject("invalid");
|
||||
}
|
||||
|
||||
var encoded = encodingUtil.Base64.encode(domain + ":" + credentials.email + ":" + credentials.password);
|
||||
var headers = {
|
||||
'Authorization': 'Basic ' + encoded
|
||||
};
|
||||
|
||||
return this.get('ajax').post(url, {
|
||||
headers
|
||||
}).then((response)=>{
|
||||
this.setSession(response.token, models.UserModel.create(response.user));
|
||||
this.get('ready', true);
|
||||
return response;
|
||||
});
|
||||
},
|
||||
|
||||
sso: function(credentials) {
|
||||
let url = this.appMeta.getUrl('public/authenticate');
|
||||
this.clearSession();
|
||||
|
||||
if (is.empty(credentials.email) || is.empty(credentials.password)) {
|
||||
return Ember.RSVP.reject("invalid");
|
||||
}
|
||||
|
||||
var headers = {
|
||||
'Authorization': 'Basic ' + credentials
|
||||
};
|
||||
|
||||
return this.get('ajax').post(url, {
|
||||
headers
|
||||
}).then((response)=>{
|
||||
this.setSession(response.token, models.UserModel.create(response.user));
|
||||
this.get('ready', true);
|
||||
return response;
|
||||
});
|
||||
},
|
||||
|
||||
// Goodbye
|
||||
logout: function() {
|
||||
this.clearSession();
|
||||
},
|
||||
|
||||
// Session management
|
||||
setSession: function(token, user) {
|
||||
this.set('user', user);
|
||||
this.set('authenticated', true);
|
||||
|
||||
this.storeSessionItem('token', token);
|
||||
this.storeSessionItem('user', JSON.stringify(user));
|
||||
|
||||
let self = this;
|
||||
|
||||
$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
|
||||
// We only tack on auth header for Documize API calls
|
||||
if (is.startWith(options.url, self.get('appMeta.url'))) {
|
||||
jqXHR.setRequestHeader('Authorization', 'Bearer ' + token);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
clearSession: function() {
|
||||
this.set('user', null);
|
||||
this.set('authenticated', false);
|
||||
// TODO: clear session properly with ESA
|
||||
// localStorage.clear();
|
||||
},
|
||||
|
||||
storeSessionItem: function() {
|
||||
// localStorage[key] = data;
|
||||
// console.log(data);
|
||||
},
|
||||
|
||||
getSessionItem: function() {
|
||||
// return localStorage[key];
|
||||
// console.log(data);
|
||||
},
|
||||
|
||||
clearSessionItem: function() {
|
||||
// delete localStorage[key];
|
||||
},
|
||||
|
||||
boot() {
|
||||
let self = this;
|
||||
let dbhash = "";
|
||||
|
||||
if (is.not.null(document.head.querySelector("[property=dbhash]"))) {
|
||||
dbhash = document.head.querySelector("[property=dbhash]").content;
|
||||
}
|
||||
|
||||
if (dbhash.length > 0 && dbhash !== "{{.DBhash}}") {
|
||||
self.get('appMeta').set('orgId', "response.orgId");
|
||||
self.get('appMeta').setSafe('title', "Documize Setup");
|
||||
self.get('appMeta').set('version', "response.version");
|
||||
self.get('appMeta').setSafe('message', "response.message");
|
||||
self.get('appMeta').set('allowAnonymousAccess', false);
|
||||
self.set('ready', true);
|
||||
return new Ember.RSVP.Promise(function(resolve) {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
if (this.get('ready')) {
|
||||
return new Ember.RSVP.Promise(function(resolve) {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
// var blockedPopupTest = window.open("http://maintenance.documize.com", "directories=no,height=1,width=1,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,top=0,location=no");
|
||||
//
|
||||
// if (!blockedPopupTest) {
|
||||
// this.set('popupBlocked', true);
|
||||
// } else {
|
||||
// blockedPopupTest.close();
|
||||
// this.set('popupBlocked', false);
|
||||
// }
|
||||
|
||||
let url = this.get('appMeta').getUrl("public/meta");
|
||||
|
||||
return this.get('ajax').request(url)
|
||||
.then((response) => {
|
||||
this.get('appMeta').set('orgId', response.orgId);
|
||||
this.get('appMeta').setSafe('title', response.title);
|
||||
this.get('appMeta').set('version', response.version);
|
||||
this.get('appMeta').setSafe('message', response.message);
|
||||
this.get('appMeta').set('allowAnonymousAccess', response.allowAnonymousAccess);
|
||||
|
||||
let token = this.getSessionItem('token');
|
||||
|
||||
if (is.not.undefined(token)) {
|
||||
// We now validate current token
|
||||
let tokenCheckUrl = this.get('appMeta').getUrl(`public/validate?token=${token}`);
|
||||
|
||||
return this.get('ajax').request(tokenCheckUrl, {
|
||||
method: 'GET',
|
||||
contentType: 'json'
|
||||
}).then((user) => {
|
||||
this.setSession(token, models.UserModel.create(user));
|
||||
this.set('ready', true);
|
||||
}).catch((reason) => {
|
||||
if (reason.status === 401 || reason.status === 403) {
|
||||
// localStorage.clear();
|
||||
window.location.href = "/auth/login";
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
export default Ember.Test.registerAsyncHelper('stubSession', function(app, test, attrs={}) {
|
||||
test.register('service:session', Session.extend(attrs));
|
||||
});
|
||||
|
|
12
app/tests/unit/services/local-storage-test.js
Normal file
12
app/tests/unit/services/local-storage-test.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { moduleFor, test } from 'ember-qunit';
|
||||
|
||||
moduleFor('service:local-storage', 'Unit | Service | local storage', {
|
||||
// Specify the other units that are required for this test.
|
||||
// needs: ['service:foo']
|
||||
});
|
||||
|
||||
// Replace this with your real tests.
|
||||
test('it exists', function(assert) {
|
||||
let service = this.subject();
|
||||
assert.ok(service);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue