62 lines
1.5 KiB
PHP
62 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Infrastructure\Adapters\Services;
|
|
|
|
use App\Exceptions\AuthServiceException;
|
|
use App\Models\User;
|
|
use Domain\Contracts\AdminDetails;
|
|
use Domain\Ports\Services\AdminAuthenticationService;
|
|
use Illuminate\Contracts\Auth\StatefulGuard;
|
|
|
|
/** @implements AdminAuthenticationService<User> */
|
|
class AppAdminAuthService implements AdminAuthenticationService
|
|
{
|
|
public function __construct(
|
|
private readonly StatefulGuard $guard
|
|
) {
|
|
}
|
|
|
|
public function canWithUser(\UnitEnum | string $abilities, mixed $arguments = []): bool
|
|
{
|
|
return $this->getUser()?->can($abilities, $arguments) ?? false;
|
|
}
|
|
|
|
public function check(): bool
|
|
{
|
|
return $this->guard->check();
|
|
}
|
|
|
|
public function guest(): bool
|
|
{
|
|
return ! $this->check();
|
|
}
|
|
|
|
public function getUser(): ?AdminDetails
|
|
{
|
|
$user = $this->guard->user();
|
|
|
|
if ($user && ! ($user instanceof User)) {
|
|
throw new \InvalidArgumentException('Auth service needs a backend guard');
|
|
}
|
|
|
|
return $user ? clone $user : null;
|
|
}
|
|
|
|
public function getUserAsMandatory(): AdminDetails
|
|
{
|
|
$user = $this->getUser();
|
|
|
|
if ($user === null) {
|
|
throw new AuthServiceException('Authenticated user is required from here');
|
|
}
|
|
|
|
return clone $user;
|
|
}
|
|
|
|
public function getMandatoryUserId(): string
|
|
{
|
|
return (string) $this->getUserAsMandatory()->id;
|
|
}
|
|
}
|