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

    
14
    if ( (getCookie('openAIREUser') !== null) &&
15
         (getCookie('openAIREUser') !== '') &&
16
         this.authenticationService.getIsUserLoggedIn() ) { return true; }
17

    
18
    if ( (getCookie('openAIREUser') !== null) && (getCookie('openAIREUser') !== '') ) { return true; }
19

    
20
    /* If no cookie was found, clear the app's session.
21
       The user may have logged out using another OpenAIRE portal */
22
    localStorage.clear();
23
    sessionStorage.clear();
24

    
25
    // Store the attempted URL for redirecting
26
    sessionStorage.setItem('state.location', state.url);
27

    
28
    // If we decide that in this case we will send the user back to the aai
29
    // this.authenticationService.redirectUrl = state.url;
30
    // this.authenticationService.loginWithState();
31

    
32
    this.router.navigate(['/landing']);
33

    
34
    return false;
35
  }
36

    
37
  canLoad () {
38

    
39
    if (this.authenticationService.getUserRole() &&
40
        (this.authenticationService.getUserRole().includes('ROLE_ADMIN') ||
41
         this.authenticationService.getUserRole().includes('ROLE_PORTAL_ADMIN')) ) {
42

    
43
      return true;
44
    }
45

    
46
    this.router.navigate(['/landing']);
47

    
48
    return false;
49
  }
50
}
(1-1/11)