From 558638f9120fb52b42bd77765ff81c4e12fbebe3 Mon Sep 17 00:00:00 2001 From: Qian Deng Date: Thu, 1 Nov 2018 11:26:50 +0800 Subject: [PATCH] Fix: When session expire can not click anything This is casued that not handle 401 err on member routing guard Signed-off-by: Qian Deng --- .../message-handler.service.ts | 2 +- .../route/member-guard-activate.service.ts | 44 ++++++++-------- .../route/start-guard-activate.service.ts | 51 ------------------- src/portal/src/app/shared/session.service.ts | 4 +- 4 files changed, 26 insertions(+), 75 deletions(-) delete mode 100644 src/portal/src/app/shared/route/start-guard-activate.service.ts diff --git a/src/portal/src/app/shared/message-handler/message-handler.service.ts b/src/portal/src/app/shared/message-handler/message-handler.service.ts index 1414d3721..ed19f0875 100644 --- a/src/portal/src/app/shared/message-handler/message-handler.service.ts +++ b/src/portal/src/app/shared/message-handler/message-handler.service.ts @@ -44,7 +44,7 @@ export class MessageHandlerService implements ErrorHandler { let code = error.statusCode || error.status; if (code === httpStatusCode.Unauthorized) { this.msgService.announceAppLevelMessage(code, msg, AlertType.DANGER); - // Session is invalida now, clare session cache + // Session is invalid now, clare session cache this.session.clear(); } else { this.msgService.announceMessage(code, msg, AlertType.DANGER); diff --git a/src/portal/src/app/shared/route/member-guard-activate.service.ts b/src/portal/src/app/shared/route/member-guard-activate.service.ts index 6e6099f0f..9ff813ea6 100644 --- a/src/portal/src/app/shared/route/member-guard-activate.service.ts +++ b/src/portal/src/app/shared/route/member-guard-activate.service.ts @@ -35,9 +35,12 @@ export class MemberGuard implements CanActivate, CanActivateChild { return new Promise((resolve, reject) => { let user = this.sessionService.getCurrentUser(); if (user === null) { - this.sessionService.retrieveUser().then(currentUser => { - return resolve(this.checkMemberStatus(state.url, projectId)); - }).catch(err => resolve(true)); + this.sessionService.retrieveUser() + .then(() => resolve(this.checkMemberStatus(state.url, projectId))) + .catch(() => { + this.router.navigate([CommonRoutes.HARBOR_DEFAULT]); + resolve(false); + }); } else { return resolve(this.checkMemberStatus(state.url, projectId)); } @@ -47,25 +50,24 @@ export class MemberGuard implements CanActivate, CanActivateChild { checkMemberStatus(url: string, projectId: number): Promise { return new Promise((resolve, reject) => { this.projectService.checkProjectMember(projectId) - .subscribe( - res => { - this.sessionService.setProjectMembers(res); + .subscribe(res => { + this.sessionService.setProjectMembers(res); + return resolve(true); + }, + () => { + // Add exception for repository in project detail router activation. + this.projectService.getProject(projectId).subscribe(project => { + if (project.public === 1) { return resolve(true); - }, - error => { - // Add exception for repository in project detail router activation. - if (url.endsWith('repository')) { - return resolve(true); - } - this.projectService.getProject(projectId) - .subscribe(project => { - if (project.public === 1) { - return resolve(true); - } - this.router.navigate([CommonRoutes.HARBOR_DEFAULT]); - return resolve(false); - }); - }); + } + this.router.navigate([CommonRoutes.HARBOR_DEFAULT]); + return resolve(false); + }, + () => { + this.router.navigate([CommonRoutes.HARBOR_DEFAULT]); + return resolve(false); + }); + }); }); } diff --git a/src/portal/src/app/shared/route/start-guard-activate.service.ts b/src/portal/src/app/shared/route/start-guard-activate.service.ts deleted file mode 100644 index 5e48192af..000000000 --- a/src/portal/src/app/shared/route/start-guard-activate.service.ts +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright Project Harbor Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -import { Injectable } from '@angular/core'; -import { - CanActivate, Router, - ActivatedRouteSnapshot, - RouterStateSnapshot, - CanActivateChild -} from '@angular/router'; -import { SessionService } from '../../shared/session.service'; -import { CommonRoutes } from '../../shared/shared.const'; - -@Injectable() -export class StartGuard implements CanActivate, CanActivateChild { - constructor(private authService: SessionService, private router: Router) { } - - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise | boolean { - // Authenticated user should not see the start page now - return new Promise((resolve, reject) => { - let user = this.authService.getCurrentUser(); - if (!user) { - this.authService.retrieveUser() - .then(() => { - this.router.navigate([CommonRoutes.HARBOR_DEFAULT]); - return resolve(false); - }) - .catch(error => { - return resolve(true); - }); - } else { - this.router.navigate([CommonRoutes.HARBOR_DEFAULT]); - return resolve(false); - } - }); - } - - canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise | boolean { - return this.canActivate(route, state); - } -} diff --git a/src/portal/src/app/shared/session.service.ts b/src/portal/src/app/shared/session.service.ts index 7e9fe880f..c74e7e5b8 100644 --- a/src/portal/src/app/shared/session.service.ts +++ b/src/portal/src/app/shared/session.service.ts @@ -23,7 +23,7 @@ import { enLang } from '../shared/shared.const'; import {HTTP_FORM_OPTIONS, HTTP_JSON_OPTIONS, HTTP_GET_OPTIONS} from "./shared.utils"; const signInUrl = '/c/login'; -const currentUserEndpint = "/api/users/current"; +const currentUserEndpoint = "/api/users/current"; const signOffEndpoint = "/c/log_out"; const accountEndpoint = "/api/users/:id"; const langEndpoint = "/language"; @@ -84,7 +84,7 @@ export class SessionService { * @memberOf SessionService */ retrieveUser(): Promise { - return this.http.get(currentUserEndpint, HTTP_GET_OPTIONS).toPromise() + return this.http.get(currentUserEndpoint, HTTP_GET_OPTIONS).toPromise() .then(response => this.currentUser = response.json() as SessionUser) .catch(error => this.handleError(error)); }