From 2172a146c955e202546986ca72ed895b73647eed Mon Sep 17 00:00:00 2001 From: Norbert Schvoy Date: Thu, 7 Mar 2024 10:32:00 +0100 Subject: [PATCH] * Change composer.json requirements * Add new `EightMarq\UserBundle\EventSubscriber\PasswordHashingDoctrineEventSubscriber` * Remove unnecessary type cast from User entity --- CHANGELOG.md | 6 +++ README.md | 30 +++++++++---- composer.json | 8 ++-- src/Entity/User.php | 4 +- ...PasswordHashingDoctrineEventSubscriber.php | 42 +++++++++++++++++++ src/Resources/config/services.yaml | 3 ++ 6 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 src/EventSubscriber/PasswordHashingDoctrineEventSubscriber.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f220a3..d6f9a57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.9.0 + +* Change composer.json requirements +* Add new `EightMarq\UserBundle\EventSubscriber\PasswordHashingDoctrineEventSubscriber` +* Remove unnecessary type cast from User entity + ## 0.8.3 * Change composer.json requirements diff --git a/README.md b/README.md index 6f091b2..b3dd0b4 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ # EightMarq - User bundle -[WIP] User bundle - -## Requirements - -> Important! PHP 8.1 is required for this bundle, because in this bundle we use typed properties feature! +This bundle provides a basic user entity and user repository, +that can be used immediately without so much effort, just a few small steps needed to use it. +However, these are extendable and you can add additional properties easily. ## Installation @@ -50,10 +48,8 @@ use App\Repository\UserRepository; use Doctrine\ORM\Mapping as ORM; use EightMarq\UserBundle\Entity\User as BaseUser; -/** - * @ORM\Entity(repositoryClass=UserRepository::class) - * @ORM\Table(name="users") - */ +#[ORM\Entity(repositoryClass: UserRepository::class)] +#[ORM\Table(name: 'users')] class User extends BaseUser { @@ -76,6 +72,22 @@ security: [...] ``` +## Event subscriber + +The `EightMarq\UserBundle\EventSubscriber\PasswordHashingDoctrineEventSubscriber` automatically hashes the plain password, when during a new user entity creation. + +In some cases, you don't need this behavior, so you can disable it with the following code: + +```php + PasswordHashingDoctrineEventSubscriber::setEnabled(false); +``` + +And after that you can re-enable it: + +```php + PasswordHashingDoctrineEventSubscriber::setEnabled(true); +``` + ## Configuration reference No configuration \ No newline at end of file diff --git a/composer.json b/composer.json index 2b02a11..8c90812 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "eightmarq/user-bundle", - "description": "EightMarq user bundle", + "description": "EightMarq user bundle - This bundle provides a basic user entity and user repository.", "type": "symfony-bundle", "minimum-stability": "dev", "license": "MIT", @@ -19,10 +19,10 @@ "psr-4": { "EightMarq\\UserBundle\\Tests\\": "tests/" } }, "require": { - "php": "^8.1", - "symfony/framework-bundle": "^6.1", + "php": ">=8.1", + "symfony/framework-bundle": "^6.4|^7.0", "symfony/orm-pack": "*", - "eightmarq/core-bundle": "^0.8" + "eightmarq/core-bundle": "^0.9" }, "require-dev": { "phpunit/phpunit": "^9.5" diff --git a/src/Entity/User.php b/src/Entity/User.php index a4b2f3e..1ac6a6e 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -53,7 +53,7 @@ public function setEmail(string $email): void */ public function getUsername(): string { - return (string)$this->email; + return $this->email; } /** @@ -79,7 +79,7 @@ public function setRoles(array $roles): void */ public function getPassword(): string { - return (string)$this->password; + return $this->password; } public function setPassword(string $password): void diff --git a/src/EventSubscriber/PasswordHashingDoctrineEventSubscriber.php b/src/EventSubscriber/PasswordHashingDoctrineEventSubscriber.php new file mode 100644 index 0000000..3daba22 --- /dev/null +++ b/src/EventSubscriber/PasswordHashingDoctrineEventSubscriber.php @@ -0,0 +1,42 @@ +getObject(); + + if ($entity instanceof User && self::$enabled) { + $this->passwordUpdater->hashPassword($entity, $entity->getPassword()); + } + } +} diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 14b991a..51c134b 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -8,3 +8,6 @@ services: autowire: true EightMarq\UserBundle\Security\PasswordUpdaterInterface: '@EightMarq\UserBundle\Security\PasswordUpdater' + + EightMarq\UserBundle\EventSubscriber\PasswordHashingDoctrineEventSubscriber: + tags: [ 'doctrine.event_subscriber' ] \ No newline at end of file