[Library | Trunk]: Change getUserInfo to be suitable for guards too.
git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@57939 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
parent
66375c1a1d
commit
eee4f0807e
|
@ -59,7 +59,7 @@ export class ClaimsAdminComponent {
|
|||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.userManagementService.getUserInfo(this.userInfoURL).subscribe(user => {
|
||||
this.userManagementService.getUserInfo().subscribe(user => {
|
||||
this.user = user;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -95,7 +95,7 @@ export class ClaimInsertComponent {
|
|||
this.errorInClaims = [];
|
||||
this.insertedRecords = [];
|
||||
this.errorInRecords = [];
|
||||
this.userManagementService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
|
||||
this.userManagementService.getUserInfo().subscribe(user => {
|
||||
if (!user) {
|
||||
localStorage.setItem(this.localStoragePrefix + "results", JSON.stringify(this.results));
|
||||
if (this.sources != null) {
|
||||
|
|
|
@ -41,7 +41,7 @@ export class MyClaimsComponent {
|
|||
constructor(private userManagementService: UserManagementService) {}
|
||||
|
||||
ngOnInit() {
|
||||
this.userManagementService.getUserInfo(this.userInfoURL).subscribe(user => {
|
||||
this.userManagementService.getUserInfo().subscribe(user => {
|
||||
this.user = user;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ export class ConnectAdminLoginGuard implements CanActivate, CanLoad {
|
|||
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
||||
let email = null;
|
||||
const authorized = this.propertiesService.subscribeEnvironment().pipe(map(res => res), mergeMap(properties => {
|
||||
return this.userManagementService.getUserInfo(properties.userInfoUrl).pipe(map(user => {
|
||||
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
||||
if (user) {
|
||||
email = user.email;
|
||||
if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user)) {
|
||||
|
|
|
@ -21,7 +21,7 @@ export class ConnectSubscriberGuard implements CanActivate {
|
|||
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
||||
let email = null;
|
||||
const subscribed = this.propertiesService.subscribeEnvironment().pipe(map(res => res), mergeMap(properties => {
|
||||
return this.userManagementService.getUserInfo(properties.userInfoUrl).pipe(map(user => {
|
||||
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
||||
if (user) {
|
||||
errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
|
||||
email = user.email;
|
||||
|
|
|
@ -19,7 +19,7 @@ export class IsCommunityOrAdmin implements CanActivate {
|
|||
return true;
|
||||
} else {
|
||||
const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
|
||||
return this.userManagementService.getUserInfo(properties['userInfoUrl']).pipe(map(user => {
|
||||
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
||||
return Session.isPortalAdministrator(user);
|
||||
}));
|
||||
}));
|
||||
|
|
|
@ -71,7 +71,7 @@ export class EntitiesComponent implements OnInit {
|
|||
|
||||
this.route.queryParams.subscribe(params => {
|
||||
HelperFunctions.scroll();
|
||||
this.userManagementService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
|
||||
this.userManagementService.getUserInfo().subscribe(user => {
|
||||
this.selectedCommunityPid = params['communityId'];
|
||||
this.applyCommunityFilter(this.selectedCommunityPid);
|
||||
this.isPortalAdministrator = Session.isPortalAdministrator(user) && !this.selectedCommunityPid;
|
||||
|
|
|
@ -105,7 +105,7 @@ export class PagesComponent implements OnInit {
|
|||
}
|
||||
|
||||
this.keyword = '';
|
||||
this.userManagementService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
|
||||
this.userManagementService.getUserInfo().subscribe(user => {
|
||||
this.selectedCommunityPid = params['communityId'];
|
||||
this.applyCommunityFilter(this.selectedCommunityPid);
|
||||
this.isPortalAdministrator = Session.isPortalAdministrator(user) && !this.selectedCommunityPid;
|
||||
|
|
|
@ -26,7 +26,7 @@ export class AdminLoginGuard implements CanActivate{
|
|||
check(path: string): Observable<boolean> {
|
||||
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
||||
const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
|
||||
return this.userManagementService.getUserInfo(properties['userInfoUrl']).pipe(map( user => {
|
||||
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
||||
if(user) {
|
||||
errorCode = LoginErrorCodes.NOT_ADMIN;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ export class ClaimsCuratorGuard implements CanActivate {
|
|||
check(path: string): Observable<boolean> |boolean {
|
||||
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
||||
const obs = this.propertiesService.subscribeEnvironment().pipe(mergeMap(properties => {
|
||||
return this.userManagementService.getUserInfo(properties['userInfoUrl']).pipe(map( user => {
|
||||
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
||||
if(user) {
|
||||
errorCode = LoginErrorCodes.NOT_ADMIN;
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ export class UserComponent {
|
|||
});
|
||||
if (typeof document !== 'undefined') {
|
||||
this.server = false;
|
||||
this.userManagementsService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
|
||||
this.userManagementsService.getUserInfo().subscribe(user => {
|
||||
this.user = user;
|
||||
this.loggedIn = !!this.user;
|
||||
this.errorMessage = "";
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import {Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {BehaviorSubject, Observable, of} from "rxjs";
|
||||
import {BehaviorSubject, from, Observable, of} from "rxjs";
|
||||
import {COOKIE, User} from "../login/utils/helper.class";
|
||||
import {catchError, map, timeout} from "rxjs/operators";
|
||||
import {NavigationEnd, Router} from "@angular/router";
|
||||
import {EnvironmentSpecificService} from "../utils/properties/environment-specific.service";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
|
@ -11,10 +12,29 @@ import {NavigationEnd, Router} from "@angular/router";
|
|||
export class UserManagementService {
|
||||
|
||||
private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null);
|
||||
private lock: boolean = false;
|
||||
private readonly promise: Promise<User>;
|
||||
|
||||
constructor(private http: HttpClient,
|
||||
private router: Router) {
|
||||
private router: Router,
|
||||
private environmentSpecificService: EnvironmentSpecificService) {
|
||||
this.promise = new Promise<any>((resolve => {
|
||||
this.environmentSpecificService.subscribeEnvironment().subscribe(properties => {
|
||||
const token = COOKIE.getCookie('AccessToken');
|
||||
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 => {
|
||||
if (event instanceof NavigationEnd) {
|
||||
const token = COOKIE.getCookie('AccessToken');
|
||||
|
@ -25,32 +45,22 @@ export class UserManagementService {
|
|||
})
|
||||
}
|
||||
|
||||
public getUserInfo(url: string): Observable<User> {
|
||||
const token = COOKIE.getCookie('AccessToken');
|
||||
if (!token) {
|
||||
this.getUserInfoSubject.next(null);
|
||||
} else {
|
||||
if (this.getUserInfoSubject.getValue() === null && !this.lock) {
|
||||
this.lock = true;
|
||||
this.http.get<User>(url + token).pipe(map(userInfo => {
|
||||
return this.parseUserInfo(userInfo);
|
||||
})).pipe(timeout(3000), catchError(() => {
|
||||
return of(null);
|
||||
})).subscribe(user => {
|
||||
if (this.getUserInfoSubject.getValue() === null) {
|
||||
this.getUserInfoSubject.next(user);
|
||||
this.lock = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
public getUserInfo(subject: boolean = true): Observable<User> {
|
||||
if(subject) {
|
||||
return this.getUserInfoSubject.asObservable();
|
||||
} else {
|
||||
return from(this.getUserInfoAsync());
|
||||
}
|
||||
}
|
||||
|
||||
private async getUserInfoAsync(): Promise<User> {
|
||||
await this.promise;
|
||||
return this.getUserInfoSubject.getValue();
|
||||
}
|
||||
|
||||
private parseUserInfo(info: any) {
|
||||
const user: User = new User();
|
||||
user.id = (info.sub && info.sub.indexOf('@')) ? info.sub.substring(0, info.sub.indexOf('@')) : info.sub;
|
||||
|
||||
user.firstname = (info.given_name) ? info.given_name : "";
|
||||
user.lastname = (info.family_name) ? info.family_name : "";
|
||||
user.email = info.email;
|
||||
|
|
Loading…
Reference in New Issue