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 ConnectAdminLoginGuard implements CanActivate {
13
  properties:EnvProperties;
14

    
15
  constructor(private route: ActivatedRouteSnapshot, private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService ) {}
16

    
17
  ngOnInit() {
18
    this.route.data
19
        .subscribe((data: { envSpecific: EnvProperties }) => {
20
           this.properties = data.envSpecific;
21
         });
22
  }
23

    
24
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
25
    console.log(state.url);
26
    var user;
27
    var loggedIn = false;
28
    var isAdmin = false;
29
    var errorCode = ErrorCodes.NOT_LOGGIN;
30
    let community = (route.queryParams["communityId"]);
31
    if(Session.isLoggedIn()){
32
      loggedIn = true;
33
      if(!Session.isValidAndRemove()){
34
        loggedIn = false;
35
        errorCode = ErrorCodes.NOT_VALID;
36
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl":  state.url } });
37

    
38
        return false;
39
      }else if(Session.isPortalAdministrator() || Session.isCommunityCurator()) {
40
        console.log("is Admin");
41
        isAdmin = true;
42
        return true;
43
      }else {
44
        let obs = this.propertiesService.subscribeEnvironment().map(res=>res["communityAPI"]).mergeMap(url => {
45
          return this.communityService.iscommunityManager(this.properties, url+community,Session.getUserEmail())});
46
        obs.filter(enabled => !enabled)
47
        .subscribe(() => this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl":  state.url } }));
48
        return obs;
49
       }
50
    }else{
51
      errorCode =ErrorCodes.NOT_LOGGIN;
52
      this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl":  state.url } });
53

    
54
      return false;
55
    }
56

    
57

    
58
  }
59
  /*
60
  constructor(private route: ActivatedRoute,private router: Router, private config: ConfigurationService) {}
61

    
62
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
63

    
64
         let customRedirect = route.data['redirect'];
65
         let api  = route.data['api'];
66
         let community  = route.data['community']
67
         if(!community){
68
           community = (route.queryParams["communityId"])?route.queryParams["communityId"]:route.queryParams["community"];
69
         }
70
         if(community){
71
           let isEnabled = this.config.isPageEnabled(api, community,"/"+state.url.split("?")[0].substring(1));
72
           let redirect = !!customRedirect ? customRedirect : '/error';
73

    
74
           isEnabled.filter(enabled => !enabled)
75
           .subscribe(() => this.router.navigate([redirect], { queryParams: { "page": state.url } }));
76
           return isEnabled;
77
         }
78
         return true;
79
      }
80
  */
81
}
(3-3/5)