From 4fc876f879e4eadf8ecd0de321ce71d7143278db Mon Sep 17 00:00:00 2001 From: AntoineGonzalez <45924026+AntoineGonzalez@users.noreply.github.com> Date: Thu, 8 Sep 2022 14:39:55 +0200 Subject: [PATCH 1/4] feature: user registration --- apps/backend/composer.json | 1 + apps/backend/composer.lock | 553 +++++++++++++++++- apps/backend/mapping/User.orm.xml | 1 - ...08082231.php => Version20220908120753.php} | 2 +- .../src/Controller/CourseController.php | 1 - .../src/Controller/RegistrationController.php | 65 ++ .../backend/src/Repository/UserRepository.php | 17 + .../src/Service/RegistrationService.php | 45 ++ apps/backend/symfony.lock | 9 + apps/backend/templates/base.html.twig | 1 + apps/backend/templates/login/index.html.twig | 4 +- .../templates/registration/index.html.twig | 20 + 12 files changed, 713 insertions(+), 6 deletions(-) rename apps/backend/migrations/{Version20220908082231.php => Version20220908120753.php} (92%) create mode 100644 apps/backend/src/Controller/RegistrationController.php create mode 100644 apps/backend/src/Repository/UserRepository.php create mode 100644 apps/backend/src/Service/RegistrationService.php create mode 100644 apps/backend/templates/registration/index.html.twig diff --git a/apps/backend/composer.json b/apps/backend/composer.json index b1b1642..0eaffb0 100644 --- a/apps/backend/composer.json +++ b/apps/backend/composer.json @@ -13,6 +13,7 @@ "symfony/console": "6.1.*", "symfony/dotenv": "6.1.*", "symfony/flex": "^2", + "symfony/form": "6.1.*", "symfony/framework-bundle": "6.1.*", "symfony/proxy-manager-bridge": "6.1.*", "symfony/runtime": "6.1.*", diff --git a/apps/backend/composer.lock b/apps/backend/composer.lock index 60445f7..1c2905f 100644 --- a/apps/backend/composer.lock +++ b/apps/backend/composer.lock @@ -4,8 +4,64 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "986f079b38b32a722111a8e27c221eb1", + "content-hash": "0d587a168602f74b16f0b4c7a3b6e97a", "packages": [ + { + "name": "brick/math", + "version": "0.10.2", + "source": { + "type": "git", + "url": "https://github.com/brick/math.git", + "reference": "459f2781e1a08d52ee56b0b1444086e038561e3f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/brick/math/zipball/459f2781e1a08d52ee56b0b1444086e038561e3f", + "reference": "459f2781e1a08d52ee56b0b1444086e038561e3f", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": "^7.4 || ^8.0" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^2.2", + "phpunit/phpunit": "^9.0", + "vimeo/psalm": "4.25.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Brick\\Math\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Arbitrary-precision arithmetic library", + "keywords": [ + "Arbitrary-precision", + "BigInteger", + "BigRational", + "arithmetic", + "bigdecimal", + "bignum", + "brick", + "math" + ], + "support": { + "issues": "https://github.com/brick/math/issues", + "source": "https://github.com/brick/math/tree/0.10.2" + }, + "funding": [ + { + "url": "https://github.com/BenMorel", + "type": "github" + } + ], + "time": "2022-08-10T22:54:19+00:00" + }, { "name": "doctrine/annotations", "version": "1.13.3", @@ -1725,6 +1781,245 @@ }, "time": "2021-07-14T16:46:02+00:00" }, + { + "name": "ramsey/collection", + "version": "1.2.2", + "source": { + "type": "git", + "url": "https://github.com/ramsey/collection.git", + "reference": "cccc74ee5e328031b15640b51056ee8d3bb66c0a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ramsey/collection/zipball/cccc74ee5e328031b15640b51056ee8d3bb66c0a", + "reference": "cccc74ee5e328031b15640b51056ee8d3bb66c0a", + "shasum": "" + }, + "require": { + "php": "^7.3 || ^8", + "symfony/polyfill-php81": "^1.23" + }, + "require-dev": { + "captainhook/captainhook": "^5.3", + "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", + "ergebnis/composer-normalize": "^2.6", + "fakerphp/faker": "^1.5", + "hamcrest/hamcrest-php": "^2", + "jangregor/phpstan-prophecy": "^0.8", + "mockery/mockery": "^1.3", + "phpspec/prophecy-phpunit": "^2.0", + "phpstan/extension-installer": "^1", + "phpstan/phpstan": "^0.12.32", + "phpstan/phpstan-mockery": "^0.12.5", + "phpstan/phpstan-phpunit": "^0.12.11", + "phpunit/phpunit": "^8.5 || ^9", + "psy/psysh": "^0.10.4", + "slevomat/coding-standard": "^6.3", + "squizlabs/php_codesniffer": "^3.5", + "vimeo/psalm": "^4.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "Ramsey\\Collection\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ben Ramsey", + "email": "ben@benramsey.com", + "homepage": "https://benramsey.com" + } + ], + "description": "A PHP library for representing and manipulating collections.", + "keywords": [ + "array", + "collection", + "hash", + "map", + "queue", + "set" + ], + "support": { + "issues": "https://github.com/ramsey/collection/issues", + "source": "https://github.com/ramsey/collection/tree/1.2.2" + }, + "funding": [ + { + "url": "https://github.com/ramsey", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/ramsey/collection", + "type": "tidelift" + } + ], + "time": "2021-10-10T03:01:02+00:00" + }, + { + "name": "ramsey/uuid", + "version": "4.4.0", + "source": { + "type": "git", + "url": "https://github.com/ramsey/uuid.git", + "reference": "373f7bacfcf3de038778ff27dcce5672ddbf4c8a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ramsey/uuid/zipball/373f7bacfcf3de038778ff27dcce5672ddbf4c8a", + "reference": "373f7bacfcf3de038778ff27dcce5672ddbf4c8a", + "shasum": "" + }, + "require": { + "brick/math": "^0.8 || ^0.9 || ^0.10", + "ext-ctype": "*", + "ext-json": "*", + "php": "^8.0", + "ramsey/collection": "^1.0" + }, + "replace": { + "rhumsaa/uuid": "self.version" + }, + "require-dev": { + "captainhook/captainhook": "^5.10", + "captainhook/plugin-composer": "^5.3", + "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", + "doctrine/annotations": "^1.8", + "ergebnis/composer-normalize": "^2.15", + "mockery/mockery": "^1.3", + "paragonie/random-lib": "^2", + "php-mock/php-mock": "^2.2", + "php-mock/php-mock-mockery": "^1.3", + "php-parallel-lint/php-parallel-lint": "^1.1", + "phpbench/phpbench": "^1.0", + "phpstan/extension-installer": "^1.0", + "phpstan/phpstan": "^0.12", + "phpstan/phpstan-mockery": "^0.12", + "phpstan/phpstan-phpunit": "^0.12", + "phpunit/phpunit": "^8.5 || ^9", + "slevomat/coding-standard": "^7.0", + "squizlabs/php_codesniffer": "^3.5", + "vimeo/psalm": "^4.9" + }, + "suggest": { + "ext-bcmath": "Enables faster math with arbitrary-precision integers using BCMath.", + "ext-ctype": "Enables faster processing of character classification using ctype functions.", + "ext-gmp": "Enables faster math with arbitrary-precision integers using GMP.", + "ext-uuid": "Enables the use of PeclUuidTimeGenerator and PeclUuidRandomGenerator.", + "paragonie/random-lib": "Provides RandomLib for use with the RandomLibAdapter", + "ramsey/uuid-doctrine": "Allows the use of Ramsey\\Uuid\\Uuid as Doctrine field type." + }, + "type": "library", + "extra": { + "captainhook": { + "force-install": true + } + }, + "autoload": { + "files": [ + "src/functions.php" + ], + "psr-4": { + "Ramsey\\Uuid\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A PHP library for generating and working with universally unique identifiers (UUIDs).", + "keywords": [ + "guid", + "identifier", + "uuid" + ], + "support": { + "issues": "https://github.com/ramsey/uuid/issues", + "source": "https://github.com/ramsey/uuid/tree/4.4.0" + }, + "funding": [ + { + "url": "https://github.com/ramsey", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/ramsey/uuid", + "type": "tidelift" + } + ], + "time": "2022-08-05T17:58:37+00:00" + }, + { + "name": "ramsey/uuid-doctrine", + "version": "1.8.1", + "source": { + "type": "git", + "url": "https://github.com/ramsey/uuid-doctrine.git", + "reference": "1a6f235ba3faf1cd9ba18daf5b54d8dc9d3bc7d0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ramsey/uuid-doctrine/zipball/1a6f235ba3faf1cd9ba18daf5b54d8dc9d3bc7d0", + "reference": "1a6f235ba3faf1cd9ba18daf5b54d8dc9d3bc7d0", + "shasum": "" + }, + "require": { + "doctrine/dbal": "^2.5 || ^3.0", + "php": "^5.4 || ^7 || ^8", + "ramsey/uuid": "^3.5 || ^4" + }, + "require-dev": { + "doctrine/orm": "^2.5", + "mockery/mockery": "^0.9.11 || ^1", + "php-parallel-lint/php-parallel-lint": "^1", + "phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5 || ^7 || ^8 || ^9", + "squizlabs/php_codesniffer": "^3.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Ramsey\\Uuid\\Doctrine\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ben Ramsey", + "email": "ben@benramsey.com", + "homepage": "https://benramsey.com" + } + ], + "description": "Use ramsey/uuid as a Doctrine field type.", + "keywords": [ + "database", + "doctrine", + "guid", + "identifier", + "uuid" + ], + "support": { + "issues": "https://github.com/ramsey/uuid-doctrine/issues", + "source": "https://github.com/ramsey/uuid-doctrine/tree/1.8.1" + }, + "funding": [ + { + "url": "https://github.com/ramsey", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/ramsey/uuid-doctrine", + "type": "tidelift" + } + ], + "time": "2022-01-15T23:54:44+00:00" + }, { "name": "symfony/cache", "version": "v6.1.3", @@ -2840,6 +3135,108 @@ ], "time": "2022-08-07T09:39:47+00:00" }, + { + "name": "symfony/form", + "version": "v6.1.4", + "source": { + "type": "git", + "url": "https://github.com/symfony/form.git", + "reference": "0a1a3ea071a216e2902cebe0b47750ca51f12f27" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/form/zipball/0a1a3ea071a216e2902cebe0b47750ca51f12f27", + "reference": "0a1a3ea071a216e2902cebe0b47750ca51f12f27", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "symfony/deprecation-contracts": "^2.1|^3", + "symfony/event-dispatcher": "^5.4|^6.0", + "symfony/options-resolver": "^5.4|^6.0", + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-intl-icu": "^1.21", + "symfony/polyfill-mbstring": "~1.0", + "symfony/property-access": "^5.4|^6.0", + "symfony/service-contracts": "^1.1|^2|^3" + }, + "conflict": { + "phpunit/phpunit": "<5.4.3", + "symfony/console": "<5.4", + "symfony/dependency-injection": "<5.4", + "symfony/doctrine-bridge": "<5.4", + "symfony/error-handler": "<5.4", + "symfony/framework-bundle": "<5.4", + "symfony/http-kernel": "<5.4", + "symfony/translation": "<5.4", + "symfony/translation-contracts": "<1.1.7", + "symfony/twig-bridge": "<5.4" + }, + "require-dev": { + "doctrine/collections": "~1.0", + "symfony/config": "^5.4|^6.0", + "symfony/console": "^5.4|^6.0", + "symfony/dependency-injection": "^5.4|^6.0", + "symfony/expression-language": "^5.4|^6.0", + "symfony/html-sanitizer": "^6.1", + "symfony/http-foundation": "^5.4|^6.0", + "symfony/http-kernel": "^5.4|^6.0", + "symfony/intl": "^5.4|^6.0", + "symfony/security-csrf": "^5.4|^6.0", + "symfony/translation": "^5.4|^6.0", + "symfony/uid": "^5.4|^6.0", + "symfony/validator": "^5.4|^6.0", + "symfony/var-dumper": "^5.4|^6.0" + }, + "suggest": { + "symfony/security-csrf": "For protecting forms against CSRF attacks.", + "symfony/twig-bridge": "For templating with Twig.", + "symfony/validator": "For form validation." + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Form\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Allows to easily create, process and reuse HTML forms", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/form/tree/v6.1.4" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-08-09T09:57:18+00:00" + }, { "name": "symfony/framework-bundle", "version": "v6.1.4", @@ -3176,6 +3573,73 @@ ], "time": "2022-08-26T14:50:30+00:00" }, + { + "name": "symfony/options-resolver", + "version": "v6.1.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/options-resolver.git", + "reference": "a3016f5442e28386ded73c43a32a5b68586dd1c4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/options-resolver/zipball/a3016f5442e28386ded73c43a32a5b68586dd1c4", + "reference": "a3016f5442e28386ded73c43a32a5b68586dd1c4", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "symfony/deprecation-contracts": "^2.1|^3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\OptionsResolver\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides an improved replacement for the array_replace PHP function", + "homepage": "https://symfony.com", + "keywords": [ + "config", + "configuration", + "options" + ], + "support": { + "source": "https://github.com/symfony/options-resolver/tree/v6.1.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-02-25T11:15:52+00:00" + }, { "name": "symfony/password-hasher", "version": "v6.1.3", @@ -3329,6 +3793,93 @@ ], "time": "2022-05-24T11:49:31+00:00" }, + { + "name": "symfony/polyfill-intl-icu", + "version": "v1.26.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-intl-icu.git", + "reference": "e407643d610e5f2c8a4b14189150f68934bf5e48" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-intl-icu/zipball/e407643d610e5f2c8a4b14189150f68934bf5e48", + "reference": "e407643d610e5f2c8a4b14189150f68934bf5e48", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "suggest": { + "ext-intl": "For best performance and support of other locales than \"en\"" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.26-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Intl\\Icu\\": "" + }, + "classmap": [ + "Resources/stubs" + ], + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for intl's ICU-related data and classes", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "icu", + "intl", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-intl-icu/tree/v1.26.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-05-24T11:49:31+00:00" + }, { "name": "symfony/polyfill-intl-normalizer", "version": "v1.26.0", diff --git a/apps/backend/mapping/User.orm.xml b/apps/backend/mapping/User.orm.xml index fa22543..8284643 100644 --- a/apps/backend/mapping/User.orm.xml +++ b/apps/backend/mapping/User.orm.xml @@ -12,7 +12,6 @@ type="uuid" column="id" > - createFormBuilder() + ->add('email', EmailType::class, [ + 'label' => "Adresse mail", + 'required' => true, + ]) + ->add('username', TextType::class, [ + 'required' => true, + 'label' => "Nom d'utilisateur" + ]) + ->add('password', RepeatedType::class, [ + 'type' => PasswordType::class, + 'invalid_message' => 'Les mots de passes doivent être identique.', + 'options' => ['attr' => ['class' => 'password-field']], + 'required' => true, + 'first_options' => ['label' => 'Mot de passe'], + 'second_options' => ['label' => 'Confirmation du mot de passe'], + ]) + ->add('save', SubmitType::class, ['label' => 'S\'inscrire !']) + ->getForm(); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + try { + $registrationService->registerUser((array) $form->getData()); + } catch (\InvalidArgumentException $exception) { + $errorMessage = $exception->getMessage(); + + return $this->renderForm('registration/index.html.twig', [ + 'form' => $form, + 'errorMessage' => $errorMessage + ]); + } + + + return $this->redirectToRoute('app_login'); + } + + return $this->renderForm('registration/index.html.twig', [ + 'form' => $form, + 'errorMessage' => null + ]); + } +} diff --git a/apps/backend/src/Repository/UserRepository.php b/apps/backend/src/Repository/UserRepository.php new file mode 100644 index 0000000..761e4b9 --- /dev/null +++ b/apps/backend/src/Repository/UserRepository.php @@ -0,0 +1,17 @@ +userRepository->findBy(['email' => $userData['email']]); + + if (sizeof($users) > 0) { + throw new InvalidArgumentException('The User email is already used'); + } + + $user = new User( + $userData['username'], + $userData['email'], + ); + + $hashedPassword = $this->passwordHasher->hashPassword( + $user, + $userData['password'] + ); + + $user->setPassword($hashedPassword); + + $this->entityManager->persist($user); + $this->entityManager->flush($user); + } +} diff --git a/apps/backend/symfony.lock b/apps/backend/symfony.lock index 18c6f1d..ee4b1c0 100644 --- a/apps/backend/symfony.lock +++ b/apps/backend/symfony.lock @@ -60,6 +60,15 @@ "config/packages/nelmio_alice.yaml" ] }, + "ramsey/uuid-doctrine": { + "version": "1.8", + "recipe": { + "repo": "github.com/symfony/recipes-contrib", + "branch": "main", + "version": "1.3", + "ref": "471aed0fbf5620b8d7f92b7a5ebbbf6c0945c27a" + } + }, "symfony/console": { "version": "6.1", "recipe": { diff --git a/apps/backend/templates/base.html.twig b/apps/backend/templates/base.html.twig index bb89937..06123f1 100644 --- a/apps/backend/templates/base.html.twig +++ b/apps/backend/templates/base.html.twig @@ -30,6 +30,7 @@
  • Déconnexion
  • {% else %}
  • Connexion
  • +
  • Inscrition
  • {% endif %} diff --git a/apps/backend/templates/login/index.html.twig b/apps/backend/templates/login/index.html.twig index 89c3f7f..62ab986 100644 --- a/apps/backend/templates/login/index.html.twig +++ b/apps/backend/templates/login/index.html.twig @@ -9,9 +9,9 @@ {% if error %} -
    +

    {{ error.messageKey|trans(error.messageData, 'security') }} -

    +

    {% endif %}
    diff --git a/apps/backend/templates/registration/index.html.twig b/apps/backend/templates/registration/index.html.twig new file mode 100644 index 0000000..21f7223 --- /dev/null +++ b/apps/backend/templates/registration/index.html.twig @@ -0,0 +1,20 @@ +{% extends "base.html.twig" %} + +{% block title %} + Sign in +{% endblock %} +{% block main %} +

    + Sign in +

    + +
    + + {{ form(form) }} + + {% if errorMessage %} +

    + {{errorMessage}} +

    + {% endif %} +{% endblock %} From ff9eadf632bdb0fdeb700c2a5685fa92e5dded1d Mon Sep 17 00:00:00 2001 From: AntoineGonzalez <45924026+AntoineGonzalez@users.noreply.github.com> Date: Thu, 8 Sep 2022 17:03:23 +0200 Subject: [PATCH 2/4] refactoring: use findOneBy instead findOne method to check if user email already exists --- apps/backend/src/Service/RegistrationService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/backend/src/Service/RegistrationService.php b/apps/backend/src/Service/RegistrationService.php index db13f70..0f05b93 100644 --- a/apps/backend/src/Service/RegistrationService.php +++ b/apps/backend/src/Service/RegistrationService.php @@ -21,9 +21,9 @@ public function __construct( public function registerUser(array $userData) { - $users = $this->userRepository->findBy(['email' => $userData['email']]); + $user = $this->userRepository->findOneBy(['email' => $userData['email']]); - if (sizeof($users) > 0) { + if ($user) { throw new InvalidArgumentException('The User email is already used'); } From 615e9e20a250b9c9694a44d9f1a2e3c2ba654a5b Mon Sep 17 00:00:00 2001 From: AntoineGonzalez <45924026+AntoineGonzalez@users.noreply.github.com> Date: Fri, 9 Sep 2022 11:55:31 +0200 Subject: [PATCH 3/4] refactoring: Use Form type and move user password attributes in constructor --- apps/backend/fixtures/fixtures.yml | 19 ++++------ .../src/Controller/RegistrationController.php | 27 +------------ apps/backend/src/Entity/User.php | 8 +--- .../backend/src/FormType/RegistrationType.php | 38 +++++++++++++++++++ 4 files changed, 50 insertions(+), 42 deletions(-) create mode 100644 apps/backend/src/FormType/RegistrationType.php diff --git a/apps/backend/fixtures/fixtures.yml b/apps/backend/fixtures/fixtures.yml index 31fe840..b0070de 100644 --- a/apps/backend/fixtures/fixtures.yml +++ b/apps/backend/fixtures/fixtures.yml @@ -1,16 +1,13 @@ App\Entity\User: user{1..10}: __construct: - username: '' - email (unique): '' - __calls: - # password - - setPassword: ['\$2y\$10\$ci00VRrjkFz7uhyCRoKiFe8bcoPbbTQOcfUM0s918w7EZz9w0j.G2'] + username: "" + email (unique): "" + # plain : password + password: '\$2y\$10\$ci00VRrjkFz7uhyCRoKiFe8bcoPbbTQOcfUM0s918w7EZz9w0j.G2' user: __construct: - username: 'user' - email (unique): 'user@knplabs.com' - __calls: - # password - - setPassword: ['\$2y\$10\$ci00VRrjkFz7uhyCRoKiFe8bcoPbbTQOcfUM0s918w7EZz9w0j.G2'] - + username: "user" + email (unique): "user@knplabs.com" + # plain : password + password: '\$2y\$10\$ci00VRrjkFz7uhyCRoKiFe8bcoPbbTQOcfUM0s918w7EZz9w0j.G2' diff --git a/apps/backend/src/Controller/RegistrationController.php b/apps/backend/src/Controller/RegistrationController.php index 2bc1906..6e87b9f 100644 --- a/apps/backend/src/Controller/RegistrationController.php +++ b/apps/backend/src/Controller/RegistrationController.php @@ -4,14 +4,10 @@ namespace App\Controller; +use App\FormType\RegistrationType; use App\Service\RegistrationService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; -use Symfony\Component\Form\Extension\Core\Type\EmailType; -use Symfony\Component\Form\Extension\Core\Type\PasswordType; -use Symfony\Component\Form\Extension\Core\Type\RepeatedType; -use Symfony\Component\Form\Extension\Core\Type\SubmitType; -use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\HttpFoundation\Request; class RegistrationController extends AbstractController @@ -19,26 +15,7 @@ class RegistrationController extends AbstractController #[Route('/register', name: 'app_register')] public function index(Request $request, RegistrationService $registrationService) { - $form = $this->createFormBuilder() - ->add('email', EmailType::class, [ - 'label' => "Adresse mail", - 'required' => true, - ]) - ->add('username', TextType::class, [ - 'required' => true, - 'label' => "Nom d'utilisateur" - ]) - ->add('password', RepeatedType::class, [ - 'type' => PasswordType::class, - 'invalid_message' => 'Les mots de passes doivent être identique.', - 'options' => ['attr' => ['class' => 'password-field']], - 'required' => true, - 'first_options' => ['label' => 'Mot de passe'], - 'second_options' => ['label' => 'Confirmation du mot de passe'], - ]) - ->add('save', SubmitType::class, ['label' => 'S\'inscrire !']) - ->getForm(); - + $form = $this->createForm(RegistrationType::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { diff --git a/apps/backend/src/Entity/User.php b/apps/backend/src/Entity/User.php index cba7a09..166b292 100644 --- a/apps/backend/src/Entity/User.php +++ b/apps/backend/src/Entity/User.php @@ -11,11 +11,12 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface { private readonly Uuid $id; - private string $password; + public function __construct( private string $username, private string $email, + private string $password, private array $roles = [] ) { $this->id = Uuid::v4(); @@ -50,11 +51,6 @@ public function getPassword(): string return $this->password; } - public function setPassword(string $password): void - { - $this->password = $password; - } - /** * @see UserInterface */ diff --git a/apps/backend/src/FormType/RegistrationType.php b/apps/backend/src/FormType/RegistrationType.php new file mode 100644 index 0000000..a482fd5 --- /dev/null +++ b/apps/backend/src/FormType/RegistrationType.php @@ -0,0 +1,38 @@ +add('email', EmailType::class, [ + 'label' => "Adresse mail", + 'required' => true, + ]) + ->add('username', TextType::class, [ + 'required' => true, + 'label' => "Nom d'utilisateur" + ]) + ->add('password', RepeatedType::class, [ + 'type' => PasswordType::class, + 'invalid_message' => 'Les mots de passes doivent être identique.', + 'options' => ['attr' => ['class' => 'password-field']], + 'required' => true, + 'first_options' => ['label' => 'Mot de passe'], + 'second_options' => ['label' => 'Confirmation du mot de passe'], + ]) + ->add('save', SubmitType::class, ['label' => 'S\'inscrire !']); + } +} From 01b6fb07283afd9ed781d0c9644a0a7b73d60a18 Mon Sep 17 00:00:00 2001 From: AntoineGonzalez <45924026+AntoineGonzalez@users.noreply.github.com> Date: Fri, 9 Sep 2022 14:13:25 +0200 Subject: [PATCH 4/4] refactoring: remove RegistrationService --- .../src/Controller/RegistrationController.php | 36 ++++++++++++--- .../src/Service/RegistrationService.php | 45 ------------------- 2 files changed, 31 insertions(+), 50 deletions(-) delete mode 100644 apps/backend/src/Service/RegistrationService.php diff --git a/apps/backend/src/Controller/RegistrationController.php b/apps/backend/src/Controller/RegistrationController.php index 6e87b9f..046a002 100644 --- a/apps/backend/src/Controller/RegistrationController.php +++ b/apps/backend/src/Controller/RegistrationController.php @@ -4,23 +4,50 @@ namespace App\Controller; +use App\Entity\User; use App\FormType\RegistrationType; -use App\Service\RegistrationService; +use App\Repository\UserRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface; +use InvalidArgumentException; class RegistrationController extends AbstractController { #[Route('/register', name: 'app_register')] - public function index(Request $request, RegistrationService $registrationService) - { + public function register( + Request $request, + UserRepository $userRepository, + EntityManagerInterface $entityManager, + PasswordHasherFactoryInterface $passwordHasherFactory + ): Response { $form = $this->createForm(RegistrationType::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $data = $form->getData(); + try { - $registrationService->registerUser((array) $form->getData()); + $user = $userRepository->findOneBy(['email' => $data['email']]); + + if ($user) { + throw new InvalidArgumentException('The User email is already used'); + } + + $passwordHasher = $passwordHasherFactory->getPasswordHasher(User::class); + $passwordHash = $passwordHasher->hash($data['password']); + + $user = new User( + $data['username'], + $data['email'], + $passwordHash + ); + + $entityManager->persist($user); + $entityManager->flush($user); } catch (\InvalidArgumentException $exception) { $errorMessage = $exception->getMessage(); @@ -30,7 +57,6 @@ public function index(Request $request, RegistrationService $registrationService ]); } - return $this->redirectToRoute('app_login'); } diff --git a/apps/backend/src/Service/RegistrationService.php b/apps/backend/src/Service/RegistrationService.php deleted file mode 100644 index 0f05b93..0000000 --- a/apps/backend/src/Service/RegistrationService.php +++ /dev/null @@ -1,45 +0,0 @@ -userRepository->findOneBy(['email' => $userData['email']]); - - if ($user) { - throw new InvalidArgumentException('The User email is already used'); - } - - $user = new User( - $userData['username'], - $userData['email'], - ); - - $hashedPassword = $this->passwordHasher->hashPassword( - $user, - $userData['password'] - ); - - $user->setPassword($hashedPassword); - - $this->entityManager->persist($user); - $this->entityManager->flush($user); - } -}