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 <dengq@vmware.com>
This commit is contained in:
Qian Deng 2018-11-01 11:26:50 +08:00
parent e37e275a77
commit 558638f912
4 changed files with 26 additions and 75 deletions

View File

@ -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);

View File

@ -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<boolean> {
return new Promise<boolean>((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);
});
});
});
}

View File

@ -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> | 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> | boolean {
return this.canActivate(route, state);
}
}

View File

@ -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<SessionUser> {
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));
}