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 {LoginErrorCodes} 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

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

    
14
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
15
    console.log(state.url);
16
    var user;
17
    var loggedIn = false;
18
    var isAdmin = false;
19
    var errorCode = LoginErrorCodes.NOT_LOGIN;
20

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

    
29
        return false;
30
      }else if(Session.isPortalAdministrator() || Session.isCommunityCurator()) {
31
        console.log("is Admin");
32
        isAdmin = true;
33
        return true;
34
      }else {
35
        let obs = this.propertiesService.subscribeEnvironment().map(res=>res).mergeMap(properties => {
36
          return this.communityService.iscommunityManager(properties, properties["communityAPI"]+community,Session.getUserEmail())});
37
        obs.filter(enabled => !enabled)
38
        .subscribe(() => this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl":  state.url } }));
39
        return obs;
40
       }
41
    }else{
42
      errorCode =LoginErrorCodes.NOT_LOGIN;
43
      this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl":  state.url } });
44

    
45
      return false;
46
    }
47

    
48

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

    
53
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
54

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

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