[Library | Trunk]: 1. Community Service refactor. 2. Subscribe service refactor. 3. Add service on role-verification
git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@60453 d315682c-612b-4755-9ff5-7f18f6832af3deprecated-master
parent
ce59d4e8bf
commit
e8774312f5
@ -1,67 +0,0 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
||||
import {Observable} from 'rxjs/Observable';
|
||||
import {Session} from './utils/helper.class';
|
||||
import {LoginErrorCodes} from './utils/guardHelper.class';
|
||||
import {catchError, filter, map, mergeMap} from "rxjs/operators";
|
||||
import {UserManagementService} from "../services/user-management.service";
|
||||
import {UserRegistryService} from "../services/user-registry.service";
|
||||
import {of} from "rxjs";
|
||||
import {properties} from "../../../environments/environment";
|
||||
import {ConnectHelper} from "../connect/connectHelper";
|
||||
|
||||
@Injectable()
|
||||
export class VerificationGuard implements CanActivate {
|
||||
|
||||
constructor(private router: Router,
|
||||
private userRegistryService: UserRegistryService,
|
||||
private userManagementService: UserManagementService) {
|
||||
}
|
||||
|
||||
check(path: string, id: string, type: string, entity: string): Observable<boolean> | boolean {
|
||||
if (Session.isLoggedIn()) {
|
||||
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
||||
if(user) {
|
||||
if(id) {
|
||||
return this.userRegistryService.getInvitation(id).pipe(map(invitation => {
|
||||
if(invitation.type !== type || invitation.entity !== entity) {
|
||||
this.router.navigate(['/error'], {queryParams: {'page': path}});
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}), catchError(error => {
|
||||
if(error.status === 404) {
|
||||
this.router.navigate(['/error'], {queryParams: {'page': path}});
|
||||
} else {
|
||||
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_AUTHORIZED, 'redirectUrl': path}});
|
||||
}
|
||||
return of(false);
|
||||
}));
|
||||
} else {
|
||||
this.router.navigate(['/error'], {queryParams: {'page': path}});
|
||||
return of(false);
|
||||
}
|
||||
} else {
|
||||
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, 'redirectUrl': path}});
|
||||
return of(false);
|
||||
}
|
||||
}), mergeMap(authorized => {
|
||||
return authorized;
|
||||
}));
|
||||
} else {
|
||||
this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN,'redirectUrl': path}});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
||||
if(properties.dashboard === "connect" && properties.environment === 'development') {
|
||||
let communityId = ConnectHelper.getCommunityFromDomain(properties.domain);
|
||||
communityId = (communityId)?communityId:route.queryParams['communityId'];
|
||||
return this.check(state.url, route.params.id, 'community', communityId);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,90 +1,24 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {map, tap} from "rxjs/operators";
|
||||
import {CustomOptions} from "../../services/servicesUtils/customOptions.class";
|
||||
import {EnvProperties} from "../properties/env-properties";
|
||||
import {BehaviorSubject, Observable, Subscriber} from "rxjs";
|
||||
import {BehaviorSubject, Observable} from "rxjs";
|
||||
|
||||
@Injectable()
|
||||
export class SubscribeService {
|
||||
private isSubscribedSubject: BehaviorSubject<boolean> = new BehaviorSubject(false);
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
sub;
|
||||
ngOnDestroy() {
|
||||
this.clearSubscriptions();
|
||||
}
|
||||
clearSubscriptions(){
|
||||
if (this.sub instanceof Subscriber) {
|
||||
this.sub.unsubscribe();
|
||||
}
|
||||
}
|
||||
public initIsSubscribedToCommunity(properties: EnvProperties, pid: string) {
|
||||
let url = properties.adminToolsAPIURL + "/community/" + pid + "/is-subscriber/";
|
||||
this.sub = this.http.get<boolean>(url, CustomOptions.getAuthOptionsWithBody()).subscribe((isSubscribed) => {
|
||||
this.isSubscribedSubject.next(isSubscribed);
|
||||
}, error => {
|
||||
this.isSubscribedSubject.error(error);
|
||||
});
|
||||
}
|
||||
|
||||
public get isSubscribed(): Observable<boolean> {
|
||||
return this.isSubscribedSubject.asObservable();
|
||||
}
|
||||
|
||||
getCommunitySubscribers(properties: EnvProperties, pid: string) {
|
||||
let url = properties.adminToolsAPIURL + "/" + properties.adminToolsPortalType + "/" + pid + "/subscribers";
|
||||
return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
|
||||
}
|
||||
|
||||
getNumberOfCommunitySubscribers(properties: EnvProperties, pid: string) {
|
||||
let url = properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType +"/" + pid + "/subscribers/count";
|
||||
return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
|
||||
}
|
||||
|
||||
isSubscribedToCommunity(properties: EnvProperties, pid: string) {
|
||||
let url = properties.adminToolsAPIURL + "/community/" + pid + "/is-subscriber/";
|
||||
return this.http.get<boolean>(url, CustomOptions.getAuthOptionsWithBody())
|
||||
.pipe(map(res => {
|
||||
// if (res['status'] && res['status'] != 200) {
|
||||
// return null;
|
||||
// }
|
||||
// if (res['subscribers'] && res['subscribers'] != null) {
|
||||
//
|
||||
// for (var i = 0; i < res['subscribers'].length; i++) {
|
||||
// if (res['subscribers'][i] != null && res['subscribers'][i].email == email) {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return false;
|
||||
return res;
|
||||
}));
|
||||
}
|
||||
|
||||
subscribeToCommunity(properties: EnvProperties, pid: string) {
|
||||
return this.http.post<any>(properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType +"/" + pid + "/subscriber", {}, CustomOptions.getAuthOptionsWithBody())
|
||||
.pipe(tap(isSubscribed => {
|
||||
this.isSubscribedSubject.next(isSubscribed);}));
|
||||
}
|
||||
|
||||
unSubscribeToCommunity(properties: EnvProperties, pid: string) {
|
||||
return this.http.post<any>(properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType+"/" + pid + "/subscriber/delete", {}, CustomOptions.getAuthOptionsWithBody())
|
||||
.pipe(tap(unSubscribed => {
|
||||
this.isSubscribedSubject.next(!unSubscribed);}));
|
||||
}
|
||||
|
||||
subscribeToCommunityByEmail(properties: EnvProperties, pid: string, email: string) {
|
||||
let subscriber = {"email": email};
|
||||
return this.http.post<any>(properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType+"/" + pid + "/subscribers", JSON.stringify(subscriber), CustomOptions.getAuthOptionsWithBody());
|
||||
}
|
||||
|
||||
unSubscribeToCommunityByEmail(properties: EnvProperties, pid: string, email: string) {
|
||||
return this.http.post<any>(properties.adminToolsAPIURL + "/"+ properties.adminToolsPortalType+"/" + pid + "/subscribers/delete", JSON.stringify([email]), CustomOptions.getAuthOptionsWithBody());
|
||||
}
|
||||
|
||||
getCommunitiesSubscribedTo(properties: EnvProperties/*, email: string*/) {
|
||||
let url = properties.adminToolsAPIURL + "/subscriber/communities";//?email=" + email;
|
||||
return this.http.get<any>(url, CustomOptions.getAuthOptionsWithBody());
|
||||
}
|
||||
|
||||
private loading: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
|
||||
private members: BehaviorSubject<number> = new BehaviorSubject<number>(0);
|
||||
|
||||
public setLoading(loading: boolean) {
|
||||
this.loading.next(loading);
|
||||
}
|
||||
|
||||
public setMembers(members: number) {
|
||||
this.members.next(members);
|
||||
}
|
||||
|
||||
public getLoading(): Observable<any> {
|
||||
return this.loading.asObservable();
|
||||
}
|
||||
|
||||
public getMembers(): Observable<any> {
|
||||
return this.members.asObservable();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue