-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adaptive username #536
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 代码看起来没有明显的错误风险,但以下是一些建议:
总体而言,代码看起来相当简单且功能齐全。建议继续进行更准确的测试以确保正确的行为。 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ import { WebauthnCredential as WebauthnCredentialEntity } from '../entity/Webaut | |
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> ; | ||
|
||
|
@@ -59,12 +62,36 @@ type CreateWebauthnCredentialOptions = { | |
export class UserService extends AbstractService { | ||
@Inject() | ||
private readonly userRepository: UserRepository; | ||
@Inject() | ||
private readonly registryManagerService: RegistryManagerService; | ||
|
||
checkPassword(user: UserEntity, password: string): boolean { | ||
const plain = `${user.passwordSalt}${password}`; | ||
return checkIntegrity(plain, user.passwordIntegrity); | ||
} | ||
|
||
async findUserByNameOrDisplayName(name: string) { | ||
const hasPrefix = name.includes(':'); | ||
if (hasPrefix) { | ||
return await this.findUserByName(name); | ||
} | ||
|
||
const selfRegistry = await this.registryManagerService.ensureSelfRegistry(); | ||
const selfUser = await this.findUserByName(getPrefixedName(selfRegistry.userPrefix, name)); | ||
if (selfUser) { | ||
return selfUser; | ||
} | ||
|
||
const defaultRegistry = await this.registryManagerService.ensureDefaultRegistry(); | ||
const defaultUser = await this.findUserByName(getPrefixedName(defaultRegistry.userPrefix, name)); | ||
|
||
return 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> { | ||
return await this.userRepository.findUserByName(name); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段代码对UserService进行了补丁。下面是一些代码审查的建议和改进建议:
这些建议都是基于代码片段本身,更全面的代码审查还需要结合整个项目上下文来进行。如果有其他具体问题或需求,请提供更多细节。 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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:${encodeURIComponent('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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段代码的主要目标是测试用户控制器(UserController)中的一个方法。以下是对代码进行的简要代码审查:
针对改进的建议:
请注意,这只是对给定代码片段的初步审查,可能还有其他方面需要检查。最佳实践是进行全面而彻底的代码审查,包括阅读和理解相关的功能实现和错误处理逻辑。 |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在给定的代码补丁中,有一个新的函数
getPrefixedName
被添加到代码中。这个函数将一个前缀字符串和一个用户名字符串作为参数,并返回一个带有前缀的用户名字符串。代码的修改看起来是正确的,没有明显的错误或缺陷风险。然而,以下是一些建议改进:
getPrefixedName
函数中,可以添加输入参数的类型检查器,确保传递正确的参数类型。例如,为prefix
和username
添加类型注解string
。calculateIntegrity
函数中,你可以添加返回类型注解(可能是Promise<void>
)来指示异步函数的返回值类型。如果代码的上下文中还有其他内容,或者有其他方面需要注意的限制,请提供更多信息,以便我能够作出更准确的建议。