[Library]: Fortgot to add service on svn.

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@57059 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2019-09-13 07:03:07 +00:00
parent 98c98e6625
commit 69c86456b8
1 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,57 @@
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable, of} from "rxjs";
import {COOKIE, User} from "../login/utils/helper.class";
import {catchError, map} from "rxjs/operators";
@Injectable({
providedIn: 'root'
})
export class UserManagementService {
constructor(private http: HttpClient) {
}
public isLoggedIn(url: string): Observable<boolean> {
return this.getUserInfo(url).pipe(map(user => {
return user !== null;
}));
}
public getUserInfo(url: string): Observable<User> {
const token = COOKIE.getCookie('AccessToken');
return this.http.get<User>(url + token).pipe(map(userInfo => {
return this.parseUserInfo(userInfo);
})).pipe(catchError(() => {
return of(null);
}));
}
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;
user.fullname = (info.name) ? info.name : "";
if (user.fullname == "") {
if (user.firstname != "") {
user.fullname += user.firstname;
}
if (user.lastname != "") {
user.fullname += user.lastname;
}
if (user.fullname == "") { //fullname is still empty set a default
user.fullname = "Anonymous user";
}
}
if (info.edu_person_entitlements) {
user.role = info.edu_person_entitlements;
} else {
user.role = [];
}
user.expirationDate = info.exp;
return user;
}
}