Improve auth check guard service (#17605)

Signed-off-by: AllForNothing <sshijun@vmware.com>

Signed-off-by: AllForNothing <sshijun@vmware.com>
This commit is contained in:
Shijun Sun 2022-09-28 17:05:25 +08:00 committed by GitHub
parent 90fe39684b
commit 303133f695
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,11 +22,11 @@ import {
} from '@angular/router';
import { SessionService } from '../services/session.service';
import { AppConfigService } from '../../services/app-config.service';
import { maintainUrlQueryParmas } from '../units/shared.utils';
import { MessageHandlerService } from '../services/message-handler.service';
import { SearchTriggerService } from '../components/global-search/search-trigger.service';
import { Observable } from 'rxjs';
import { AdmiralQueryParamKey, CommonRoutes } from '../entities/shared.const';
import { CommonRoutes } from '../entities/shared.const';
import { UN_LOGGED_PARAM, YES } from '../../account/sign-in/sign-in.service';
@Injectable({
providedIn: 'root',
@ -40,19 +40,6 @@ export class AuthCheckGuard implements CanActivate, CanActivateChild {
private searchTrigger: SearchTriggerService
) {}
isGuest(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): boolean {
const proRegExp = /\/harbor\/projects\/[\d]+\/.+/i;
const libRegExp = /\/harbor\/tags\/[\d]+\/.+/i;
if (proRegExp.test(state.url) || libRegExp.test(state.url)) {
return true;
}
return false;
}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
@ -68,25 +55,10 @@ export class AuthCheckGuard implements CanActivate, CanActivateChild {
this.searchTrigger.closeSearch(true);
return new Observable(observer => {
let queryParams = route.queryParams;
if (queryParams) {
if (queryParams[AdmiralQueryParamKey]) {
this.appConfigService.saveAdmiralEndpoint(
queryParams[AdmiralQueryParamKey]
);
// Remove the query parameter key pair and redirect
let keyRemovedUrl = maintainUrlQueryParmas(
state.url,
AdmiralQueryParamKey,
undefined
);
if (!/[?]{1}.+/i.test(keyRemovedUrl)) {
keyRemovedUrl = keyRemovedUrl.replace('?', '');
}
this.router.navigateByUrl(keyRemovedUrl);
return observer.next(false);
}
// if the url has the queryParam `publicAndNotLogged=yes`, then skip auth check
const queryParams = route.queryParams;
if (queryParams && queryParams[UN_LOGGED_PARAM] === YES) {
return observer.next(true);
}
let user = this.authService.getCurrentUser();
if (!user) {
@ -95,13 +67,9 @@ export class AuthCheckGuard implements CanActivate, CanActivateChild {
return observer.next(true);
},
error => {
// If is guest, skip it
if (this.isGuest(route, state)) {
return observer.next(true);
}
// Session retrieving failed then redirect to sign-in
// no matter what status code is.
// Please pay attention that router-guard 'HARBOR_ROOT' and 'EMBEDDED_SIGN_IN' support anonymous user
// no need to check auth for `sign in` page
if (
!state.url.startsWith(CommonRoutes.EMBEDDED_SIGN_IN)
) {