2019-01-23 12:11:44 +00:00
|
|
|
import ngModule from '../module';
|
|
|
|
import UserError from 'core/lib/user-error';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authentication service.
|
|
|
|
*
|
|
|
|
* @property {Boolean} loggedIn Whether the user is currently logged
|
|
|
|
*/
|
|
|
|
export default class Auth {
|
2022-11-07 14:15:00 +00:00
|
|
|
constructor($http, $q, $state, $transitions, $window, vnToken, vnModules, aclService) {
|
2019-01-23 12:11:44 +00:00
|
|
|
Object.assign(this, {
|
|
|
|
$http,
|
2019-01-25 14:09:29 +00:00
|
|
|
$q,
|
2019-01-23 12:11:44 +00:00
|
|
|
$state,
|
|
|
|
$transitions,
|
|
|
|
$window,
|
|
|
|
vnToken,
|
|
|
|
vnModules,
|
|
|
|
aclService,
|
2022-11-07 14:15:00 +00:00
|
|
|
loggedIn: false
|
2019-01-23 12:11:44 +00:00
|
|
|
});
|
|
|
|
}
|
2019-10-24 10:44:36 +00:00
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
initialize() {
|
|
|
|
let criteria = {
|
2022-11-22 13:57:52 +00:00
|
|
|
to: state => {
|
2023-06-13 09:49:42 +00:00
|
|
|
const outLayout = ['login', 'recover-password', 'reset-password', 'change-password', 'validate-email'];
|
2022-11-22 13:57:52 +00:00
|
|
|
return !outLayout.some(ol => ol == state.name);
|
|
|
|
}
|
2019-01-23 12:11:44 +00:00
|
|
|
};
|
|
|
|
this.$transitions.onStart(criteria, transition => {
|
2019-01-25 11:04:35 +00:00
|
|
|
if (this.loggedIn)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
let redirectToLogin = () => {
|
2022-09-26 06:48:46 +00:00
|
|
|
return transition.router.stateService.target('login', {
|
|
|
|
continue: this.$window.location.hash
|
|
|
|
});
|
2019-01-25 11:04:35 +00:00
|
|
|
};
|
2019-01-23 12:11:44 +00:00
|
|
|
|
2022-09-26 06:48:46 +00:00
|
|
|
if (this.vnToken.token) {
|
2019-01-25 11:04:35 +00:00
|
|
|
return this.loadAcls()
|
|
|
|
.then(() => true)
|
|
|
|
.catch(redirectToLogin);
|
|
|
|
} else
|
|
|
|
return redirectToLogin();
|
2019-01-23 12:11:44 +00:00
|
|
|
});
|
|
|
|
}
|
2019-10-24 10:44:36 +00:00
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
login(user, password, remember) {
|
2020-03-25 19:44:59 +00:00
|
|
|
if (!user) {
|
|
|
|
let err = new UserError('Please enter your username');
|
|
|
|
err.code = 'EmptyLogin';
|
|
|
|
return this.$q.reject(err);
|
|
|
|
}
|
2019-01-23 12:11:44 +00:00
|
|
|
|
|
|
|
let params = {
|
|
|
|
user,
|
|
|
|
password: password || undefined
|
|
|
|
};
|
2019-01-25 11:04:35 +00:00
|
|
|
|
2023-07-03 11:33:48 +00:00
|
|
|
const now = new Date();
|
|
|
|
return this.$http.post('VnUsers/sign-in', params).then(
|
|
|
|
json => this.onLoginOk(json, now, remember));
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
2019-10-24 10:44:36 +00:00
|
|
|
|
2023-04-06 12:59:25 +00:00
|
|
|
validateCode(user, password, code, remember) {
|
|
|
|
if (!user) {
|
|
|
|
let err = new UserError('Please enter your username');
|
|
|
|
err.code = 'EmptyLogin';
|
|
|
|
return this.$q.reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
user: user,
|
|
|
|
password: password || undefined,
|
|
|
|
code: code
|
|
|
|
};
|
|
|
|
|
2023-07-03 11:33:48 +00:00
|
|
|
const now = new Date();
|
|
|
|
return this.$http.post('VnUsers/validate-auth', params)
|
|
|
|
.then(json => this.onLoginOk(json, now, remember));
|
2023-04-06 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 11:33:48 +00:00
|
|
|
onLoginOk(json, now, remember) {
|
2024-02-27 13:26:54 +00:00
|
|
|
return this.$http.get('VnUsers/ShareToken', {
|
|
|
|
headers: {Authorization: json.data.token}
|
|
|
|
}).then(({data}) => {
|
|
|
|
this.vnToken.set(json.data.token, data.multimediaToken.id, now, json.data.ttl, remember);
|
|
|
|
this.loadAcls().then(() => {
|
|
|
|
let continueHash = this.$state.params.continue;
|
|
|
|
if (continueHash)
|
|
|
|
this.$window.location = continueHash;
|
|
|
|
else
|
|
|
|
this.$state.go('home');
|
|
|
|
});
|
|
|
|
}).catch(() => {});
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
2019-10-24 10:44:36 +00:00
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
logout() {
|
2023-01-31 13:57:24 +00:00
|
|
|
let promise = this.$http.post('VnUsers/logout', null, {
|
2019-01-23 12:11:44 +00:00
|
|
|
headers: {Authorization: this.vnToken.token}
|
2019-01-25 16:05:53 +00:00
|
|
|
}).catch(() => {});
|
2019-01-23 12:11:44 +00:00
|
|
|
|
|
|
|
this.vnToken.unset();
|
|
|
|
this.loggedIn = false;
|
2019-01-25 11:04:35 +00:00
|
|
|
this.vnModules.reset();
|
|
|
|
this.aclService.reset();
|
2022-09-22 07:22:24 +00:00
|
|
|
this.$state.go('login');
|
2019-01-23 12:11:44 +00:00
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
2019-10-24 10:44:36 +00:00
|
|
|
|
2019-01-25 11:04:35 +00:00
|
|
|
loadAcls() {
|
|
|
|
return this.aclService.load()
|
|
|
|
.then(() => {
|
|
|
|
this.loggedIn = true;
|
|
|
|
this.vnModules.reset();
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
this.vnToken.unset();
|
|
|
|
throw err;
|
|
|
|
});
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-07 14:15:00 +00:00
|
|
|
Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnModules', 'aclService'];
|
2019-01-23 12:11:44 +00:00
|
|
|
|
|
|
|
ngModule.service('vnAuth', Auth);
|