2020-11-11 15:43:13 +01:00
|
|
|
import {map, filter, mergeMap, take} from 'rxjs/operators';
|
2019-09-13 09:01:19 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-02-26 14:57:04 +01:00
|
|
|
import {
|
|
|
|
Router,
|
|
|
|
CanActivate,
|
|
|
|
ActivatedRouteSnapshot,
|
|
|
|
RouterStateSnapshot,
|
2019-06-03 15:20:36 +02:00
|
|
|
CanLoad, Route, UrlSegment
|
2019-02-26 14:57:04 +01:00
|
|
|
} from '@angular/router';
|
2020-07-17 01:08:10 +02:00
|
|
|
import {Observable, of, Subscription} from 'rxjs';
|
2018-03-15 10:53:07 +01:00
|
|
|
import {Session} from '../../login/utils/helper.class';
|
2018-11-01 18:20:05 +01:00
|
|
|
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
|
2018-03-15 10:53:07 +01:00
|
|
|
import {CommunityService} from '../community/community.service';
|
2019-09-13 09:01:19 +02:00
|
|
|
import {EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
|
2019-02-27 11:39:13 +01:00
|
|
|
import {ConnectHelper} from '../connectHelper';
|
2019-09-05 16:02:18 +02:00
|
|
|
import {StringUtils} from '../../utils/string-utils.class';
|
2019-09-13 09:01:19 +02:00
|
|
|
import {UserManagementService} from "../../services/user-management.service";
|
2020-07-17 01:08:10 +02:00
|
|
|
import {properties} from "../../../../environments/environment";
|
2018-03-15 10:53:07 +01:00
|
|
|
|
|
|
|
@Injectable()
|
2019-02-27 11:39:13 +01:00
|
|
|
export class ConnectAdminLoginGuard implements CanActivate, CanLoad {
|
2020-07-17 01:08:10 +02:00
|
|
|
sub: Subscription = null;
|
|
|
|
|
2019-02-26 14:57:04 +01:00
|
|
|
constructor(private router: Router,
|
|
|
|
private communityService: CommunityService,
|
2019-09-13 09:01:19 +02:00
|
|
|
private propertiesService: EnvironmentSpecificService,
|
|
|
|
private userManagementService: UserManagementService) {
|
|
|
|
}
|
2018-03-15 10:53:07 +01:00
|
|
|
|
2019-02-27 11:39:13 +01:00
|
|
|
check(community: string, path: string): Observable<boolean> | boolean {
|
2019-02-25 16:28:14 +01:00
|
|
|
let errorCode = LoginErrorCodes.NOT_LOGIN;
|
2019-09-13 09:01:19 +02:00
|
|
|
let email = null;
|
2020-11-11 15:43:13 +01:00
|
|
|
const authorized = this.userManagementService.getUserInfo(false).pipe(take(1),map(user => {
|
2019-09-13 09:01:19 +02:00
|
|
|
if (user) {
|
|
|
|
email = user.email;
|
2020-08-06 22:01:37 +02:00
|
|
|
if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user) || Session.isManager('community', community, user)) {
|
2019-09-13 09:01:19 +02:00
|
|
|
return of(true);
|
|
|
|
} else {
|
2019-09-13 11:38:21 +02:00
|
|
|
errorCode = LoginErrorCodes.NOT_ADMIN;
|
2020-07-17 01:08:10 +02:00
|
|
|
return this.communityService.isCommunityManagerByState(properties, properties['communityAPI'] + community,
|
2020-11-11 15:43:13 +01:00
|
|
|
email).pipe(take(1));
|
2019-09-13 09:01:19 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return of(false);
|
|
|
|
}
|
|
|
|
}), mergeMap( authorized => {
|
|
|
|
return authorized;
|
|
|
|
}));
|
2020-07-17 01:08:10 +02:00
|
|
|
//}));
|
|
|
|
this.sub = authorized.pipe(filter(authorized => !authorized)).subscribe(() => {
|
2019-09-13 09:01:19 +02:00
|
|
|
this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
'errorCode': errorCode,
|
|
|
|
'redirectUrl': path
|
|
|
|
}
|
|
|
|
})});
|
|
|
|
return authorized;
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
2018-03-15 10:53:07 +01:00
|
|
|
|
2019-02-26 14:57:04 +01:00
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
2019-02-27 11:39:13 +01:00
|
|
|
return this.check(route.queryParams['communityId'], state.url);
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
2018-03-27 10:22:55 +02:00
|
|
|
|
2019-06-03 15:20:36 +02:00
|
|
|
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
|
2019-09-05 16:02:18 +02:00
|
|
|
const path = StringUtils.URLSegmentsToPath(segments) + document.location.search;
|
2019-02-27 11:39:13 +01:00
|
|
|
return this.check(ConnectHelper.getCommunityFromPath(path), path);
|
2018-03-15 10:53:07 +01:00
|
|
|
}
|
2020-07-17 01:08:10 +02:00
|
|
|
|
|
|
|
canDeactivate() {
|
|
|
|
if(this.sub) {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-15 10:53:07 +01:00
|
|
|
}
|