From e803bf84bfbe73f76b97f6f74b2abfa3a414df5d Mon Sep 17 00:00:00 2001 From: core23 Date: Sun, 11 Jun 2023 17:08:49 +0200 Subject: [PATCH] Remove canonical feature --- UPGRADE-3.0.md | 19 +++++ docs/canonicalizer.rst | 47 ------------ docs/configuration_reference.rst | 2 - docs/custom_storage_layer.rst | 2 +- docs/index.rst | 1 - docs/user_manager.rst | 8 +- src/DependencyInjection/Configuration.php | 2 - .../NucleosUserExtension.php | 3 - src/Doctrine/UserListener.php | 8 +- src/Doctrine/UserManager.php | 7 +- src/Model/BaseUserManager.php | 23 +----- src/Model/User.php | 28 +------ src/Model/UserInterface.php | 20 ----- src/Model/UserManager.php | 5 -- .../config/doctrine-mapping/User.mongodb.xml | 6 +- .../config/doctrine-mapping/User.orm.xml | 6 +- src/Resources/config/doctrine.php | 2 - src/Resources/config/noop.php | 4 - .../storage-validation/mongodb/validation.xml | 4 +- .../storage-validation/orm/validation.xml | 4 +- src/Resources/config/util.php | 13 ---- src/Resources/config/validator.php | 8 -- src/Util/CanonicalFieldsUpdater.php | 25 ------- src/Util/Canonicalizer.php | 19 ----- src/Util/SimpleCanonicalFieldsUpdater.php | 54 -------------- src/Util/SimpleCanonicalizer.php | 38 ---------- src/Validator/Initializer.php | 38 ---------- .../DependencyInjection/ConfigurationTest.php | 2 - .../NucleosUserExtensionTest.php | 6 -- tests/Doctrine/BaseUserManagerTest.php | 4 +- tests/Model/BaseUserManagerTest.php | 64 ++-------------- .../Util/SimpleCanonicalFieldsUpdaterTest.php | 74 ------------------- tests/Util/SimpleCanonicalizerTest.php | 40 ---------- 33 files changed, 43 insertions(+), 543 deletions(-) create mode 100644 UPGRADE-3.0.md delete mode 100644 docs/canonicalizer.rst delete mode 100644 src/Util/CanonicalFieldsUpdater.php delete mode 100644 src/Util/Canonicalizer.php delete mode 100644 src/Util/SimpleCanonicalFieldsUpdater.php delete mode 100644 src/Util/SimpleCanonicalizer.php delete mode 100644 src/Validator/Initializer.php delete mode 100644 tests/Util/SimpleCanonicalFieldsUpdaterTest.php delete mode 100644 tests/Util/SimpleCanonicalizerTest.php diff --git a/UPGRADE-3.0.md b/UPGRADE-3.0.md new file mode 100644 index 00000000..0501b1c0 --- /dev/null +++ b/UPGRADE-3.0.md @@ -0,0 +1,19 @@ +UPGRADE FROM 2.x to 3.0 +======================= + +## Remove canonical feature + +The fields `usernameCanonical` and `emailCanonical` are removed from the `User` class in favor of using lowercase +username and password by default. You need to check your database own in order to have no duplicates after the +update. + +Because of the feature removal, the following classes were removed: + +- `Nucleos\UserBundle\Canonicalizer` +- `Nucleos\UserBundle\CanonicalFieldsUpdater` +- `Nucleos\UserBundle\SimpleCanonicalizer` +- `Nucleos\UserBundle\UtilCanonicalFieldsUpdater` + +## Deprecations + +All the deprecated code introduced on 2.x is removed on 3.0. diff --git a/docs/canonicalizer.rst b/docs/canonicalizer.rst deleted file mode 100644 index b1cbde28..00000000 --- a/docs/canonicalizer.rst +++ /dev/null @@ -1,47 +0,0 @@ -Canonicalization -================ - -NucleosUserBundle stores canonicalized versions of the username and the email -which are used when querying and checking for uniqueness. -The default implementation makes them case-insensitive to avoid having -users whose username only differs because of the case. It uses :phpfunction:`mb_convert_case` -to achieve this result. - -.. caution:: - - If you do not have the mbstring extension installed you will need to - define your own canonicalizer. - -Replacing the canonicalizers ----------------------------- - -If you want to change the way the canonical fields are populated, -create a class implementing ``Nucleos\UserBundle\Util\CanonicalizerInterface`` -and register it as a service: - -.. code-block:: yaml - - # config/services.yaml - services: - app.my_canonicalizer: - class: App\Util\CustomCanonicalizer - public: false - - -You can now configure NucleosUserBundle to use your own implementation: - -.. code-block:: yaml - - # config/packages/nucleos_user.yaml - nucleos_user: - # ... - service: - email_canonicalizer: app.my_canonicalizer - username_canonicalizer: app.my_canonicalizer - -You can of course use different services for each field if you don't want -to use the same logic. - -.. note:: - - The default implementation has the id ``nucleos_user.util.canonicalizer.simple``. diff --git a/docs/configuration_reference.rst b/docs/configuration_reference.rst index d916221f..a464d28d 100644 --- a/docs/configuration_reference.rst +++ b/docs/configuration_reference.rst @@ -20,8 +20,6 @@ All available configuration options are listed below with their default values. from_email: # Use this node only if you don't want the global email address for the resetting email service: mailer: nucleos_user.mailer.simple - email_canonicalizer: nucleos_user.util.canonicalizer.simple - username_canonicalizer: nucleos_user.util.canonicalizer.simple token_generator: nucleos_user.util.token_generator.simple user_manager: nucleos_user.user_manager.default group: diff --git a/docs/custom_storage_layer.rst b/docs/custom_storage_layer.rst index 21fe9104..c0df4529 100644 --- a/docs/custom_storage_layer.rst +++ b/docs/custom_storage_layer.rst @@ -16,7 +16,7 @@ implement these interfaces. .. note:: - You need to take care to always call ``updateCanonicalFields`` and ``updatePassword`` + You need to take care to always call ``updatePassword`` before saving a user. This is done when calling ``updateUser`` so you will be safe if you always use the user manager to save the users. If your storage layer gives you a hook in its saving process, you can use diff --git a/docs/index.rst b/docs/index.rst index 9e6d9782..f7b6cf23 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -22,7 +22,6 @@ The following documents are available: emails groups doctrine - canonicalizer custom_storage_layer security deletion diff --git a/docs/user_manager.rst b/docs/user_manager.rst index baf4cabd..b9ac44d6 100644 --- a/docs/user_manager.rst +++ b/docs/user_manager.rst @@ -59,8 +59,7 @@ users. - ``findUsers()`` To save a user object, you can use the ``updateUser`` method of the user manager. -This method will update the encoded password and the canonical fields and -then persist the changes. +This method will update the encoded password field and then persist the changes. Updating a User object ---------------------- @@ -76,7 +75,7 @@ Updating a User object .. note:: To make it easier, the bundle comes with a Doctrine listener handling - the update of the password and the canonical fields for you behind the + the update of the password fields for you behind the scenes. If you always save the user through the user manager, you may want to disable it to improve performance. @@ -92,8 +91,7 @@ Updating a User object For the Doctrine implementations, the default behavior is to flush the unit of work when calling the ``updateUser`` method. You can disable the flush by passing a second argument set to ``false``. - This will then be equivalent to calling ``updateCanonicalFields`` and - ``updatePassword``. + This will then be equivalent to calling ``updatePassword``. An ORM example: diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index ee104b96..8bb94731 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -112,9 +112,7 @@ private function addServiceSection(NodeDefinition $node): void ->addDefaultsIfNotSet() ->children() ->scalarNode('mailer')->defaultValue('nucleos_user.mailer.simple')->end() - ->scalarNode('email_canonicalizer')->defaultValue('nucleos_user.util.canonicalizer.simple')->end() ->scalarNode('token_generator')->defaultValue('nucleos_user.util.token_generator.simple')->end() - ->scalarNode('username_canonicalizer')->defaultValue('nucleos_user.util.canonicalizer.simple')->end() ->scalarNode('user_manager')->defaultValue('nucleos_user.user_manager.default')->end() ->end() ->end() diff --git a/src/DependencyInjection/NucleosUserExtension.php b/src/DependencyInjection/NucleosUserExtension.php index 91c13638..983549aa 100644 --- a/src/DependencyInjection/NucleosUserExtension.php +++ b/src/DependencyInjection/NucleosUserExtension.php @@ -86,9 +86,6 @@ public function load(array $configs, ContainerBuilder $container): void $loader->load('flash_notifications.php'); } - $container->setAlias('nucleos_user.util.email_canonicalizer', new Alias($config['service']['email_canonicalizer'], true)); - $container->setAlias('nucleos_user.util.username_canonicalizer', new Alias($config['service']['username_canonicalizer'], true)); - $container->setAlias('nucleos_user.util.token_generator', new Alias($config['service']['token_generator'], true)); $container->setAlias(TokenGenerator::class, new Alias($config['service']['token_generator'], true)); diff --git a/src/Doctrine/UserListener.php b/src/Doctrine/UserListener.php index 25610913..d5443d66 100644 --- a/src/Doctrine/UserListener.php +++ b/src/Doctrine/UserListener.php @@ -22,19 +22,15 @@ use Doctrine\Persistence\Event\LifecycleEventArgs; use Doctrine\Persistence\ObjectManager; use Nucleos\UserBundle\Model\UserInterface; -use Nucleos\UserBundle\Util\CanonicalFieldsUpdater; use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; final class UserListener implements EventSubscriber { private UserPasswordHasherInterface $userPasswordHasher; - private CanonicalFieldsUpdater $canonicalFieldsUpdater; - - public function __construct(UserPasswordHasherInterface $userPasswordHasher, CanonicalFieldsUpdater $canonicalFieldsUpdater) + public function __construct(UserPasswordHasherInterface $userPasswordHasher) { $this->userPasswordHasher = $userPasswordHasher; - $this->canonicalFieldsUpdater = $canonicalFieldsUpdater; } public function getSubscribedEvents(): array @@ -70,8 +66,6 @@ public function preUpdate(LifecycleEventArgs $args): void private function updateUserFields(UserInterface $user): void { - $this->canonicalFieldsUpdater->updateCanonicalFields($user); - if (null === $user->getPlainPassword()) { return; } diff --git a/src/Doctrine/UserManager.php b/src/Doctrine/UserManager.php index 5e603d0a..afb3879d 100644 --- a/src/Doctrine/UserManager.php +++ b/src/Doctrine/UserManager.php @@ -17,7 +17,6 @@ use Doctrine\Persistence\ObjectRepository; use Nucleos\UserBundle\Model\BaseUserManager; use Nucleos\UserBundle\Model\UserInterface; -use Nucleos\UserBundle\Util\CanonicalFieldsUpdater; final class UserManager extends BaseUserManager { @@ -31,10 +30,8 @@ final class UserManager extends BaseUserManager /** * @phpstan-param class-string $class */ - public function __construct(CanonicalFieldsUpdater $canonicalFieldsUpdater, ObjectManager $om, string $class) + public function __construct(ObjectManager $om, string $class) { - parent::__construct($canonicalFieldsUpdater); - $this->objectManager = $om; $this->class = $class; } @@ -72,8 +69,6 @@ public function reloadUser(UserInterface $user): void public function updateUser(UserInterface $user, bool $andFlush = true): void { - $this->updateCanonicalFields($user); - $this->objectManager->persist($user); if ($andFlush) { $this->objectManager->flush(); diff --git a/src/Model/BaseUserManager.php b/src/Model/BaseUserManager.php index 3af04ab2..36157f7f 100644 --- a/src/Model/BaseUserManager.php +++ b/src/Model/BaseUserManager.php @@ -13,17 +13,8 @@ namespace Nucleos\UserBundle\Model; -use Nucleos\UserBundle\Util\CanonicalFieldsUpdater; - abstract class BaseUserManager implements UserManager { - private CanonicalFieldsUpdater $canonicalFieldsUpdater; - - public function __construct(CanonicalFieldsUpdater $canonicalFieldsUpdater) - { - $this->canonicalFieldsUpdater = $canonicalFieldsUpdater; - } - public function createUser(): UserInterface { $class = $this->getClass(); @@ -33,26 +24,16 @@ public function createUser(): UserInterface public function findUserByEmail(string $email): ?UserInterface { - return $this->findUserBy(['emailCanonical' => $this->canonicalFieldsUpdater->canonicalizeEmail($email)]); + return $this->findUserBy(['email' => strtolower($email)]); } public function findUserByUsername(string $username): ?UserInterface { - return $this->findUserBy(['usernameCanonical' => $this->canonicalFieldsUpdater->canonicalizeUsername($username)]); + return $this->findUserBy(['username' => strtolower($username)]); } public function findUserByConfirmationToken(string $token): ?UserInterface { return $this->findUserBy(['confirmationToken' => $token]); } - - public function updateCanonicalFields(UserInterface $user): void - { - $this->canonicalFieldsUpdater->updateCanonicalFields($user); - } - - final protected function getCanonicalFieldsUpdater(): CanonicalFieldsUpdater - { - return $this->canonicalFieldsUpdater; - } } diff --git a/src/Model/User.php b/src/Model/User.php index 209be135..fa80bedd 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -31,12 +31,8 @@ abstract class User implements UserInterface, GroupAwareUser, LocaleAwareUser { protected ?string $username = null; - protected ?string $usernameCanonical = null; - protected ?string $email = null; - protected ?string $emailCanonical = null; - protected bool $enabled = false; protected ?string $password = null; @@ -97,21 +93,11 @@ public function getUserIdentifier(): string return $this->getUsername(); } - public function getUsernameCanonical(): string - { - return $this->usernameCanonical ?? ''; - } - public function getEmail(): string { return $this->email ?? ''; } - public function getEmailCanonical(): string - { - return $this->emailCanonical ?? ''; - } - public function getPassword(): string { return $this->password ?? ''; @@ -186,22 +172,12 @@ public function removeRole(string $role): void public function setUsername(string $username): void { - $this->username = $username; - } - - public function setUsernameCanonical(string $usernameCanonical): void - { - $this->usernameCanonical = $usernameCanonical; + $this->username = strtolower($username); } public function setEmail(string $email): void { - $this->email = $email; - } - - public function setEmailCanonical(string $emailCanonical): void - { - $this->emailCanonical = $emailCanonical; + $this->email = strtolower($email); } public function setEnabled(bool $boolean): void diff --git a/src/Model/UserInterface.php b/src/Model/UserInterface.php index 21d4a5c7..f9e905ae 100644 --- a/src/Model/UserInterface.php +++ b/src/Model/UserInterface.php @@ -40,16 +40,6 @@ public function getUsername(): string; */ public function setUsername(string $username): void; - /** - * Gets the canonical username in search and sort queries. - */ - public function getUsernameCanonical(): string; - - /** - * Sets the canonical username. - */ - public function setUsernameCanonical(string $usernameCanonical): void; - public function getEmail(): string; /** @@ -57,16 +47,6 @@ public function getEmail(): string; */ public function setEmail(string $email): void; - /** - * Gets the canonical email in search and sort queries. - */ - public function getEmailCanonical(): string; - - /** - * Sets the canonical email. - */ - public function setEmailCanonical(string $emailCanonical): void; - /** * Gets the plain password. */ diff --git a/src/Model/UserManager.php b/src/Model/UserManager.php index eab1d705..440a4fe5 100644 --- a/src/Model/UserManager.php +++ b/src/Model/UserManager.php @@ -70,9 +70,4 @@ public function reloadUser(UserInterface $user): void; * Updates a user. */ public function updateUser(UserInterface $user, bool $andFlush = true): void; - - /** - * Updates the canonical username and email fields for a user. - */ - public function updateCanonicalFields(UserInterface $user): void; } diff --git a/src/Resources/config/doctrine-mapping/User.mongodb.xml b/src/Resources/config/doctrine-mapping/User.mongodb.xml index 7f3ccf14..1f245749 100644 --- a/src/Resources/config/doctrine-mapping/User.mongodb.xml +++ b/src/Resources/config/doctrine-mapping/User.mongodb.xml @@ -2,9 +2,7 @@ - - @@ -15,11 +13,11 @@ - + - + diff --git a/src/Resources/config/doctrine-mapping/User.orm.xml b/src/Resources/config/doctrine-mapping/User.orm.xml index 365b55d4..dd9db689 100644 --- a/src/Resources/config/doctrine-mapping/User.orm.xml +++ b/src/Resources/config/doctrine-mapping/User.orm.xml @@ -1,10 +1,8 @@ - - - - + + diff --git a/src/Resources/config/doctrine.php b/src/Resources/config/doctrine.php index 4ffcea34..a4ed6f91 100644 --- a/src/Resources/config/doctrine.php +++ b/src/Resources/config/doctrine.php @@ -22,7 +22,6 @@ ->set('nucleos_user.user_manager.default', UserManager::class) ->args([ - new Reference('nucleos_user.util.canonical_fields_updater'), new Reference('nucleos_user.object_manager'), new Parameter('nucleos_user.model.user.class'), ]) @@ -36,7 +35,6 @@ ->set('nucleos_user.user_listener', UserListener::class) ->args([ new Reference('security.password_hasher'), - new Reference('nucleos_user.util.canonical_fields_updater'), ]) ; }; diff --git a/src/Resources/config/noop.php b/src/Resources/config/noop.php index b295fbbc..854291c0 100644 --- a/src/Resources/config/noop.php +++ b/src/Resources/config/noop.php @@ -13,15 +13,11 @@ use Nucleos\UserBundle\Noop\UserListener; use Nucleos\UserBundle\Noop\UserManager; -use Symfony\Component\DependencyInjection\Reference; return static function (ContainerConfigurator $container): void { $container->services() ->set('nucleos_user.user_manager.default', UserManager::class) - ->args([ - new Reference('nucleos_user.util.canonical_fields_updater'), - ]) ->set('nucleos_user.user_listener', UserListener::class) ; diff --git a/src/Resources/config/storage-validation/mongodb/validation.xml b/src/Resources/config/storage-validation/mongodb/validation.xml index e8bf3530..9c1ccea7 100644 --- a/src/Resources/config/storage-validation/mongodb/validation.xml +++ b/src/Resources/config/storage-validation/mongodb/validation.xml @@ -2,12 +2,12 @@ - + - + diff --git a/src/Resources/config/storage-validation/orm/validation.xml b/src/Resources/config/storage-validation/orm/validation.xml index de79314b..2d078e08 100644 --- a/src/Resources/config/storage-validation/orm/validation.xml +++ b/src/Resources/config/storage-validation/orm/validation.xml @@ -2,12 +2,12 @@ - + - + diff --git a/src/Resources/config/util.php b/src/Resources/config/util.php index 9b1dbe37..697bc0e9 100644 --- a/src/Resources/config/util.php +++ b/src/Resources/config/util.php @@ -12,9 +12,6 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; use Nucleos\UserBundle\Model\UserManager; -use Nucleos\UserBundle\Util\CanonicalFieldsUpdater; -use Nucleos\UserBundle\Util\SimpleCanonicalFieldsUpdater; -use Nucleos\UserBundle\Util\SimpleCanonicalizer; use Nucleos\UserBundle\Util\SimpleTokenGenerator; use Nucleos\UserBundle\Util\SimpleUserManipulator; use Nucleos\UserBundle\Util\TokenGenerator; @@ -23,8 +20,6 @@ return static function (ContainerConfigurator $container): void { $container->services() - ->set('nucleos_user.util.canonicalizer.simple', SimpleCanonicalizer::class) - ->set('nucleos_user.util.user_manipulator.simple', SimpleUserManipulator::class) ->args([ new Reference('nucleos_user.user_manager'), @@ -38,14 +33,6 @@ ->alias(TokenGenerator::class, 'nucleos_user.util.token_generator') - ->set('nucleos_user.util.canonical_fields_updater', SimpleCanonicalFieldsUpdater::class) - ->args([ - new Reference('nucleos_user.util.username_canonicalizer'), - new Reference('nucleos_user.util.email_canonicalizer'), - ]) - - ->alias(CanonicalFieldsUpdater::class, 'nucleos_user.util.canonical_fields_updater') - ->alias(UserManager::class, 'nucleos_user.user_manager') ; }; diff --git a/src/Resources/config/validator.php b/src/Resources/config/validator.php index 50480cb4..41342f78 100644 --- a/src/Resources/config/validator.php +++ b/src/Resources/config/validator.php @@ -12,18 +12,10 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; use Nucleos\UserBundle\Validator\Constraints\PatternValidator; -use Nucleos\UserBundle\Validator\Initializer; -use Symfony\Component\DependencyInjection\Reference; return static function (ContainerConfigurator $container): void { $container->services() - ->set(Initializer::class) - ->tag('validator.initializer') - ->args([ - new Reference('nucleos_user.util.canonical_fields_updater'), - ]) - ->set(PatternValidator::class) ->tag('validator.constraint_validator', []) ; diff --git a/src/Util/CanonicalFieldsUpdater.php b/src/Util/CanonicalFieldsUpdater.php deleted file mode 100644 index 3b5e7874..00000000 --- a/src/Util/CanonicalFieldsUpdater.php +++ /dev/null @@ -1,25 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Nucleos\UserBundle\Util; - -use Nucleos\UserBundle\Model\UserInterface; - -interface CanonicalFieldsUpdater -{ - public function updateCanonicalFields(UserInterface $user): void; - - public function canonicalizeEmail(?string $email): string; - - public function canonicalizeUsername(?string $username): string; -} diff --git a/src/Util/Canonicalizer.php b/src/Util/Canonicalizer.php deleted file mode 100644 index b3c1fe30..00000000 --- a/src/Util/Canonicalizer.php +++ /dev/null @@ -1,19 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Nucleos\UserBundle\Util; - -interface Canonicalizer -{ - public function canonicalize(?string $string): string; -} diff --git a/src/Util/SimpleCanonicalFieldsUpdater.php b/src/Util/SimpleCanonicalFieldsUpdater.php deleted file mode 100644 index eb9e0324..00000000 --- a/src/Util/SimpleCanonicalFieldsUpdater.php +++ /dev/null @@ -1,54 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Nucleos\UserBundle\Util; - -use Nucleos\UserBundle\Model\UserInterface; - -final class SimpleCanonicalFieldsUpdater implements CanonicalFieldsUpdater -{ - private Canonicalizer $usernameCanonicalizer; - - private Canonicalizer $emailCanonicalizer; - - public function __construct(Canonicalizer $usernameCanonicalizer, Canonicalizer $emailCanonicalizer) - { - $this->usernameCanonicalizer = $usernameCanonicalizer; - $this->emailCanonicalizer = $emailCanonicalizer; - } - - public function updateCanonicalFields(UserInterface $user): void - { - $usernameCanonical = $this->canonicalizeUsername($user->getUsername()); - - if ('' !== $usernameCanonical) { - $user->setUsernameCanonical($usernameCanonical); - } - - $emailCanonical = $this->canonicalizeEmail($user->getEmail()); - - if ('' !== $emailCanonical) { - $user->setEmailCanonical($emailCanonical); - } - } - - public function canonicalizeEmail(?string $email): string - { - return $this->emailCanonicalizer->canonicalize($email); - } - - public function canonicalizeUsername(?string $username): string - { - return $this->usernameCanonicalizer->canonicalize($username); - } -} diff --git a/src/Util/SimpleCanonicalizer.php b/src/Util/SimpleCanonicalizer.php deleted file mode 100644 index 3aa17d84..00000000 --- a/src/Util/SimpleCanonicalizer.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Nucleos\UserBundle\Util; - -final class SimpleCanonicalizer implements Canonicalizer -{ - public function canonicalize(?string $string): string - { - if (null === $string) { - return ''; - } - - $encodings = mb_detect_order(); - - if (!\is_array($encodings)) { - return $string; - } - - $encoding = mb_detect_encoding($string, $encodings, true); - - if (false !== $encoding) { - return mb_convert_case($string, MB_CASE_LOWER, $encoding); - } - - return mb_convert_case($string, MB_CASE_LOWER); - } -} diff --git a/src/Validator/Initializer.php b/src/Validator/Initializer.php deleted file mode 100644 index c3906562..00000000 --- a/src/Validator/Initializer.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Nucleos\UserBundle\Validator; - -use Nucleos\UserBundle\Model\UserInterface; -use Nucleos\UserBundle\Util\CanonicalFieldsUpdater; -use Symfony\Component\Validator\ObjectInitializerInterface; - -final class Initializer implements ObjectInitializerInterface -{ - private CanonicalFieldsUpdater $canonicalFieldsUpdater; - - public function __construct(CanonicalFieldsUpdater $canonicalFieldsUpdater) - { - $this->canonicalFieldsUpdater = $canonicalFieldsUpdater; - } - - /** - * @param object $object - */ - public function initialize($object): void - { - if ($object instanceof UserInterface) { - $this->canonicalFieldsUpdater->updateCanonicalFields($object); - } - } -} diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index b2dbb635..6c8de819 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -50,9 +50,7 @@ public function testOptions(): void ], 'service' => [ 'mailer' => 'nucleos_user.mailer.simple', - 'email_canonicalizer' => 'nucleos_user.util.canonicalizer.simple', 'token_generator' => 'nucleos_user.util.token_generator.simple', - 'username_canonicalizer' => 'nucleos_user.util.canonicalizer.simple', 'user_manager' => 'nucleos_user.user_manager.default', ], ]; diff --git a/tests/DependencyInjection/NucleosUserExtensionTest.php b/tests/DependencyInjection/NucleosUserExtensionTest.php index 5332ad19..db54fea5 100644 --- a/tests/DependencyInjection/NucleosUserExtensionTest.php +++ b/tests/DependencyInjection/NucleosUserExtensionTest.php @@ -135,8 +135,6 @@ public function testUserLoadUtilServiceWithDefaults(): void $this->assertParameter('custom_loggedin', 'nucleos_user.loggedin.route'); $this->assertAlias('nucleos_user.mailer.simple', 'nucleos_user.mailer'); - $this->assertAlias('nucleos_user.util.canonicalizer.simple', 'nucleos_user.util.email_canonicalizer'); - $this->assertAlias('nucleos_user.util.canonicalizer.simple', 'nucleos_user.util.username_canonicalizer'); } public function testUserLoadUtilService(): void @@ -144,8 +142,6 @@ public function testUserLoadUtilService(): void $this->createFullConfiguration(); $this->assertAlias('acme_my.mailer', 'nucleos_user.mailer'); - $this->assertAlias('acme_my.email_canonicalizer', 'nucleos_user.util.email_canonicalizer'); - $this->assertAlias('acme_my.username_canonicalizer', 'nucleos_user.util.username_canonicalizer'); } public function testUserLoadFlashesByDefault(): void @@ -260,8 +256,6 @@ protected function getFullConfig(): array from_email: Acme Corp service: mailer: acme_my.mailer - email_canonicalizer: acme_my.email_canonicalizer - username_canonicalizer: acme_my.username_canonicalizer user_manager: acme_my.user_manager group: group_class: Acme\MyBundle\Entity\Group diff --git a/tests/Doctrine/BaseUserManagerTest.php b/tests/Doctrine/BaseUserManagerTest.php index e31f68cb..b0b00a9e 100644 --- a/tests/Doctrine/BaseUserManagerTest.php +++ b/tests/Doctrine/BaseUserManagerTest.php @@ -18,7 +18,6 @@ use Doctrine\Persistence\ObjectRepository; use Nucleos\UserBundle\Doctrine\UserManager; use Nucleos\UserBundle\Tests\App\Entity\TestUser; -use Nucleos\UserBundle\Util\CanonicalFieldsUpdater; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -43,7 +42,6 @@ final class BaseUserManagerTest extends TestCase protected function setUp(): void { - $fieldsUpdater = $this->createMock(CanonicalFieldsUpdater::class); $class = $this->getMockBuilder(ClassMetadata::class)->getMock(); $this->om = $this->getMockBuilder(ObjectManager::class)->getMock(); $this->repository = $this->getMockBuilder(ObjectRepository::class)->getMock(); @@ -63,7 +61,7 @@ protected function setUp(): void ->willReturn(static::USER_CLASS) ; - $this->userManager = new UserManager($fieldsUpdater, $this->om, static::USER_CLASS); + $this->userManager = new UserManager($this->om, static::USER_CLASS); } public function testDeleteUser(): void diff --git a/tests/Model/BaseUserManagerTest.php b/tests/Model/BaseUserManagerTest.php index 5ccdccbe..9d76ee86 100644 --- a/tests/Model/BaseUserManagerTest.php +++ b/tests/Model/BaseUserManagerTest.php @@ -14,9 +14,6 @@ namespace Nucleos\UserBundle\Tests\Model; use Nucleos\UserBundle\Model\BaseUserManager; -use Nucleos\UserBundle\Model\GroupInterface; -use Nucleos\UserBundle\Model\User; -use Nucleos\UserBundle\Util\CanonicalFieldsUpdater; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -27,42 +24,16 @@ final class BaseUserManagerTest extends TestCase */ private $manager; - /** - * @var CanonicalFieldsUpdater&MockObject - */ - private $fieldsUpdater; - protected function setUp(): void { - $this->fieldsUpdater = $this->createMock(CanonicalFieldsUpdater::class); - - $this->manager = $this->getUserManager([ - $this->fieldsUpdater, - ]); - } - - public function testUpdateCanonicalFields(): void - { - $user = $this->getUser(); - - $this->fieldsUpdater->expects(static::once()) - ->method('updateCanonicalFields') - ->with(static::identicalTo($user)) - ; - - $this->manager->updateCanonicalFields($user); + $this->manager = $this->getUserManager([]); } public function testFindUserByUsername(): void { $this->manager->expects(static::once()) ->method('findUserBy') - ->with(static::equalTo(['usernameCanonical' => 'jack'])) - ; - $this->fieldsUpdater->expects(static::once()) - ->method('canonicalizeUsername') - ->with('jack') - ->willReturn('jack') + ->with(static::equalTo(['username' => 'jack'])) ; $this->manager->findUserByUsername('jack'); @@ -72,12 +43,7 @@ public function testFindUserByUsernameLowercasesTheUsername(): void { $this->manager->expects(static::once()) ->method('findUserBy') - ->with(static::equalTo(['usernameCanonical' => 'jack'])) - ; - $this->fieldsUpdater->expects(static::once()) - ->method('canonicalizeUsername') - ->with('JaCk') - ->willReturn('jack') + ->with(static::equalTo(['username' => 'jack'])) ; $this->manager->findUserByUsername('JaCk'); @@ -87,12 +53,7 @@ public function testFindUserByEmail(): void { $this->manager->expects(static::once()) ->method('findUserBy') - ->with(static::equalTo(['emailCanonical' => 'jack@email.org'])) - ; - $this->fieldsUpdater->expects(static::once()) - ->method('canonicalizeEmail') - ->with('jack@email.org') - ->willReturn('jack@email.org') + ->with(static::equalTo(['email' => 'jack@email.org'])) ; $this->manager->findUserByEmail('jack@email.org'); @@ -102,27 +63,12 @@ public function testFindUserByEmailLowercasesTheEmail(): void { $this->manager->expects(static::once()) ->method('findUserBy') - ->with(static::equalTo(['emailCanonical' => 'jack@email.org'])) - ; - $this->fieldsUpdater->expects(static::once()) - ->method('canonicalizeEmail') - ->with('JaCk@EmAiL.oRg') - ->willReturn('jack@email.org') + ->with(static::equalTo(['email' => 'jack@email.org'])) ; $this->manager->findUserByEmail('JaCk@EmAiL.oRg'); } - /** - * @return MockObject&User - */ - private function getUser(): MockObject - { - return $this->getMockBuilder(User::class) - ->getMockForAbstractClass() - ; - } - /** * @return BaseUserManager&MockObject */ diff --git a/tests/Util/SimpleCanonicalFieldsUpdaterTest.php b/tests/Util/SimpleCanonicalFieldsUpdaterTest.php deleted file mode 100644 index 7d8e02a6..00000000 --- a/tests/Util/SimpleCanonicalFieldsUpdaterTest.php +++ /dev/null @@ -1,74 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Nucleos\UserBundle\Tests\Util; - -use Nucleos\UserBundle\Tests\App\Entity\TestUser; -use Nucleos\UserBundle\Util\Canonicalizer; -use Nucleos\UserBundle\Util\SimpleCanonicalFieldsUpdater; -use PHPUnit\Framework\MockObject\MockObject; -use PHPUnit\Framework\TestCase; - -final class SimpleCanonicalFieldsUpdaterTest extends TestCase -{ - private SimpleCanonicalFieldsUpdater $fieldsUpdater; - - /** - * @var Canonicalizer&MockObject - */ - private $usernameCanonicalizer; - - /** - * @var Canonicalizer&MockObject - */ - private $emailCanonicalizer; - - protected function setUp(): void - { - $this->usernameCanonicalizer = $this->getMockCanonicalizer(); - $this->emailCanonicalizer = $this->getMockCanonicalizer(); - - $this->fieldsUpdater = new SimpleCanonicalFieldsUpdater($this->usernameCanonicalizer, $this->emailCanonicalizer); - } - - public function testUpdateCanonicalFields(): void - { - $user = new TestUser(); - $user->setUsername('Username'); - $user->setEmail('User@Example.com'); - - $this->usernameCanonicalizer->expects(static::once()) - ->method('canonicalize') - ->with('Username') - ->willReturnCallback('strtolower') - ; - - $this->emailCanonicalizer->expects(static::once()) - ->method('canonicalize') - ->with('User@Example.com') - ->willReturnCallback('strtolower') - ; - - $this->fieldsUpdater->updateCanonicalFields($user); - static::assertSame('username', $user->getUsernameCanonical()); - static::assertSame('user@example.com', $user->getEmailCanonical()); - } - - /** - * @return Canonicalizer&MockObject - */ - private function getMockCanonicalizer(): MockObject - { - return $this->getMockBuilder(Canonicalizer::class)->getMock(); - } -} diff --git a/tests/Util/SimpleCanonicalizerTest.php b/tests/Util/SimpleCanonicalizerTest.php deleted file mode 100644 index 5bec1c6c..00000000 --- a/tests/Util/SimpleCanonicalizerTest.php +++ /dev/null @@ -1,40 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Nucleos\UserBundle\Tests\Util; - -use Generator; -use Nucleos\UserBundle\Util\SimpleCanonicalizer; -use PHPUnit\Framework\TestCase; - -final class SimpleCanonicalizerTest extends TestCase -{ - /** - * @dataProvider canonicalizeProvider - */ - public function testCanonicalize(string $source, string $expectedResult): void - { - $canonicalizer = new SimpleCanonicalizer(); - static::assertSame($expectedResult, $canonicalizer->canonicalize($source)); - } - - /** - * @phpstan-return Generator - */ - public static function canonicalizeProvider(): Generator - { - yield ['FOO', 'foo']; - - yield [\chr(171), \PHP_VERSION_ID < 50600 ? \chr(171) : '?']; - } -}