Skip to content

Commit 41a7561

Browse files
committed
Make sure a user has a unique email + save and encrypt password
1 parent b925719 commit 41a7561

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

src/Dal/UserDal.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static function create(UserEntity $userEntity): int|string|false
1818
$userBean->last_name = $userEntity->getLastName();
1919
$userBean->email = $userEntity->getEmail();
2020
$userBean->phone = $userEntity->getPhone();
21+
$userBean->password = $userEntity->getPassword();
2122
$userBean->created_date = $userEntity->getCreationDate();
2223

2324
try {
@@ -89,4 +90,10 @@ public static function remove(string $userUuid): bool
8990

9091
return false;
9192
}
93+
94+
public static function doesEmailExist(string $email): bool
95+
{
96+
// If R::findOne doesn't find any rows, it returns NULL (meaning, the email address doesn't exist)
97+
return R::findOne(self::TABLE_NAME, 'email = :email', ['email' => $email]) !== null;
98+
}
9299
}

src/Route/user.routes.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace PH7\ApiSimpleMenu\Route;
33

44
use PH7\ApiSimpleMenu\Route\Exception\NotFoundException;
5+
use PH7\ApiSimpleMenu\Service\Exception\EmailExistsException;
56
use PH7\ApiSimpleMenu\Service\User;
67
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
78

@@ -59,6 +60,14 @@ public function getResponse(): string
5960
'code' => $e->getCode()
6061
]
6162
];
63+
} catch (EmailExistsException $e) {
64+
HttpResponse::setHeadersByCode(StatusCode::BAD_REQUEST);
65+
66+
$response = [
67+
'errors' => [
68+
'message' => $e->getMessage()
69+
]
70+
];
6271
}
6372

6473
return json_encode($response);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace PH7\ApiSimpleMenu\Service\Exception;
4+
5+
use RuntimeException;
6+
7+
class EmailExistsException extends RuntimeException
8+
{
9+
}

src/Service/User.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace PH7\ApiSimpleMenu\Service;
33

44
use PH7\ApiSimpleMenu\Dal\UserDal;
5+
use PH7\ApiSimpleMenu\Service\Exception\EmailExistsException;
56
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
67
use PH7\ApiSimpleMenu\Validation\UserValidation;
78
use PH7\JustHttp\StatusCode;
@@ -27,8 +28,15 @@ public function create(mixed $data): array|object
2728
->setLastName($data->last)
2829
->setEmail($data->email)
2930
->setPhone($data->phone)
31+
->setPassword(password_hash($data->password, PASSWORD_ARGON2I))
3032
->setCreationDate(date(self::DATE_TIME_FORMAT));
3133

34+
$email = $userEntity->getEmail();
35+
if (UserDal::doesEmailExist($email)) {
36+
throw new EmailExistsException(
37+
sprintf('Email address %s already exists', $email)
38+
);
39+
}
3240

3341
if (UserDal::create($userEntity) === false) {
3442
// Set an internal error 500 when we cannot add an entry to the database

src/Validation/UserValidation.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ class UserValidation
1010
private const MINIMUM_NAME_LENGTH = 2;
1111
private const MAXIMUM_NAME_LENGTH = 40;
1212

13+
private const MINIMUM_PASSWORD_LENGTH = 5;
14+
1315
public function __construct(private readonly mixed $data) {}
1416

1517
public function isCreationSchemaValid(): bool
1618
{
1719
$schemaValidation =
1820
v::attribute('first', v::stringType()->length(self::MINIMUM_NAME_LENGTH, self::MAXIMUM_NAME_LENGTH))
1921
->attribute('last', v::stringType()->length(self::MINIMUM_NAME_LENGTH, self::MAXIMUM_NAME_LENGTH))
22+
->attribute('password', v::stringType()->length(self::MINIMUM_PASSWORD_LENGTH))
2023
->attribute('email', v::email())
2124
->attribute('phone', v::phone());
2225

0 commit comments

Comments
 (0)