Project

General

Profile

« Previous | Next » 

Revision 60684

[Library | Trunk]: Fix is Community guard

View differences:

modules/uoa-services-library/trunk/ng-openaire-library/src/app/connect/communityGuard/isCommunity.guard.ts
4 4
  CanActivate,
5 5
  ActivatedRouteSnapshot,
6 6
  RouterStateSnapshot,
7
  CanLoad, Route, UrlSegment
7
  CanLoad, Route, UrlSegment, CanActivateChild, UrlTree
8 8
} from '@angular/router';
9 9
import {Observable} from 'rxjs';
10 10

  
11 11
import {ConnectHelper} from '../connectHelper';
12 12
import {properties} from "../../../../environments/environment";
13
import {CommunityService} from "../community/community.service";
14
import {map} from "rxjs/operators";
13 15

  
14 16
@Injectable()
15
export class IsCommunity implements CanActivate, CanLoad {
17
export class IsCommunity implements CanActivate, CanActivateChild {
16 18

  
17
  constructor(private router: Router) {
19
  constructor(private router: Router,
20
              private communityService: CommunityService) {
18 21
  }
19 22

  
20
  check(community: string): Observable<boolean> | boolean {
21
    if (community && community !== 'undefined') {
22
      return true;
23
  check(route: ActivatedRouteSnapshot): Observable<boolean> | boolean {
24
    let community;
25
    if(properties.isDashboard) {
26
      community = route.params['community'];
23 27
    } else {
28
      community = route.queryParams['communityId'];
29
      if(!community) {
30
        community = ConnectHelper.getCommunityFromDomain(properties.domain);
31
      }
32
    }
33
    if (community) {
34
      return this.communityService.getCommunity(community).pipe(map(community => {
35
        if(community) {
36
          return true;
37
        } else {
38
          this.router.navigate(['error']);
39
          return false;
40
        }
41
      }));
42
    } else {
24 43
      this.router.navigate(['error']);
25 44
      return false;
26 45
    }
27 46
  }
28 47

  
29 48
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
30
    return this.check((route.queryParams['communityId']) ? route.queryParams['communityId'] :  ConnectHelper.getCommunityFromDomain(properties.domain));
49
    return this.check(route);
31 50
  }
32

  
33
  canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
34
    const path = '/' + route.path + document.location.search;
35
    return this.check(ConnectHelper.getCommunityFromPath(path));
51
  
52
  canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
53
    return this.check(childRoute);
36 54
  }
37 55
}

Also available in: Unified diff