Skip to content

Commit 621bbf2

Browse files
committed
Check roles in Configuration guard
1 parent f235e57 commit 621bbf2

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

cloudapp/src/app/configuration/configuration.component.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Component, OnInit, Injectable } from '@angular/core';
22
import { AppService } from '../app.service';
33
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';
55
import { ToastrService } from 'ngx-toastr';
66
import { CanActivate, Router } from '@angular/router';
77
import { Observable } from 'rxjs';
8-
import { map } from 'rxjs/operators';
8+
import { map, flatMap } from 'rxjs/operators';
99
import { ErrorMessages } from '../static/error.component';
1010

1111
@Component({
@@ -58,18 +58,28 @@ export class ConfigurationComponent implements OnInit {
5858
providedIn: 'root',
5959
})
6060
export class ConfigurationGuard implements CanActivate {
61-
constructor(
61+
constructor (
6262
private eventsService: CloudAppEventsService,
63+
private restService: CloudAppRestService,
6364
private router: Router
6465
) {}
66+
6567
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+
);
7484
}
7585
}

0 commit comments

Comments
 (0)