Skip to content

Commit

Permalink
feat: adaptive username
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed Jun 28, 2023
1 parent 893626c commit 18a355b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
4 changes: 4 additions & 0 deletions app/common/PackageUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
19 changes: 12 additions & 7 deletions app/core/service/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, K extends keyof T> = Omit < T, K > & Partial<T> ;

Expand Down Expand Up @@ -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<UserEntity | null> {
return await this.findUserByName(getPrefixedName(registry.userPrefix, name));
}

async findUserByName(name: string): Promise<UserEntity | null> {
Expand Down
25 changes: 25 additions & 0 deletions test/port/controller/UserController/showUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Check failure on line 23 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (16, ubuntu-latest)

Strings must use singlequote

Check failure on line 23 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)

Strings must use singlequote

Check failure on line 23 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)

Strings must use singlequote

Check failure on line 23 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (16, ubuntu-latest)

Strings must use singlequote

Check failure on line 23 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)

Strings must use singlequote

Check failure on line 23 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)

Strings must use singlequote
.set('authorization', authorization)
.expect(200);
assert.deepEqual(res.body, {
_id: `org.couchdb.user:banana`,

Check failure on line 27 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (16, ubuntu-latest)

Strings must use singlequote

Check failure on line 27 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)

Strings must use singlequote

Check failure on line 27 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)

Strings must use singlequote

Check failure on line 27 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (16, ubuntu-latest)

Strings must use singlequote

Check failure on line 27 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)

Strings must use singlequote

Check failure on line 27 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)

Strings must use singlequote
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`)

Check failure on line 37 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (16, ubuntu-latest)

Strings must use singlequote

Check failure on line 37 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)

Strings must use singlequote

Check failure on line 37 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)

Strings must use singlequote

Check failure on line 37 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (16, ubuntu-latest)

Strings must use singlequote

Check failure on line 37 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)

Strings must use singlequote

Check failure on line 37 in test/port/controller/UserController/showUser.test.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)

Strings must use singlequote
.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()
Expand Down

0 comments on commit 18a355b

Please sign in to comment.