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
    sessionStorage.clear();
23

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

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

    
31
    this.router.navigate(['/home']);
32

    
33
    return false;
34
  }
35

    
36
  canLoad () {
37

    
38
    if (this.authenticationService.getUserRole() &&
39
        (this.authenticationService.getUserRole().includes('ROLE_ADMIN') ||
40
         this.authenticationService.getUserRole().includes('ROLE_PROVIDE_ADMIN')) ) {
41
      console.log('Admin recognized');
42
      return true;
43
    }
44

    
45
    this.router.navigate(['/home']);
46

    
47
    return false;
48
  }
49
}
(1-1/13)