extend User entity with displayname and email confirmation
Add display_name (unique) and email_confirmed_at columns plus matching getters, DTO fields, repo methods (findByDisplayName, update), and migration. Existing auth tests updated to construct User with the new params.
This commit is contained in:
parent
d547ec2c61
commit
298b8634ec
10 changed files with 131 additions and 15 deletions
|
|
@ -3,12 +3,15 @@
|
|||
namespace App\User;
|
||||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use DateTimeImmutable;
|
||||
|
||||
readonly class CreateUserDto
|
||||
{
|
||||
public function __construct(
|
||||
public EmailAddress $email,
|
||||
public string $displayName,
|
||||
public string $passwordHash,
|
||||
public bool $isAdmin,
|
||||
public ?DateTimeImmutable $emailConfirmedAt,
|
||||
) {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@
|
|||
namespace App\User;
|
||||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use RuntimeException;
|
||||
|
||||
class EloquentUserRepository implements UserRepository
|
||||
{
|
||||
|
|
@ -10,8 +13,10 @@ class EloquentUserRepository implements UserRepository
|
|||
{
|
||||
$model = UserModel::create([
|
||||
'email' => $dto->email->value(),
|
||||
'display_name' => $dto->displayName,
|
||||
'password_hash' => $dto->passwordHash,
|
||||
'is_admin' => $dto->isAdmin,
|
||||
'email_confirmed_at' => $dto->emailConfirmedAt,
|
||||
]);
|
||||
|
||||
return $this->toDomain($model);
|
||||
|
|
@ -31,13 +36,51 @@ class EloquentUserRepository implements UserRepository
|
|||
return $model === null ? null : $this->toDomain($model);
|
||||
}
|
||||
|
||||
public function findByDisplayName(string $displayName): ?User
|
||||
{
|
||||
$model = UserModel::where('display_name', $displayName)->first();
|
||||
|
||||
return $model === null ? null : $this->toDomain($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function update(User $user): User
|
||||
{
|
||||
$model = UserModel::find($user->getId());
|
||||
if ($model === null) {
|
||||
throw new RuntimeException(
|
||||
"User with id: {$user->getId()} does not exist"
|
||||
);
|
||||
}
|
||||
$model->email = $user->getEmail()->value();
|
||||
$model->display_name = $user->getDisplayName();
|
||||
$model->password_hash = $user->getPasswordHash();
|
||||
$model->is_admin = $user->isAdmin();
|
||||
$model->email_confirmed_at = $user->getEmailConfirmedAt();
|
||||
$model->save();
|
||||
|
||||
return $this->toDomain($model);
|
||||
}
|
||||
|
||||
private function toDomain(UserModel $model): User
|
||||
{
|
||||
$confirmedAt = null;
|
||||
if ($model->email_confirmed_at !== null) {
|
||||
$confirmedAt = new DateTimeImmutable(
|
||||
$model->email_confirmed_at->toDateTimeString(),
|
||||
new DateTimeZone('UTC'),
|
||||
);
|
||||
}
|
||||
|
||||
return new User(
|
||||
id: $model->id,
|
||||
email: new EmailAddress($model->email),
|
||||
displayName: $model->display_name,
|
||||
passwordHash: $model->password_hash,
|
||||
isAdmin: $model->is_admin,
|
||||
emailConfirmedAt: $confirmedAt,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,14 +3,17 @@
|
|||
namespace App\User;
|
||||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class User
|
||||
{
|
||||
public function __construct(
|
||||
private int $id,
|
||||
private EmailAddress $email,
|
||||
private string $displayName,
|
||||
private string $passwordHash,
|
||||
private bool $isAdmin,
|
||||
private ?DateTimeImmutable $emailConfirmedAt,
|
||||
) {}
|
||||
|
||||
public function getId(): int
|
||||
|
|
@ -23,6 +26,11 @@ class User
|
|||
return $this->email;
|
||||
}
|
||||
|
||||
public function getDisplayName(): string
|
||||
{
|
||||
return $this->displayName;
|
||||
}
|
||||
|
||||
public function getPasswordHash(): string
|
||||
{
|
||||
return $this->passwordHash;
|
||||
|
|
@ -32,4 +40,14 @@ class User
|
|||
{
|
||||
return $this->isAdmin;
|
||||
}
|
||||
|
||||
public function getEmailConfirmedAt(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->emailConfirmedAt;
|
||||
}
|
||||
|
||||
public function isEmailConfirmed(): bool
|
||||
{
|
||||
return $this->emailConfirmedAt !== null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,21 +2,26 @@
|
|||
|
||||
namespace App\User;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $email
|
||||
* @property string $display_name
|
||||
* @property string $password_hash
|
||||
* @property bool $is_admin
|
||||
* @property ?DateTimeImmutable $email_confirmed_at
|
||||
*
|
||||
* @method static Builder<static>|UserModel newModelQuery()
|
||||
* @method static Builder<static>|UserModel newQuery()
|
||||
* @method static Builder<static>|UserModel query()
|
||||
* @method static Builder<static>|UserModel whereId($value)
|
||||
* @method static Builder<static>|UserModel whereEmail($value)
|
||||
* @method static Builder<static>|UserModel whereDisplayName($value)
|
||||
* @method static Builder<static>|UserModel whereIsAdmin($value)
|
||||
* @method static Builder<static>|UserModel whereEmailConfirmedAt($value)
|
||||
*
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
|
|
@ -28,11 +33,14 @@ class UserModel extends Model
|
|||
|
||||
protected $fillable = [
|
||||
'email',
|
||||
'display_name',
|
||||
'password_hash',
|
||||
'is_admin',
|
||||
'email_confirmed_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_admin' => 'boolean',
|
||||
'email_confirmed_at' => 'immutable_datetime',
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,4 +11,8 @@ interface UserRepository
|
|||
public function find(int $id): ?User;
|
||||
|
||||
public function findByEmail(EmailAddress $email): ?User;
|
||||
|
||||
public function findByDisplayName(string $displayName): ?User;
|
||||
|
||||
public function update(User $user): User;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue