Fix pull command issue for unlogged users (#14584)

Signed-off-by: AllForNothing <sshijun@vmware.com>
This commit is contained in:
Will Sun 2021-04-08 18:42:46 +08:00 committed by GitHub
parent 778ce4d336
commit d9052c8241
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 10 deletions

View File

@ -49,3 +49,5 @@ export class SignInService {
}
}
export const UN_LOGGED_PARAM: string = 'publicAndNotLogged';

View File

@ -68,6 +68,7 @@ import { errorHandler } from "../../../../../../../shared/units/shared.utils";
import { ConfirmationDialogComponent } from "../../../../../../../shared/components/confirmation-dialog";
import { ConfirmationMessage } from "../../../../../../global-confirmation-dialog/confirmation-message";
import { ConfirmationAcknowledgement } from "../../../../../../global-confirmation-dialog/confirmation-state-message";
import { UN_LOGGED_PARAM } from "../../../../../../../account/sign-in/sign-in.service";
export interface LabelState {
iconsShow: boolean;
@ -438,7 +439,8 @@ export class ArtifactListTabComponent implements OnInit, OnDestroy {
artifactPullCommands.forEach(artifactPullCommand => {
if (artifactPullCommand.type === artifact.type) {
artifact.pullCommand =
`${artifactPullCommand.pullCommand} ${this.registryUrl}/${this.projectName}/${this.repoName}@${artifact.digest}`;
`${artifactPullCommand.pullCommand} ${this.registryUrl ?
this.registryUrl : location.hostname}/${this.projectName}/${this.repoName}@${artifact.digest}`;
}
});
});
@ -834,8 +836,8 @@ export class ArtifactListTabComponent implements OnInit, OnDestroy {
goIntoArtifactSummaryPage(artifact: Artifact): void {
const relativeRouterLink: string[] = ['artifacts', artifact.digest];
if (this.activatedRoute.snapshot.queryParams['publicAndNotLogged'] === YES) {
this.router.navigate(relativeRouterLink , { relativeTo: this.activatedRoute, queryParams: {publicAndNotLogged: YES} });
if (this.activatedRoute.snapshot.queryParams[UN_LOGGED_PARAM] === YES) {
this.router.navigate(relativeRouterLink , { relativeTo: this.activatedRoute, queryParams: {[UN_LOGGED_PARAM]: YES} });
} else {
this.router.navigate(relativeRouterLink , { relativeTo: this.activatedRoute });
}
@ -957,8 +959,8 @@ export class ArtifactListTabComponent implements OnInit, OnDestroy {
depth = artifact.digest;
}
const linkUrl = ['harbor', 'projects', this.projectId, 'repositories', this.repoName, 'depth', depth];
if (this.activatedRoute.snapshot.queryParams['publicAndNotLogged'] === YES) {
this.router.navigate(linkUrl, {queryParams: {publicAndNotLogged: YES}});
if (this.activatedRoute.snapshot.queryParams[UN_LOGGED_PARAM] === YES) {
this.router.navigate(linkUrl, {queryParams: {[UN_LOGGED_PARAM]: YES}});
} else {
this.router.navigate(linkUrl);
}

View File

@ -329,7 +329,10 @@ export class ArtifactTagComponent implements OnInit, OnDestroy {
return this.appConfigService.getConfig().with_notary;
}
public get registryUrl(): string {
return this.systemInfo ? this.systemInfo.registry_url : '';
if (this.systemInfo && this.systemInfo.registry_url) {
return this.systemInfo.registry_url;
}
return location.hostname;
}
hasPullCommand(): boolean {
return this.artifactDetails

View File

@ -18,6 +18,8 @@ import { TranslateService } from '@ngx-translate/core';
import { Message } from './message';
import { MessageService } from './message.service';
import { CommonRoutes, dismissInterval, httpStatusCode } from "../../entities/shared.const";
import { delUrlParam } from "../../units/utils";
import { UN_LOGGED_PARAM } from "../../../account/sign-in/sign-in.service";
const YES: string = 'yes';
@Component({
@ -127,7 +129,9 @@ export class MessageComponent implements OnInit, OnDestroy {
}
signIn(): void {
this.router.navigate([ CommonRoutes.EMBEDDED_SIGN_IN ], {queryParams: {redirect_url: this.router.url}});
// remove queryParam UN_LOGGED_PARAM of redirect url
const url = delUrlParam(this.router.url, UN_LOGGED_PARAM);
this.router.navigate([ CommonRoutes.EMBEDDED_SIGN_IN ], {queryParams: {redirect_url: url}});
}
onClose() {
@ -138,6 +142,6 @@ export class MessageComponent implements OnInit, OnDestroy {
}
// if navigate from global search(un-logged users visit public project)
isFromGlobalSearch(): boolean {
return this.route.snapshot.queryParams['publicAndNotLogged'] === YES;
return this.route.snapshot.queryParams[UN_LOGGED_PARAM] === YES;
}
}

View File

@ -21,6 +21,7 @@ import { Repository } from '../../../../../ng-swagger-gen/models/repository';
import { SearchTriggerService } from '../global-search/search-trigger.service';
import {Subscription} from "rxjs";
import { SessionService } from "../../services/session.service";
import { UN_LOGGED_PARAM } from "../../../account/sign-in/sign-in.service";
const YES: string = 'yes';
@Component({
selector: 'list-repository-ro',
@ -76,7 +77,7 @@ export class ListRepositoryROComponent implements OnInit, OnDestroy {
if (this.sessionService.getCurrentUser()) {
this.router.navigate(linkUrl);
} else {// if not logged in and it's a public project, add param 'publicAndNotLogged'
this.router.navigate(linkUrl, {queryParams: {publicAndNotLogged: YES}});
this.router.navigate(linkUrl, {queryParams: {[UN_LOGGED_PARAM]: YES}});
}
}

View File

@ -1,4 +1,4 @@
import { isSameArrayValue, isSameObject } from "./utils";
import { delUrlParam, isSameArrayValue, isSameObject } from "./utils";
describe('functions in utils.ts should work', () => {
it('function isSameArrayValue() should work', () => {
@ -22,4 +22,12 @@ describe('functions in utils.ts should work', () => {
expect(isSameObject({a: {a: 1 , b: 2}, b: null}, {a: {b: 2, a: 1}})).toBeTruthy();
expect(isSameObject([1, 2, 3], [3 , 2, 1])).toBeFalsy();
});
it('function delUrlParam() should work', () => {
expect(delUrlParam).toBeTruthy();
expect(delUrlParam('http://test.com?param1=a&param2=b&param3=c', 'param2'))
.toEqual('http://test.com?param1=a&param3=c');
expect(delUrlParam('http://test.com', 'param2')).toEqual('http://test.com');
expect(delUrlParam('http://test.com?param2', 'param2')).toEqual('http://test.com');
});
});

View File

@ -723,3 +723,33 @@ export function isSameArrayValue(a: any, b: any): boolean {
}
return false;
}
/**
* delete specified param from target url
* @param url
* @param key
*/
export function delUrlParam(url: string, key: string): string {
if (url && url.indexOf('?') !== -1) {
const baseUrl: string = url.split('?')[0];
const query: string = url.split('?')[1];
if (query.indexOf(key) > -1) {
let obj = {};
let arr: any[] = query.split('&');
for (let i = 0; i < arr.length; i++) {
arr[i] = arr[i].split('=');
obj[arr[i][0]] = arr[i][1];
}
delete obj[key];
if (!Object.keys(obj) || !Object.keys(obj).length) {
return baseUrl;
}
return baseUrl + '?' +
JSON.stringify(obj)
.replace(/[\"\{\}]/g, '')
.replace(/\:/g, '=')
.replace(/\,/g, '&');
}
}
return url;
}