[Library | Trunk]: Fix parse roles for userinfo

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@59250 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2020-08-11 12:54:14 +00:00
parent 99198fb0d9
commit c1977b1404
3 changed files with 26 additions and 30 deletions

View File

@ -31,7 +31,7 @@ export class ConnectSubscriberGuard implements CanActivate {
if (communityDomain) { if (communityDomain) {
community = communityDomain; community = communityDomain;
} }
if (Session.isSubscriber('community', community, user)) { if (Session.isSubscribedTo('community', community, user)) {
return of(true); return of(true);
} }
return this.subscribeService.isSubscribedToCommunity(properties, community) return this.subscribeService.isSubscribedToCommunity(properties, community)

View File

@ -80,8 +80,8 @@ export class Session{
user.role.indexOf('USER_MANAGER') !== -1); user.role.indexOf('USER_MANAGER') !== -1);
} }
public static isSubscriber(type: string, id: string, user: User): boolean { public static isSubscribedTo(type: string, id: string, user: User): boolean {
return user && user.role.indexOf(type.toUpperCase() + '_' + id.toUpperCase()) !== -1 return user && user.role.indexOf(type.toUpperCase() + '_' + id.toUpperCase()) !== -1;
} }
public static isManager(type: string, id: string, user: User): boolean { public static isManager(type: string, id: string, user: User): boolean {

View File

@ -5,7 +5,6 @@ import {COOKIE, User} from "../login/utils/helper.class";
import {catchError, map, timeout} from "rxjs/operators"; import {catchError, map, timeout} from "rxjs/operators";
import {NavigationEnd, Router} from "@angular/router"; import {NavigationEnd, Router} from "@angular/router";
import {properties} from "../../../environments/environment"; import {properties} from "../../../environments/environment";
import {CustomOptions} from "./servicesUtils/customOptions.class";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -18,20 +17,7 @@ export class UserManagementService {
constructor(private http: HttpClient, constructor(private http: HttpClient,
private router: Router) { private router: Router) {
this.promise = new Promise<any>((resolve => { this.promise = new Promise<any>((resolve => {
const token = COOKIE.getCookie('AccessToken'); this.updateUserInfo(resolve);
if (!token) {
this.getUserInfoSubject.next(null);
resolve();
} else {
this.http.get<User>(properties.userInfoUrl + token).pipe(map(userInfo => {
return this.parseUserInfo(userInfo);
})).pipe(timeout(3000), catchError(() => {
return of(null);
})).subscribe(user => {
this.getUserInfoSubject.next(user);
resolve();
});
}
})); }));
this.router.events.subscribe(event => { this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) { if (event instanceof NavigationEnd) {
@ -52,16 +38,24 @@ export class UserManagementService {
} }
public updateUserInfo(resolve: Function = null) { public updateUserInfo(resolve: Function = null) {
this.http.get<User>(properties.userInfoUrl, CustomOptions.registryOptions()).pipe(map(userInfo => { const token = COOKIE.getCookie('AccessToken');
return this.parseUserInfo(userInfo); if (!token) {
})).pipe(timeout(3000), catchError(() => { this.getUserInfoSubject.next(null);
return of(null); if(resolve) {
})).subscribe(user => {
this.getUserInfoSubject.next(user);
if (resolve) {
resolve(); resolve();
} }
}); } else {
this.http.get<User>(properties.userInfoUrl + token).pipe(map(userInfo => {
return this.parseUserInfo(userInfo);
})).pipe(timeout(3000), catchError(() => {
return of(null);
})).subscribe(user => {
this.getUserInfoSubject.next(user);
if(resolve) {
resolve();
}
});
}
} }
private async getUserInfoAsync(): Promise<User> { private async getUserInfoAsync(): Promise<User> {
@ -87,12 +81,14 @@ export class UserManagementService {
user.fullname = "Anonymous user"; user.fullname = "Anonymous user";
} }
} }
user.role = [];
if (info.edu_person_entitlements) { if (info.edu_person_entitlements) {
user.role = info.edu_person_entitlements; user.role = info.edu_person_entitlements;
} else if (info.roles) { }
user.role = info.roles; if (info.roles) {
} else { info.roles.forEach(role => {
user.role = []; user.role.push(role);
});
} }
user.expirationDate = info.exp_date; user.expirationDate = info.exp_date;
return user; return user;