Project

General

Profile

1

    
2
import {Injectable} from '@angular/core';
3
import {ActivatedRouteSnapshot, CanActivate, CanLoad, Router, RouterStateSnapshot} from '@angular/router';
4
import {AuthenticationService} from './authentication.service';
5
import { getCookie } from '../domain/utils';
6

    
7
@Injectable ()
8
export class AuthGuardService implements CanActivate, CanLoad {
9

    
10
  constructor (private authenticationService: AuthenticationService, private router: Router) {}
11

    
12
  canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
13
    if ( this.authenticationService.getIsUserLoggedIn() ) { return true; }
14

    
15
    /* If no cookie was found, clear the app's session.
16
       The user may have logged out using another OpenAIRE portal */
17
    sessionStorage.clear();
18

    
19
    // Store the attempted URL for redirecting
20
    sessionStorage.setItem('state.location', state.url);
21

    
22
    // If we decide that in this case we will send the user back to the aai
23
    // this.authenticationService.redirectUrl = state.url;
24
    // this.authenticationService.loginWithState();
25

    
26
    this.router.navigate(['/home']);
27

    
28
    return false;
29
  }
30

    
31
  canLoad () {
32

    
33
    if (this.authenticationService.getUserRole() &&
34
        (this.authenticationService.getUserRole().includes('SUPER_ADMINISTRATOR') ||
35
         this.authenticationService.getUserRole().includes('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')) ) {
36
      console.log('Admin recognized');
37
      return true;
38
    }
39

    
40
    this.router.navigate(['/home']);
41

    
42
    return false;
43
  }
44
}
(1-1/13)