|
1 | 1 | import { Component, OnInit, Injectable } from '@angular/core'; |
2 | 2 | import { AppService } from '../app.service'; |
3 | 3 | import { FormBuilder, FormGroup } from '@angular/forms'; |
4 | | -import { CloudAppConfigService, CloudAppEventsService } from '@exlibris/exl-cloudapp-angular-lib'; |
| 4 | +import { CloudAppConfigService, CloudAppEventsService, CloudAppRestService } from '@exlibris/exl-cloudapp-angular-lib'; |
5 | 5 | import { ToastrService } from 'ngx-toastr'; |
6 | 6 | import { CanActivate, Router } from '@angular/router'; |
7 | 7 | import { Observable } from 'rxjs'; |
8 | | -import { map } from 'rxjs/operators'; |
| 8 | +import { map, flatMap } from 'rxjs/operators'; |
9 | 9 | import { ErrorMessages } from '../static/error.component'; |
10 | 10 |
|
11 | 11 | @Component({ |
@@ -58,18 +58,28 @@ export class ConfigurationComponent implements OnInit { |
58 | 58 | providedIn: 'root', |
59 | 59 | }) |
60 | 60 | export class ConfigurationGuard implements CanActivate { |
61 | | - constructor( |
| 61 | + constructor ( |
62 | 62 | private eventsService: CloudAppEventsService, |
| 63 | + private restService: CloudAppRestService, |
63 | 64 | private router: Router |
64 | 65 | ) {} |
| 66 | + |
65 | 67 | canActivate(): Observable<boolean> { |
66 | | - return this.eventsService.getInitData().pipe(map( data => { |
67 | | - if (!data.user.isAdmin) { |
68 | | - this.router.navigate(['/error'], |
69 | | - { queryParams: { error: ErrorMessages.NO_ACCESS}}); |
70 | | - return false; |
71 | | - } |
72 | | - return true; |
73 | | - })) |
| 68 | + return this.eventsService.getInitData().pipe( |
| 69 | + /* Until primaryId is available: */ |
| 70 | + map( data => `first_name~${encodeURIComponent(data.user.firstName.replace(' ', '+'))}+AND+last_name~${encodeURIComponent(data.user.lastName.replace(' ','+'))}`), |
| 71 | + flatMap( query => this.restService.call(`/users?q=${query}`)), |
| 72 | + map( resp => resp.user[0].primary_id ), |
| 73 | + // map( data => data.user.primaryId ), |
| 74 | + flatMap( primaryId => this.restService.call(`/users/${primaryId}`)), |
| 75 | + map( user => { |
| 76 | + if (!user.user_role.some(role=>role.role_type.value=='221')) { |
| 77 | + this.router.navigate(['/error'], |
| 78 | + { queryParams: { error: ErrorMessages.NO_ACCESS }}); |
| 79 | + return false; |
| 80 | + } |
| 81 | + return true; |
| 82 | + }) |
| 83 | + ); |
74 | 84 | } |
75 | 85 | } |
0 commit comments