2020-05-20 10:07:34 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import {ApiService} from './api.service';
|
2020-07-30 15:35:19 +02:00
|
|
|
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
2020-05-20 10:07:34 +02:00
|
|
|
import {LocalStorageService} from 'angular-2-local-storage';
|
2020-06-19 08:43:22 +02:00
|
|
|
import {Observable} from 'rxjs';
|
2020-05-20 10:07:34 +02:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class LoginService implements CanActivate {
|
|
|
|
|
2020-07-29 13:14:29 +02:00
|
|
|
private pathPermissions = [
|
|
|
|
{path: 'templates', permission: 'maintain'},
|
|
|
|
{path: 'users', permission: 'admin'}
|
|
|
|
];
|
|
|
|
readonly levels = [
|
|
|
|
'read',
|
|
|
|
'write',
|
|
|
|
'maintain',
|
|
|
|
'dev',
|
|
|
|
'admin'
|
|
|
|
];
|
2020-07-27 17:52:03 +02:00
|
|
|
|
2020-06-19 08:43:22 +02:00
|
|
|
private loggedIn;
|
2020-07-27 17:52:03 +02:00
|
|
|
private level;
|
2020-05-20 10:07:34 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private api: ApiService,
|
2020-07-30 15:35:19 +02:00
|
|
|
private storage: LocalStorageService,
|
|
|
|
private router: Router
|
2020-05-22 12:52:17 +02:00
|
|
|
) {
|
2020-06-19 08:43:22 +02:00
|
|
|
|
2020-05-22 12:52:17 +02:00
|
|
|
}
|
2020-05-20 10:07:34 +02:00
|
|
|
|
2020-05-22 12:52:17 +02:00
|
|
|
login(username = '', password = '') {
|
2020-05-20 10:07:34 +02:00
|
|
|
return new Promise(resolve => {
|
2020-07-29 13:14:29 +02:00
|
|
|
if (username !== '' || password !== '') { // some credentials given
|
|
|
|
let credentials: string[];
|
|
|
|
const credentialString: string = this.storage.get('basicAuth');
|
|
|
|
if (credentialString) { // found stored credentials
|
|
|
|
credentials = atob(credentialString).split(':');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
credentials = ['', ''];
|
|
|
|
}
|
|
|
|
if (username !== '' && password !== '') { // all credentials given
|
|
|
|
this.storage.set('basicAuth', btoa(username + ':' + password));
|
|
|
|
}
|
|
|
|
else if (username !== '') { // username given
|
|
|
|
this.storage.set('basicAuth', btoa(username + ':' + credentials[1]));
|
|
|
|
}
|
|
|
|
else if (password !== '') { // password given
|
|
|
|
this.storage.set('basicAuth', btoa(credentials[0] + ':' + password));
|
|
|
|
}
|
2020-05-22 12:52:17 +02:00
|
|
|
}
|
2020-06-19 08:43:22 +02:00
|
|
|
this.api.get('/authorized', (data: any, error) => {
|
|
|
|
if (!error) {
|
2020-05-20 10:07:34 +02:00
|
|
|
if (data.status === 'Authorization successful') {
|
|
|
|
this.loggedIn = true;
|
2020-07-27 17:52:03 +02:00
|
|
|
this.level = data.level;
|
2020-05-20 10:07:34 +02:00
|
|
|
resolve(true);
|
2020-06-19 08:43:22 +02:00
|
|
|
} else {
|
2020-05-20 10:07:34 +02:00
|
|
|
this.loggedIn = false;
|
|
|
|
this.storage.remove('basicAuth');
|
|
|
|
resolve(false);
|
|
|
|
}
|
2020-06-19 08:43:22 +02:00
|
|
|
} else {
|
2020-05-20 10:07:34 +02:00
|
|
|
this.loggedIn = false;
|
|
|
|
this.storage.remove('basicAuth');
|
|
|
|
resolve(false);
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
});
|
2020-05-20 10:07:34 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-14 09:39:37 +02:00
|
|
|
logout() {
|
|
|
|
this.storage.remove('basicAuth');
|
|
|
|
this.loggedIn = false;
|
|
|
|
}
|
|
|
|
|
2020-06-19 08:43:22 +02:00
|
|
|
canActivate(route: ActivatedRouteSnapshot = null, state: RouterStateSnapshot = null): Observable<boolean> {
|
|
|
|
return new Observable<boolean>(observer => {
|
2020-07-30 15:35:19 +02:00
|
|
|
new Promise(resolve => {
|
2020-07-27 17:52:03 +02:00
|
|
|
if (this.loggedIn === undefined) {
|
|
|
|
this.login().then(res => {
|
2020-07-30 15:35:19 +02:00
|
|
|
resolve(res);
|
2020-07-27 17:52:03 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2020-07-30 15:35:19 +02:00
|
|
|
resolve(this.loggedIn);
|
2020-07-27 17:52:03 +02:00
|
|
|
}
|
2020-07-30 15:35:19 +02:00
|
|
|
}).then(res => {
|
|
|
|
const pathPermission = this.pathPermissions.find(e => e.path.indexOf(route.url[0].path) >= 0);
|
|
|
|
const ok = res && !pathPermission || this.is(pathPermission.permission); // check if level is permitted for path
|
|
|
|
observer.next(ok);
|
2020-06-19 08:43:22 +02:00
|
|
|
observer.complete();
|
2020-07-30 15:35:19 +02:00
|
|
|
if (!ok) {
|
|
|
|
this.router.navigate(['/']);
|
|
|
|
}
|
|
|
|
});
|
2020-06-19 08:43:22 +02:00
|
|
|
});
|
2020-05-20 10:07:34 +02:00
|
|
|
}
|
2020-07-13 10:52:10 +02:00
|
|
|
|
|
|
|
get isLoggedIn() {
|
|
|
|
return this.loggedIn;
|
|
|
|
}
|
2020-07-14 09:39:37 +02:00
|
|
|
|
2020-07-29 13:14:29 +02:00
|
|
|
is(level) {
|
|
|
|
return this.levels.indexOf(this.level) >= this.levels.indexOf(level);
|
2020-07-27 17:52:03 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 09:39:37 +02:00
|
|
|
get username() {
|
|
|
|
return atob(this.storage.get('basicAuth')).split(':')[0];
|
|
|
|
}
|
2020-05-20 10:07:34 +02:00
|
|
|
}
|