Project

General

Profile

1
import { Injectable } from '@angular/core';
2
import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
3
import {Observable}       from 'rxjs/Observable';
4
import {Session} from '../../login/utils/helper.class';
5
import {ErrorCodes} from '../../login/utils/guardHelper.class';
6
import {CommunityService} from '../community/community.service';
7
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
8
import { mergeMap } from 'rxjs/operators';
9
import { EnvProperties} from '../../utils/properties/env-properties';
10

    
11
@Injectable()
12
export class ConnectRIGuard implements CanActivate {
13
  constructor(private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService ) {}
14

    
15
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
16
     console.log(state.url);
17
    var user;
18
    var loggedIn = false;
19
    var isAdmin = false;
20
    var errorCode = ErrorCodes.NOT_LOGGIN;
21
    var properties:EnvProperties = route.data.envSpecific;
22

    
23
    let community = (route.queryParams["communityId"]);
24
    if(Session.isLoggedIn()){
25
      loggedIn = true;
26
      if(!Session.isValidAndRemove()){
27
        loggedIn = false;
28
        errorCode = ErrorCodes.NOT_VALID;
29
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl":  state.url } });
30

    
31
        return false;
32
      }else if(Session.isPortalAdministrator()) {
33
        isAdmin = true;
34
        return true;
35
      }else {
36

    
37
        let obs = this.propertiesService.subscribeEnvironment().map(res=>res["communityAPI"]).mergeMap(url => {
38
          return this.communityService.iscommunityRI(properties, url+community)});
39
        obs.filter(enabled => !enabled)
40
        .subscribe(() => this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl":  state.url } }));
41
        return obs;
42
       }
43
    }else{
44
      errorCode =ErrorCodes.NOT_LOGGIN;
45
      this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl":  state.url } });
46

    
47
      return false;
48
    }
49

    
50

    
51
  }
52
  /*
53
  constructor(private route: ActivatedRoute,private router: Router, private config: ConfigurationService) {}
54

    
55
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
56

    
57
         let customRedirect = route.data['redirect'];
58
         let api  = route.data['api'];
59
         let community  = route.data['community']
60
         if(!community){
61
           community = (route.queryParams["communityId"])?route.queryParams["communityId"]:route.queryParams["community"];
62
         }
63
         if(community){
64
           let isEnabled = this.config.isPageEnabled(api, community,"/"+state.url.split("?")[0].substring(1));
65
           let redirect = !!customRedirect ? customRedirect : '/error';
66

    
67
           isEnabled.filter(enabled => !enabled)
68
           .subscribe(() => this.router.navigate([redirect], { queryParams: { "page": state.url } }));
69
           return isEnabled;
70
         }
71
         return true;
72
      }
73
  */
74
}
(4-4/5)