Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<?php

use Authentication\Value\EmailAddress;
use Authentication\Value\PasswordHash;
use Doctrine\DBAL\Driver\PDOSqlite\Driver;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\ORM\Proxy\ProxyFactory;
use Infrastructure\Authentication\DBAL\Type\EmailAddressType;
use Infrastructure\Authentication\DBAL\Type\PasswordHashType;

require_once __DIR__ . '/vendor/autoload.php';

Expand All @@ -19,7 +24,10 @@

// We are telling Doctrine to always generate files required for lazy-loading. This is a slow operation,
// and shouldn't be done in a production environment.
$configuration->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_ALWAYS);
$configuration->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);

Type::addType(EmailAddress::class, EmailAddressType::class);
Type::addType(PasswordHash::class, PasswordHashType::class);

// Finally creating the EntityManager: our entry point for the ORM
return EntityManager::create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping ../vendor/doctrine/orm/doctrine-mapping.xsd"
>
<entity name="Authentication\Entity\User" table="users">
<entity name="Authentication\Aggregate\User" table="users">
<id name="email" type="Authentication\Value\EmailAddress" column="email"/>
<field name="passwordHash" type="Authentication\Value\PasswordHash" column="password_hash"/>
</entity>
</doctrine-mapping>
8 changes: 6 additions & 2 deletions public/login.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?php

use Authentication\Aggregate\User;
use Authentication\Value\ClearTextPassword;
use Authentication\Value\EmailAddress;
use Infrastructure\Authentication\ReadModel\HardcodedIsUserBlocked;
use Infrastructure\Authentication\Repository\FilesystemUsers;
use Infrastructure\Authentication\Repository\DoctrineUsers;
use Infrastructure\Authentication\Service\SendNotifyOfIntrusionDetectionToStderr;

require_once __DIR__ . '/../vendor/autoload.php';

$email = EmailAddress::fromEmailAddress($_POST['emailAddress']);
$password = ClearTextPassword::fromInputPassword($_POST['password']);

$users = new FilesystemUsers(__DIR__ . '/../data/users.json');
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = require __DIR__ . '/../bootstrap.php';

$users = new DoctrineUsers($entityManager, $entityManager->getRepository(User::class));

if (! $users->exists($email)) {
echo 'Nope';
Expand Down
7 changes: 5 additions & 2 deletions public/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
use Authentication\Value\ClearTextPassword;
use Authentication\Value\EmailAddress;
use Infrastructure\Authentication\ReadModel\CheckRegisteredEmailFromRepository;
use Infrastructure\Authentication\Repository\FilesystemUsers;
use Infrastructure\Authentication\Repository\DoctrineUsers;

require_once __DIR__ . '/../vendor/autoload.php';

$email = EmailAddress::fromEmailAddress($_POST['emailAddress']);
$password = ClearTextPassword::fromInputPassword($_POST['password']);

$users = new FilesystemUsers(__DIR__ . '/../data/users.json');
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = require __DIR__ . '/../bootstrap.php';

$users = new DoctrineUsers($entityManager, $entityManager->getRepository(User::class));

$users->store(User::register($email, $password, new CheckRegisteredEmailFromRepository($users)));

Expand Down
5 changes: 5 additions & 0 deletions src/Authentication/Value/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public function toString() : string
{
return $this->email;
}

public function __toString() : string
{
return $this->email;
}
}
36 changes: 36 additions & 0 deletions src/Infrastructure/Authentication/DBAL/Type/EmailAddressType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Infrastructure\Authentication\DBAL\Type;

use Authentication\Value\EmailAddress;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\StringType;

final class EmailAddressType extends StringType
{
public function convertToPHPValue($value, AbstractPlatform $platform)
{
\assert(is_string($value));

return EmailAddress::fromEmailAddress($value);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
\assert($value instanceof EmailAddress);

return parent::convertToDatabaseValue($value->toString(), $platform);
}

public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}

public function getName()
{
return EmailAddress::class;
}
}
36 changes: 36 additions & 0 deletions src/Infrastructure/Authentication/DBAL/Type/PasswordHashType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Infrastructure\Authentication\DBAL\Type;

use Authentication\Value\PasswordHash;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\StringType;

final class PasswordHashType extends StringType
{
public function convertToPHPValue($value, AbstractPlatform $platform)
{
\assert(is_string($value));

return PasswordHash::fromHash($value);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
\assert($value instanceof PasswordHash);

return parent::convertToDatabaseValue($value->toString(), $platform);
}

public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}

public function getName()
{
return PasswordHash::class;
}
}
51 changes: 51 additions & 0 deletions src/Infrastructure/Authentication/Repository/DoctrineUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Infrastructure\Authentication\Repository;

use Authentication\Aggregate\User;
use Authentication\Repository\Users;
use Authentication\Value\EmailAddress;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;

final class DoctrineUsers implements Users
{
/** @var ObjectRepository */
private $repository;

/** @var ObjectManager */
private $objectManager;

public function __construct(ObjectManager $objectManager, ObjectRepository $repository)
{
$this->objectManager = $objectManager;
$this->repository = $repository;
}

public function exists(EmailAddress $emailAddress) : bool
{
return (bool) $this->repository->find($emailAddress);
}

public function get(EmailAddress $emailAddress) : User
{
$user = $this->repository->find($emailAddress);

if (! $user instanceof User) {
throw new \RuntimeException(sprintf(
'User %s does not exist',
$emailAddress->toString()
));
}

return $user;
}

public function store(User $user) : void
{
$this->objectManager->persist($user);
$this->objectManager->flush();
}
}