implement SignupUser displayname requirement
This commit is contained in:
parent
4829a02aac
commit
4b1689d17e
2 changed files with 21 additions and 0 deletions
|
|
@ -15,6 +15,8 @@ class SignupUser
|
|||
{
|
||||
private const MIN_PASSWORD_LENGTH = 8;
|
||||
|
||||
private const DISPLAY_NAME_PATTERN = '/^[a-z0-9_-]{3,30}$/';
|
||||
|
||||
public function __construct(
|
||||
private UserRepository $userRepo,
|
||||
private PasswordHasher $hasher,
|
||||
|
|
@ -29,6 +31,19 @@ class SignupUser
|
|||
if ($request->email === null || $request->email === '') {
|
||||
throw new BadRequestException('email is required');
|
||||
}
|
||||
if ($request->displayName === null || $request->displayName === '') {
|
||||
throw new BadRequestException('displayName is required');
|
||||
}
|
||||
if (
|
||||
preg_match(
|
||||
self::DISPLAY_NAME_PATTERN,
|
||||
$request->displayName,
|
||||
) !== 1
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
'displayName must be 3-30 chars of [a-z0-9_-]'
|
||||
);
|
||||
}
|
||||
if ($request->password === null || $request->password === '') {
|
||||
throw new BadRequestException('password is required');
|
||||
}
|
||||
|
|
@ -47,11 +62,16 @@ class SignupUser
|
|||
if ($this->userRepo->findByEmail($email) !== null) {
|
||||
throw new DomainException('email already registered');
|
||||
}
|
||||
if ($this->userRepo->findByDisplayName($request->displayName) !== null) {
|
||||
throw new DomainException('displayName already taken');
|
||||
}
|
||||
|
||||
return $this->userRepo->create(new CreateUserDto(
|
||||
email: $email,
|
||||
displayName: $request->displayName,
|
||||
passwordHash: $this->hasher->hash($request->password),
|
||||
isAdmin: false,
|
||||
emailConfirmedAt: null,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ class SignupUserRequest
|
|||
{
|
||||
public function __construct(
|
||||
public ?string $email,
|
||||
public ?string $displayName,
|
||||
public ?string $password,
|
||||
) {}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue