improved canActivate method to redirect and cover all cases
This commit is contained in:
		@@ -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;
 | 
					 | 
				
			||||||
@@ -7,7 +7,7 @@ import {Router} from '@angular/router';
 | 
				
			|||||||
// TODO: filter by not completely filled/no measurements
 | 
					// TODO: filter by not completely filled/no measurements
 | 
				
			||||||
// TODO: validation of samples
 | 
					// TODO: validation of samples
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO: get rid of chart.js (+moment.js) and lodash
 | 
					// TODO: get rid of chart.js (+moment.js)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Component({
 | 
					@Component({
 | 
				
			||||||
  selector: 'app-root',
 | 
					  selector: 'app-root',
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
import { Injectable } from '@angular/core';
 | 
					import { Injectable } from '@angular/core';
 | 
				
			||||||
import {ApiService} from './api.service';
 | 
					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 {LocalStorageService} from 'angular-2-local-storage';
 | 
				
			||||||
import {Observable} from 'rxjs';
 | 
					import {Observable} from 'rxjs';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -26,7 +26,8 @@ export class LoginService implements CanActivate {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  constructor(
 | 
					  constructor(
 | 
				
			||||||
    private api: ApiService,
 | 
					    private api: ApiService,
 | 
				
			||||||
    private storage: LocalStorageService
 | 
					    private storage: LocalStorageService,
 | 
				
			||||||
 | 
					    private router: Router
 | 
				
			||||||
  ) {
 | 
					  ) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -79,24 +80,25 @@ export class LoginService implements CanActivate {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  canActivate(route: ActivatedRouteSnapshot = null, state: RouterStateSnapshot = null): Observable<boolean> {
 | 
					  canActivate(route: ActivatedRouteSnapshot = null, state: RouterStateSnapshot = null): Observable<boolean> {
 | 
				
			||||||
    return new Observable<boolean>(observer => {
 | 
					    return new Observable<boolean>(observer => {
 | 
				
			||||||
      const pathPermission = this.pathPermissions.find(e => e.path.indexOf(route.url[0].path) >= 0);
 | 
					      new Promise(resolve => {
 | 
				
			||||||
      if (!pathPermission || this.is(pathPermission.permission)) {  // check if level is permitted for path
 | 
					 | 
				
			||||||
        if (this.loggedIn === undefined) {
 | 
					        if (this.loggedIn === undefined) {
 | 
				
			||||||
          this.login().then(res => {
 | 
					          this.login().then(res => {
 | 
				
			||||||
            observer.next(res as any);
 | 
					            resolve(res);
 | 
				
			||||||
            observer.complete();
 | 
					 | 
				
			||||||
          });
 | 
					          });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else {
 | 
					        else {
 | 
				
			||||||
          observer.next(this.loggedIn);
 | 
					          resolve(this.loggedIn);
 | 
				
			||||||
          observer.complete();
 | 
					        }
 | 
				
			||||||
        }
 | 
					      }).then(res => {
 | 
				
			||||||
      }
 | 
					        const pathPermission = this.pathPermissions.find(e => e.path.indexOf(route.url[0].path) >= 0);
 | 
				
			||||||
      else {
 | 
					        const ok = res && !pathPermission || this.is(pathPermission.permission); // check if level is permitted for path
 | 
				
			||||||
        observer.next(false);
 | 
					        observer.next(ok);
 | 
				
			||||||
        observer.complete();
 | 
					        observer.complete();
 | 
				
			||||||
 | 
					        if (!ok) {
 | 
				
			||||||
 | 
					          this.router.navigate(['/']);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  get isLoggedIn() {
 | 
					  get isLoggedIn() {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user