Skip to content

Commit

Permalink
* Change composer.json requirements
Browse files Browse the repository at this point in the history
* Add new `EightMarq\UserBundle\EventSubscriber\PasswordHashingDoctrineEventSubscriber`
* Remove unnecessary type cast from User entity
  • Loading branch information
schvoy committed Mar 7, 2024
1 parent 92d217b commit 2172a14
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
{

Expand All @@ -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
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function setEmail(string $email): void
*/
public function getUsername(): string
{
return (string)$this->email;
return $this->email;
}

/**
Expand All @@ -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
Expand Down
42 changes: 42 additions & 0 deletions src/EventSubscriber/PasswordHashingDoctrineEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace EightMarq\UserBundle\EventSubscriber;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Events;
use EightMarq\UserBundle\Entity\User;
use EightMarq\UserBundle\Security\PasswordUpdaterInterface;

class PasswordHashingDoctrineEventSubscriber implements EventSubscriber
{
private static $enabled = true;

public static function setEnabled(bool $enabled)
{
self::$enabled = $enabled;
}

public function __construct(
private readonly PasswordUpdaterInterface $passwordUpdater
) {
}

public function getSubscribedEvents(): array
{
return [
Events::prePersist,
];
}

public function prePersist(PrePersistEventArgs $args): void
{
$entity = $args->getObject();

if ($entity instanceof User && self::$enabled) {
$this->passwordUpdater->hashPassword($entity, $entity->getPassword());
}
}
}
3 changes: 3 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ services:
autowire: true

EightMarq\UserBundle\Security\PasswordUpdaterInterface: '@EightMarq\UserBundle\Security\PasswordUpdater'

EightMarq\UserBundle\EventSubscriber\PasswordHashingDoctrineEventSubscriber:
tags: [ 'doctrine.event_subscriber' ]

0 comments on commit 2172a14

Please sign in to comment.