feat: minimal locally stored user entity
This commit is contained in:
@@ -23,9 +23,9 @@ APP_SECRET=642ead390b8a38f0a017ec84d5362129
|
||||
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
|
||||
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
|
||||
#
|
||||
DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
|
||||
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
|
||||
# DATABASE_URL="mysql://app:[email protected]:3306/app?serverVersion=8.0.32&charset=utf8mb4"
|
||||
# DATABASE_URL="mysql://app:[email protected]:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4"
|
||||
DATABASE_URL="mysql://db:db@db:3306/db?serverVersion=10.11.11-MariaDB&charset=utf8mb4"
|
||||
# DATABASE_URL="postgresql://app:[email protected]:5432/app?serverVersion=16&charset=utf8"
|
||||
###< doctrine/doctrine-bundle ###
|
||||
|
||||
|
||||
@@ -4,8 +4,10 @@ security:
|
||||
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
|
||||
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
|
||||
providers:
|
||||
bpn_user_provider:
|
||||
id: App\BusProNet\Security\UserProvider
|
||||
app_user_provider:
|
||||
entity:
|
||||
class: App\Entity\User
|
||||
property: email
|
||||
firewalls:
|
||||
dev:
|
||||
pattern: ^/(_(profiler|wdt)|css|images|js)/
|
||||
@@ -18,9 +20,9 @@ security:
|
||||
- App\Security\ApiKeyAuthenticator
|
||||
main:
|
||||
lazy: true
|
||||
provider: bpn_user_provider
|
||||
provider: app_user_provider
|
||||
custom_authenticator:
|
||||
App\BusProNet\Security\Authenticator
|
||||
App\Security\BpnAuthenticator
|
||||
logout:
|
||||
path: app_logout
|
||||
target: app_login
|
||||
|
||||
+2
-2
@@ -103,10 +103,10 @@ task('deploy', [
|
||||
'rsync',
|
||||
'deploy:shared',
|
||||
'deploy:writable',
|
||||
// 'database:migrate',
|
||||
'database:migrate',
|
||||
'deploy:publish',
|
||||
'cachetool:clear:opcache',
|
||||
// 'deploy:stop-workers',
|
||||
'deploy:stop-workers',
|
||||
]);
|
||||
|
||||
task('deploy:stop-workers', function () {
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
final class Version20250322141523 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
// this up() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('CREATE TABLE messenger_messages (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, body CLOB NOT NULL, headers CLOB NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
|
||||
, available_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
|
||||
, delivered_at DATETIME DEFAULT NULL --(DC2Type:datetime_immutable)
|
||||
)');
|
||||
$this->addSql('CREATE INDEX IDX_75EA56E0FB7336F0 ON messenger_messages (queue_name)');
|
||||
$this->addSql('CREATE INDEX IDX_75EA56E0E3BD61CE ON messenger_messages (available_at)');
|
||||
$this->addSql('CREATE INDEX IDX_75EA56E016BA31DB ON messenger_messages (delivered_at)');
|
||||
$this->addSql('CREATE TABLE rememberme_token (series VARCHAR(88) NOT NULL, value VARCHAR(88) NOT NULL, lastUsed DATETIME NOT NULL --(DC2Type:datetime_immutable)
|
||||
, class VARCHAR(100) NOT NULL, username VARCHAR(200) NOT NULL, PRIMARY KEY(series))');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('DROP TABLE messenger_messages');
|
||||
$this->addSql('DROP TABLE rememberme_token');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
final class Version20250424143458 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
// this up() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(180) NOT NULL, password LONGTEXT NOT NULL, person_id INT DEFAULT NULL, address_id INT DEFAULT NULL, roles JSON NOT NULL COMMENT '(DC2Type:json)', last_login_at DATETIME DEFAULT NULL COMMENT '(DC2Type:datetime_immutable)', UNIQUE INDEX UNIQ_8D93D649E7927C74 (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB
|
||||
SQL);
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE TABLE messenger_messages (id BIGINT AUTO_INCREMENT NOT NULL, body LONGTEXT NOT NULL, headers LONGTEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at DATETIME NOT NULL COMMENT '(DC2Type:datetime_immutable)', available_at DATETIME NOT NULL COMMENT '(DC2Type:datetime_immutable)', delivered_at DATETIME DEFAULT NULL COMMENT '(DC2Type:datetime_immutable)', INDEX IDX_75EA56E0FB7336F0 (queue_name), INDEX IDX_75EA56E0E3BD61CE (available_at), INDEX IDX_75EA56E016BA31DB (delivered_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB
|
||||
SQL);
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql(<<<'SQL'
|
||||
DROP TABLE user
|
||||
SQL);
|
||||
$this->addSql(<<<'SQL'
|
||||
DROP TABLE messenger_messages
|
||||
SQL);
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\BusProNet\Security;
|
||||
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
class User implements UserInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $email,
|
||||
private readonly int $personId,
|
||||
private readonly int $addressId,
|
||||
private readonly ?string $password = null,
|
||||
private readonly array $roles = [],
|
||||
) {
|
||||
}
|
||||
|
||||
public function getEmail(): ?string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function getPassword(): ?string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function getPersonId(): ?int
|
||||
{
|
||||
return $this->personId;
|
||||
}
|
||||
|
||||
public function getAddressId(): int
|
||||
{
|
||||
return $this->addressId;
|
||||
}
|
||||
|
||||
public function getRoles(): array
|
||||
{
|
||||
return ['ROLE_USER', ...$this->roles];
|
||||
}
|
||||
|
||||
public function eraseCredentials(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function getUserIdentifier(): string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\BusProNet\Security;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Model\PersonalData;
|
||||
use App\Controller\Traits\CredentialsTrait;
|
||||
use App\Security\Crypt;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||||
|
||||
class UserProvider implements UserProviderInterface
|
||||
{
|
||||
use CredentialsTrait;
|
||||
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly RequestStack $requestStack,
|
||||
private readonly Crypt $crypt,
|
||||
) {
|
||||
}
|
||||
|
||||
public function refreshUser(UserInterface $user): UserInterface
|
||||
{
|
||||
if (null !== $activeUser = $this->requestStack->getSession()->get('bpn_user')) {
|
||||
return $activeUser;
|
||||
}
|
||||
|
||||
return $this->loadUserByIdentifier($user->getUserIdentifier());
|
||||
}
|
||||
|
||||
public function supportsClass(string $class): bool
|
||||
{
|
||||
return User::class === $class;
|
||||
}
|
||||
|
||||
public function loadUserByIdentifier(string $identifier): UserInterface
|
||||
{
|
||||
if (null === $activeUser = $this->requestStack->getSession()->get('bpn_user')) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
try {
|
||||
$email = $activeUser->getEmail();
|
||||
$password = $this->crypt->decrypt($activeUser->getPassword());
|
||||
$response = $this->apiClient->getPersonalData($email, $password);
|
||||
} catch (ApiClientException $e) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
if (false === $response instanceof PersonalData) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
return $activeUser;
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,8 @@ namespace App\Controller\Booking;
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Model\Notification;
|
||||
use App\BusProNet\Security\User;
|
||||
use App\Controller\Traits\BookingDataTrait;
|
||||
use App\Controller\Traits\CredentialsTrait;
|
||||
use App\Entity\User;
|
||||
use App\Security\Crypt;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
@@ -21,7 +20,6 @@ use function Symfony\Component\String\u;
|
||||
|
||||
class DownloadController extends AbstractController
|
||||
{
|
||||
use CredentialsTrait;
|
||||
use BookingDataTrait;
|
||||
|
||||
public function __construct(
|
||||
|
||||
@@ -5,11 +5,10 @@ namespace App\Controller\Booking;
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Model\Notification;
|
||||
use App\BusProNet\Security\User;
|
||||
use App\BusProNet\XmlLoader\PickupLoader;
|
||||
use App\BusProNet\XmlLoader\TravelLoader;
|
||||
use App\Controller\Traits\BookingDataTrait;
|
||||
use App\Controller\Traits\CredentialsTrait;
|
||||
use App\Entity\User;
|
||||
use App\Form\BookingType;
|
||||
use App\Form\Model\BookingData;
|
||||
use App\Security\Crypt;
|
||||
@@ -25,7 +24,6 @@ use Symfony\Contracts\Cache\CacheInterface;
|
||||
|
||||
class EditController extends AbstractController
|
||||
{
|
||||
use CredentialsTrait;
|
||||
use BookingDataTrait;
|
||||
|
||||
public function __construct(
|
||||
|
||||
@@ -6,9 +6,8 @@ use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Model\BaseData;
|
||||
use App\BusProNet\Model\Notification;
|
||||
use App\BusProNet\Security\User;
|
||||
use App\BusProNet\XmlLoader\TravelLoader;
|
||||
use App\Controller\Traits\CredentialsTrait;
|
||||
use App\Entity\User;
|
||||
use App\Security\Crypt;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
@@ -19,8 +18,6 @@ use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
use CredentialsTrait;
|
||||
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly TravelLoader $travelDataLoader,
|
||||
|
||||
@@ -6,8 +6,7 @@ use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Model\Notification;
|
||||
use App\BusProNet\Model\PersonalData;
|
||||
use App\BusProNet\Security\User;
|
||||
use App\Controller\Traits\CredentialsTrait;
|
||||
use App\Entity\User;
|
||||
use App\Form\PersonalDataType;
|
||||
use App\Security\Crypt;
|
||||
use Psr\Log\LoggerInterface;
|
||||
@@ -19,8 +18,6 @@ use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
class PersonalDataController extends AbstractController
|
||||
{
|
||||
use CredentialsTrait;
|
||||
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly Crypt $crypt,
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Traits;
|
||||
|
||||
use App\BusProNet\Security\User;
|
||||
use App\Security\Crypt;
|
||||
|
||||
trait CredentialsTrait
|
||||
{
|
||||
public function getPasswordPlain(User $bpnUser, string $pathToKeys): string
|
||||
{
|
||||
$crypt = new Crypt($pathToKeys);
|
||||
|
||||
return $crypt->decrypt($bpnUser->getPassword());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
#[ORM\Entity]
|
||||
class User implements UserInterface, PasswordAuthenticatedUserInterface
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 180, unique: true)]
|
||||
private ?string $email;
|
||||
|
||||
#[ORM\Column(type: 'text')]
|
||||
private ?string $password;
|
||||
|
||||
#[ORM\Column(type: 'integer', nullable: true)]
|
||||
private ?int $personId = null;
|
||||
|
||||
#[ORM\Column(type: 'integer', nullable: true)]
|
||||
private ?int $addressId = null;
|
||||
|
||||
#[ORM\Column(type: 'json')]
|
||||
private array $roles = [];
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
|
||||
private ?\DateTimeImmutable $lastLoginAt = null;
|
||||
|
||||
public function __construct(string $email, string $password)
|
||||
{
|
||||
$this->email = $email;
|
||||
$this->password = base64_encode($password);
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getEmail(): ?string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function setEmail(?string $email): static
|
||||
{
|
||||
$this->email = $email;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPersonId(): ?int
|
||||
{
|
||||
return $this->personId;
|
||||
}
|
||||
|
||||
public function setPersonId(?int $personId): static
|
||||
{
|
||||
$this->personId = $personId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAddressId(): ?int
|
||||
{
|
||||
return $this->addressId;
|
||||
}
|
||||
|
||||
public function setAddressId(?int $addressId): static
|
||||
{
|
||||
$this->addressId = $addressId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPassword(): ?string
|
||||
{
|
||||
return base64_decode($this->password);
|
||||
}
|
||||
|
||||
public function setPassword(?string $password): static
|
||||
{
|
||||
$this->password = base64_encode($password);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRoles(): array
|
||||
{
|
||||
return ['ROLE_USER', ...$this->roles];
|
||||
}
|
||||
|
||||
public function setRoles(array $roles): static
|
||||
{
|
||||
$this->roles = $roles;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLastLoginAt(): ?\DateTimeImmutable
|
||||
{
|
||||
return $this->lastLoginAt;
|
||||
}
|
||||
|
||||
public function setLastLoginAt(?\DateTimeImmutable $lastLoginAt): static
|
||||
{
|
||||
$this->lastLoginAt = $lastLoginAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function eraseCredentials(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function getUserIdentifier(): string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\BusProNet\Security;
|
||||
namespace App\Security;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Model\CrmAttributes;
|
||||
use App\BusProNet\Model\PersonalData;
|
||||
use App\Security\Crypt;
|
||||
use App\Entity\User;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -22,11 +23,12 @@ use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
|
||||
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
|
||||
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
|
||||
|
||||
class Authenticator extends AbstractLoginFormAuthenticator implements AuthenticationEntryPointInterface
|
||||
class BpnAuthenticator extends AbstractLoginFormAuthenticator implements AuthenticationEntryPointInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly UrlGeneratorInterface $urlGenerator,
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly Crypt $crypt,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
@@ -59,14 +61,7 @@ class Authenticator extends AbstractLoginFormAuthenticator implements Authentica
|
||||
|
||||
return new SelfValidatingPassport(
|
||||
new UserBadge($email, function () use ($email, $password, $response, $request) {
|
||||
$crmAttributes = $this->apiClient->getCrmAttributes($email, $password);
|
||||
$roles = $this->collectRoles($crmAttributes);
|
||||
$encryptedPassword = $this->crypt->encrypt($password);
|
||||
$user = new User($email, $response->personId, $response->addressId, $encryptedPassword, $roles);
|
||||
|
||||
$request->getSession()->set('bpn_user', $user);
|
||||
|
||||
return $user;
|
||||
return $this->createOrUpdateLocalUser($email, $password, $response->personId, $response->addressId);
|
||||
}),
|
||||
[
|
||||
new CsrfTokenBadge('authenticate', $csrfToken),
|
||||
@@ -75,6 +70,36 @@ class Authenticator extends AbstractLoginFormAuthenticator implements Authentica
|
||||
);
|
||||
}
|
||||
|
||||
private function createOrUpdateLocalUser(string $email, string $password, ?int $personId, ?int $addressId): User
|
||||
{
|
||||
try {
|
||||
$crmAttributes = $this->apiClient->getCrmAttributes($email, $password);
|
||||
} catch (ApiClientException $e) {
|
||||
throw new CustomUserMessageAuthenticationException($e->getMessage());
|
||||
}
|
||||
|
||||
$roles = $this->collectRoles($crmAttributes);
|
||||
$encryptedPassword = $this->crypt->encrypt($password);
|
||||
|
||||
$userRepository = $this->entityManager->getRepository(User::class);
|
||||
|
||||
if (null === $user = $userRepository->findOneBy(['email' => $email])) {
|
||||
$user = new User($email, $encryptedPassword);
|
||||
$this->entityManager->persist($user);
|
||||
}
|
||||
|
||||
$user
|
||||
->setPersonId($personId)
|
||||
->setAddressId($addressId)
|
||||
->setRoles($roles)
|
||||
->setLastLoginAt(new \DateTimeImmutable())
|
||||
;
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
|
||||
{
|
||||
$this->logger->info('Login', [
|
||||
Reference in New Issue
Block a user