2021-03-22 18:00:49 +01:00
|
|
|
import {filter, map, 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 {
|
|
|
|
ActivatedRouteSnapshot,
|
2021-03-22 18:00:49 +01:00
|
|
|
CanActivate,
|
|
|
|
CanActivateChild,
|
|
|
|
Router,
|
2019-02-26 14:57:04 +01:00
|
|
|
RouterStateSnapshot,
|
2021-03-22 18:00:49 +01:00
|
|
|
UrlTree
|
2019-02-26 14:57:04 +01:00
|
|
|
} from '@angular/router';
|
2021-03-22 18:00:49 +01:00
|
|
|
import {Observable, of} 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';
|
2019-09-13 09:01:19 +02:00
|
|
|
import {UserManagementService} from "../../services/user-management.service";
|
2018-03-15 10:53:07 +01:00
|
|
|
|
|
|
|
@Injectable()
|
2021-03-01 19:44:26 +01:00
|
|
|
export class ConnectAdminLoginGuard implements CanActivate, CanActivateChild {
|
2021-02-19 18:50:34 +01:00
|
|
|
|
2019-02-26 14:57:04 +01:00
|
|
|
constructor(private router: Router,
|
2019-09-13 09:01:19 +02:00
|
|
|
private userManagementService: UserManagementService) {
|
|
|
|
}
|
2021-02-19 18:50:34 +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;
|
2021-02-19 18:50:34 +01:00
|
|
|
const authorized = this.userManagementService.getUserInfo(false).pipe(take(1), map(user => {
|
|
|
|
if (user) {
|
|
|
|
if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user) || Session.isManager('community', community, user)) {
|
|
|
|
return of(true);
|
2019-09-13 09:01:19 +02:00
|
|
|
}
|
2021-02-19 18:50:34 +01:00
|
|
|
}
|
|
|
|
return of(false);
|
|
|
|
}), mergeMap(authorized => {
|
|
|
|
return authorized;
|
|
|
|
}));
|
|
|
|
authorized.pipe(filter(authorized => !authorized)).subscribe(() => {
|
2019-09-13 09:01:19 +02:00
|
|
|
this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
'errorCode': errorCode,
|
|
|
|
'redirectUrl': path
|
|
|
|
}
|
2021-02-19 18:50:34 +01:00
|
|
|
})
|
|
|
|
});
|
2019-09-13 09:01:19 +02:00
|
|
|
return authorized;
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
2021-02-19 18:50:34 +01:00
|
|
|
|
2021-07-14 13:19:57 +02:00
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
2021-03-01 19:44:26 +01:00
|
|
|
return this.check(route.params['community'], state.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
|
|
|
return this.check(childRoute.params['community'], state.url);
|
2019-02-26 14:57:04 +01:00
|
|
|
}
|
2018-03-15 10:53:07 +01:00
|
|
|
}
|