mirror of
https://github.com/kevinpapst/kimai2.git
synced 2025-03-29 19:11:32 +00:00
prevent email or username becoming non-unique
This commit is contained in:
parent
f743503705
commit
8eb5f35bee
6 changed files with 56 additions and 18 deletions
src
translations
|
@ -82,6 +82,11 @@ class UserRepository extends EntityRepository implements UserLoaderInterface
|
|||
return parent::findOneBy($criteria, $orderBy);
|
||||
}
|
||||
|
||||
public function findByUsername($username): ?User
|
||||
{
|
||||
return parent::findOneBy(['username' => $username]);
|
||||
}
|
||||
|
||||
public function countUser(?bool $enabled = null): int
|
||||
{
|
||||
if (null !== $enabled) {
|
||||
|
|
|
@ -127,7 +127,7 @@ class UserService
|
|||
|
||||
public function findUserByName(string $name): ?User
|
||||
{
|
||||
return $this->repository->findOneBy(['username' => $name]);
|
||||
return $this->repository->findByUsername($name);
|
||||
}
|
||||
|
||||
public function findUserByConfirmationToken(string $token): ?User
|
||||
|
|
|
@ -20,10 +20,14 @@ class User extends Constraint
|
|||
{
|
||||
public const USER_EXISTING_EMAIL = 'kimai-user-00';
|
||||
public const USER_EXISTING_NAME = 'kimai-user-01';
|
||||
public const USER_EXISTING_EMAIL_AS_NAME = 'kimai-user-02';
|
||||
public const USER_EXISTING_NAME_AS_EMAIL = 'kimai-user-03';
|
||||
|
||||
protected static $errorNames = [
|
||||
self::USER_EXISTING_EMAIL => 'The email is already used.',
|
||||
self::USER_EXISTING_NAME => 'The username is already used.',
|
||||
self::USER_EXISTING_EMAIL_AS_NAME => 'An equal username is already used.',
|
||||
self::USER_EXISTING_NAME_AS_EMAIL => 'An equal email is already used.',
|
||||
];
|
||||
|
||||
public $message = 'The user has invalid settings.';
|
||||
|
|
|
@ -44,28 +44,41 @@ class UserValidator extends ConstraintValidator
|
|||
|
||||
protected function validateUser(UserEntity $user, ExecutionContextInterface $context)
|
||||
{
|
||||
$matchedEmail = false;
|
||||
if ($user->getEmail() !== null) {
|
||||
$existingByEmail = $this->userService->findUserByEmail($user->getEmail());
|
||||
|
||||
if (null !== $existingByEmail && $user->getId() !== $existingByEmail->getId()) {
|
||||
$context->buildViolation(User::getErrorName(User::USER_EXISTING_EMAIL))
|
||||
->atPath('email')
|
||||
->setTranslationDomain('validators')
|
||||
->setCode(User::USER_EXISTING_EMAIL)
|
||||
->addViolation();
|
||||
}
|
||||
$this->validateEmailExists($user->getId(), $user->getEmail(), 'email', User::USER_EXISTING_EMAIL, $context);
|
||||
$this->validateEmailExists($user->getId(), $user->getUsername(), 'username', User::USER_EXISTING_NAME_AS_EMAIL, $context);
|
||||
}
|
||||
|
||||
if ($user->getUsername() !== null) {
|
||||
$existingByName = $this->userService->findUserByName($user->getUsername());
|
||||
$this->validateUsernameExists($user->getId(), $user->getUsername(), 'username', User::USER_EXISTING_NAME, $context);
|
||||
$this->validateUsernameExists($user->getId(), $user->getEmail(), 'email', User::USER_EXISTING_EMAIL_AS_NAME, $context);
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $existingByName && $user->getId() !== $existingByName->getId()) {
|
||||
$context->buildViolation(User::getErrorName(User::USER_EXISTING_NAME))
|
||||
->atPath('username')
|
||||
->setTranslationDomain('validators')
|
||||
->setCode(User::USER_EXISTING_NAME)
|
||||
->addViolation();
|
||||
}
|
||||
private function validateEmailExists(?int $userId, string $email, string $path, string $code, ExecutionContextInterface $context): void
|
||||
{
|
||||
$existingByEmail = $this->userService->findUserByEmail($email);
|
||||
|
||||
if (null !== $existingByEmail && $userId !== $existingByEmail->getId()) {
|
||||
$context->buildViolation(User::getErrorName($code))
|
||||
->atPath($path)
|
||||
->setTranslationDomain('validators')
|
||||
->setCode($code)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
private function validateUsernameExists(?int $userId, string $username, string $path, string $code, ExecutionContextInterface $context): void
|
||||
{
|
||||
$existingByName = $this->userService->findUserByName($username);
|
||||
|
||||
if (null !== $existingByName && $userId !== $existingByName->getId()) {
|
||||
$context->buildViolation(User::getErrorName($code))
|
||||
->atPath($path)
|
||||
->setTranslationDomain('validators')
|
||||
->setCode($code)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,14 @@
|
|||
<source>The username is already used.</source>
|
||||
<target>Dieser Benutzername wird bereits verwendet.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="An equal username is already used.">
|
||||
<source>An equal username is already used.</source>
|
||||
<target>Eine gleichlautender Benutzername wird bereits verwendet.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="An equal email is already used.">
|
||||
<source>An equal email is already used.</source>
|
||||
<target>Eine gleichlautende E-Mail-Adresse wird bereits verwendet.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="This value is not a valid role.">
|
||||
<source>This value is not a valid role.</source>
|
||||
<target>Dieser Wert ist keine gültige Rolle.</target>
|
||||
|
|
|
@ -18,6 +18,14 @@
|
|||
<source>The username is already used.</source>
|
||||
<target>The username is already used.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="An equal username is already used.">
|
||||
<source>An equal username is already used.</source>
|
||||
<target>An equal username is already used.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="An equal email is already used.">
|
||||
<source>An equal email is already used.</source>
|
||||
<target>An equal email is already used.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="This value is not a valid role.">
|
||||
<source>This value is not a valid role.</source>
|
||||
<target>This value is not a valid role.</target>
|
||||
|
|
Loading…
Add table
Reference in a new issue