improved canActivate method to redirect and cover all cases

This commit is contained in:
VLE2FE 2020-07-30 15:35:19 +02:00
parent 2fb5d846d6
commit 72ecdad573
3 changed files with 15 additions and 22 deletions

View File

@ -1,9 +0,0 @@
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

View File

@ -7,7 +7,7 @@ import {Router} from '@angular/router';
// TODO: filter by not completely filled/no measurements
// TODO: validation of samples
// TODO: get rid of chart.js (+moment.js) and lodash
// TODO: get rid of chart.js (+moment.js)
@Component({
selector: 'app-root',

View File

@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import {ApiService} from './api.service';
import {ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot} from '@angular/router';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {LocalStorageService} from 'angular-2-local-storage';
import {Observable} from 'rxjs';
@ -26,7 +26,8 @@ export class LoginService implements CanActivate {
constructor(
private api: ApiService,
private storage: LocalStorageService
private storage: LocalStorageService,
private router: Router
) {
}
@ -79,23 +80,24 @@ export class LoginService implements CanActivate {
canActivate(route: ActivatedRouteSnapshot = null, state: RouterStateSnapshot = null): Observable<boolean> {
return new Observable<boolean>(observer => {
const pathPermission = this.pathPermissions.find(e => e.path.indexOf(route.url[0].path) >= 0);
if (!pathPermission || this.is(pathPermission.permission)) { // check if level is permitted for path
new Promise(resolve => {
if (this.loggedIn === undefined) {
this.login().then(res => {
observer.next(res as any);
observer.complete();
resolve(res);
});
}
else {
observer.next(this.loggedIn);
observer.complete();
resolve(this.loggedIn);
}
}
else {
observer.next(false);
}).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);
observer.complete();
}
if (!ok) {
this.router.navigate(['/']);
}
});
});
}