diff --git a/app/common/PackageUtil.ts b/app/common/PackageUtil.ts index b74587c49..c3e146e4f 100644 --- a/app/common/PackageUtil.ts +++ b/app/common/PackageUtil.ts @@ -27,6 +27,10 @@ export function cleanUserPrefix(username: string): string { return username.replace(/^.*:/, ''); } +export function getPrefixedName(prefix: string, username: string): string { + return prefix ? `${prefix}${username}` : username; +} + export async function calculateIntegrity(contentOrFile: Uint8Array | string) { let integrityObj; if (typeof contentOrFile === 'string') { diff --git a/app/core/service/UserService.ts b/app/core/service/UserService.ts index 9e1320bca..1d5df1514 100644 --- a/app/core/service/UserService.ts +++ b/app/core/service/UserService.ts @@ -13,6 +13,8 @@ import { LoginResultCode } from '../../common/enum/User'; import { integrity, checkIntegrity, randomToken, sha512 } from '../../common/UserUtil'; import { AbstractService } from '../../common/AbstractService'; import { RegistryManagerService } from './RegistryManagerService'; +import { getPrefixedName } from '../../common/PackageUtil'; +import { Registry } from '../entity/Registry'; type Optional = Omit < T, K > & Partial ; @@ -75,16 +77,19 @@ export class UserService extends AbstractService { } const selfRegistry = await this.registryManagerService.ensureSelfRegistry(); - const defaultRegistry = await this.registryManagerService.ensureDefaultRegistry(); + const selfUser = await this.findUserByName(getPrefixedName(selfRegistry.userPrefix, name)); + if (selfUser) { + return selfUser; + } - const selfUser = await this.findUserByName(`${selfRegistry.name}:${name}`); - const defaultUser = await this.findUserByName(`${defaultRegistry.name}:${name}`); + const defaultRegistry = await this.registryManagerService.ensureDefaultRegistry(); + const defaultUser = await this.findUserByName(getPrefixedName(defaultRegistry.userPrefix, name)); - if (selfUser && defaultUser) { - throw new ForbiddenError(`${name} is ambiguous, please use ${selfUser.name} or ${defaultUser.name}}`); - } + return defaultUser; + } - return selfUser || defaultUser; + async findInRegistry(registry:Registry, name: string): Promise { + return await this.findUserByName(getPrefixedName(registry.userPrefix, name)); } async findUserByName(name: string): Promise { diff --git a/test/port/controller/UserController/showUser.test.ts b/test/port/controller/UserController/showUser.test.ts index 245ce502a..e30e272c5 100644 --- a/test/port/controller/UserController/showUser.test.ts +++ b/test/port/controller/UserController/showUser.test.ts @@ -17,6 +17,31 @@ describe('test/port/controller/UserController/showUser.test.ts', () => { }); }); + it('should clean prefix info', async () => { + const { authorization, email } = await TestUtil.createUser({ name: 'npm:banana' }); + const res = await app.httpRequest() + .get(`/-/user/org.couchdb.user:npm:banana`) + .set('authorization', authorization) + .expect(200); + assert.deepEqual(res.body, { + _id: `org.couchdb.user:banana`, + name: 'banana', + email, + }); + }); + + it('should return self user first', async () => { + const { authorization, email } = await TestUtil.createUser({ name: 'npm:apple' }); + await TestUtil.createUser({ name: 'apple' }); + const res = await app.httpRequest() + .get(`/-/user/org.couchdb.user:apple`) + .set('authorization', authorization) + .expect(200); + + assert.equal(res.body.email, email); + }); + + it('should 404 when user not exists', async () => { const { authorization, name } = await TestUtil.createUser(); let res = await app.httpRequest()