Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove canonical feature #702

Merged
merged 1 commit into from
Jun 11, 2023
Merged
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
19 changes: 19 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 0 additions & 47 deletions docs/canonicalizer.rst

This file was deleted.

2 changes: 0 additions & 2 deletions docs/configuration_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/custom_storage_layer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ The following documents are available:
emails
groups
doctrine
canonicalizer
custom_storage_layer
security
deletion
Expand Down
8 changes: 3 additions & 5 deletions docs/user_manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------
Expand All @@ -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.

Expand All @@ -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:

Expand Down
2 changes: 0 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 0 additions & 3 deletions src/DependencyInjection/NucleosUserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
8 changes: 1 addition & 7 deletions src/Doctrine/UserListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 1 addition & 6 deletions src/Doctrine/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -31,10 +30,8 @@ final class UserManager extends BaseUserManager
/**
* @phpstan-param class-string<UserInterface> $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;
}
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 2 additions & 21 deletions src/Model/BaseUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
}
28 changes: 2 additions & 26 deletions src/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ?? '';
Expand Down Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions src/Model/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,13 @@ 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;

/**
* Sets the email.
*/
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.
*/
Expand Down
5 changes: 0 additions & 5 deletions src/Model/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 2 additions & 4 deletions src/Resources/config/doctrine-mapping/User.mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
<mapped-superclass name="Nucleos\UserBundle\Model\User" collection="nucleos_user_user">
<field name="username" type="string"/>
<field name="usernameCanonical" type="string"/>
<field name="email" type="string"/>
<field name="emailCanonical" type="string"/>
<field name="enabled" type="boolean"/>
<field name="password" type="string"/>
<field name="lastLogin" type="date"/>
Expand All @@ -15,11 +13,11 @@
<field name="roles" type="collection"/>
<indexes>
<index>
<key name="usernameCanonical" order="asc"/>
<key name="username" order="asc"/>
<option name="unique" value="true"/>
</index>
<index>
<key name="emailCanonical" order="asc"/>
<key name="email" order="asc"/>
<option name="unique" value="true"/>
</index>
<index>
Expand Down
6 changes: 2 additions & 4 deletions src/Resources/config/doctrine-mapping/User.orm.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<mapped-superclass name="Nucleos\UserBundle\Model\User">
<field name="username" column="username" type="string" length="180"/>
<field name="usernameCanonical" column="username_canonical" type="string" length="180" unique="true"/>
<field name="email" column="email" type="string" length="180"/>
<field name="emailCanonical" column="email_canonical" type="string" length="180" unique="true"/>
<field name="username" column="username" type="string" length="180" unique="true"/>
<field name="email" column="email" type="string" length="180" unique="true"/>
<field name="enabled" column="enabled" type="boolean"/>
<field name="password" column="password" type="string"/>
<field name="lastLogin" column="last_login" type="datetime" nullable="true"/>
Expand Down
Loading