diff --git a/.env b/.env index f81e782..6e5135c 100644 --- a/.env +++ b/.env @@ -23,10 +23,10 @@ APP_SECRET=5e7ed9de1fd633f917d0e87e2e05f923 # Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url # IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml # -# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db" +DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db" # DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=8.0.32&charset=utf8mb4" # DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4" -DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8" +# DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8" ###< doctrine/doctrine-bundle ### ###> symfony/messenger ### diff --git a/.gitignore b/.gitignore index 13ff59d..0658310 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,4 @@ # Embedded web-server pid file /.web-server-pid +.idea diff --git a/composer.json b/composer.json index dbb68cb..759ce3c 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "minimum-stability": "stable", "prefer-stable": true, "require": { - "php": ">=8.2", + "php": ">=8.0", "ext-ctype": "*", "ext-iconv": "*", "doctrine/dbal": "^3", @@ -100,7 +100,7 @@ "symfony/browser-kit": "7.0.7", "symfony/css-selector": "7.0.7", "symfony/debug-bundle": "7.0.7", - "symfony/maker-bundle": "^1.0", + "symfony/maker-bundle": "^1.59", "symfony/phpunit-bridge": "^7.0", "symfony/stopwatch": "7.0.7", "symfony/web-profiler-bundle": "7.0.7" diff --git a/composer.lock b/composer.lock index 9c94d3e..d220368 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b12441d7e7daa91f6af1b31decf59970", + "content-hash": "23d2c64b5bd955feec6a3a3d7dd6a841", "packages": [ { "name": "composer/semver", @@ -9725,7 +9725,7 @@ "prefer-stable": true, "prefer-lowest": false, "platform": { - "php": ">=8.2", + "php": ">=8.0", "ext-ctype": "*", "ext-iconv": "*" }, diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 367af25..288ddbd 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -4,6 +4,10 @@ security: Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto' # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider providers: + app_user_provider: + entity: + class: App\Entity\Profil + property: name users_in_memory: { memory: null } firewalls: dev: @@ -12,6 +16,14 @@ security: main: lazy: true provider: users_in_memory + form_login: + login_path: app_login + check_path: app_login + enable_csrf: true + logout: + path: app_logout + # where to redirect after logout + # target: app_any_route # activate different ways to authenticate # https://symfony.com/doc/current/security.html#the-firewall diff --git a/config/routes/security.yaml b/config/routes/security.yaml index f853be1..60a7afb 100644 --- a/config/routes/security.yaml +++ b/config/routes/security.yaml @@ -1,3 +1,3 @@ _security_logout: resource: security.route_loader.logout - type: service + type: service \ No newline at end of file diff --git a/src/Controller/.gitignore b/src/Controller/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php deleted file mode 100644 index 9ca4c76..0000000 --- a/src/Controller/PostController.php +++ /dev/null @@ -1,17 +0,0 @@ -namespace App\Controller; - -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Routing\Attribute\Route; - -class PostController -{ - #[Route('/lucky/number/{max}', name: 'app_lucky_number')] - public function number(int $max): Response - { - $number = random_int(0, $max); - - return new Response( - '
Lucky number: '.$number.'' - ); - } -} diff --git a/src/Controller/ProfilController.php b/src/Controller/ProfilController.php new file mode 100644 index 0000000..34d6be1 --- /dev/null +++ b/src/Controller/ProfilController.php @@ -0,0 +1,18 @@ +render('profil/index.html.twig', [ + 'controller_name' => 'ProfilController', + ]); + } +} diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php new file mode 100644 index 0000000..8c26f0b --- /dev/null +++ b/src/Controller/RegistrationController.php @@ -0,0 +1,45 @@ +createForm(RegistrationFormType::class, $user); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + // encode the plain password + $user->setPassword( + $userPasswordHasher->hashPassword( + $user, + $form->get('plainPassword')->getData() + ) + ); + + $entityManager->persist($user); + $entityManager->flush(); + + // do anything else you need here, like send an email + + return $security->login($user, 'form_login', 'main'); + } + + return $this->render('registration/register.html.twig', [ + 'registrationForm' => $form, + ]); + } +} diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php new file mode 100644 index 0000000..76bf5c4 --- /dev/null +++ b/src/Controller/SecurityController.php @@ -0,0 +1,32 @@ +getLastAuthenticationError(); + + // last username entered by the user + $lastUsername = $authenticationUtils->getLastUsername(); + + return $this->render('security/login.html.twig', [ + 'last_username' => $lastUsername, + 'error' => $error, + ]); + } + + #[Route(path: '/logout', name: 'app_logout')] + public function logout(): void + { + throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.'); + } +} diff --git a/src/Entity/Entity/Commentary.php b/src/Entity/Commentary.php similarity index 100% rename from src/Entity/Entity/Commentary.php rename to src/Entity/Commentary.php diff --git a/src/Entity/Entity/.gitignore b/src/Entity/Entity/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/src/Entity/Entity/Post.php b/src/Entity/Post.php similarity index 100% rename from src/Entity/Entity/Post.php rename to src/Entity/Post.php diff --git a/src/Entity/Entity/Profil.php b/src/Entity/Profil.php similarity index 81% rename from src/Entity/Entity/Profil.php rename to src/Entity/Profil.php index 1aabf6d..4647d0e 100644 --- a/src/Entity/Entity/Profil.php +++ b/src/Entity/Profil.php @@ -6,15 +6,21 @@ use App\Repository\ProfilRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; +use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; +use Symfony\Component\Security\Core\User\UserInterface; #[ORM\Entity(repositoryClass: ProfilRepository::class)] -class Profil +#[UniqueEntity(fields: ['name'], message: 'There is already an account with this name')] +class Profil implements UserInterface, PasswordAuthenticatedUserInterface { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; + private array $roles = []; + #[ORM\Column(length: 255, nullable: true)] private ?string $name = null; @@ -173,4 +179,30 @@ class Profil return $this; } + + public function getRoles(): array + { + $roles = $this->roles; + // guarantee every user at least has ROLE_USER + $roles[] = 'ROLE_USER'; + + return array_unique($roles); + } + + public function setRoles(array $roles): self + { + $this->roles = $roles; + + return $this; + } + + public function eraseCredentials(): void + { + // TODO: Implement eraseCredentials() method. + } + + public function getUserIdentifier(): string + { + return $this->name; + } } diff --git a/src/Entity/Entity/Tags.php b/src/Entity/Tags.php similarity index 100% rename from src/Entity/Entity/Tags.php rename to src/Entity/Tags.php diff --git a/src/Form/RegistrationFormType.php b/src/Form/RegistrationFormType.php new file mode 100644 index 0000000..b121f26 --- /dev/null +++ b/src/Form/RegistrationFormType.php @@ -0,0 +1,55 @@ +add('name') + ->add('agreeTerms', CheckboxType::class, [ + 'mapped' => false, + 'constraints' => [ + new IsTrue([ + 'message' => 'You should agree to our terms.', + ]), + ], + ]) + ->add('plainPassword', PasswordType::class, [ + // instead of being set onto the object directly, + // this is read and encoded in the controller + 'mapped' => false, + 'attr' => ['autocomplete' => 'new-password'], + 'constraints' => [ + new NotBlank([ + 'message' => 'Please enter a password', + ]), + new Length([ + 'min' => 6, + 'minMessage' => 'Your password should be at least {{ limit }} characters', + // max length allowed by Symfony for security reasons + 'max' => 4096, + ]), + ], + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Profil::class, + ]); + } +} diff --git a/src/Repository/Repository/CommentaryRepository.php b/src/Repository/CommentaryRepository.php similarity index 100% rename from src/Repository/Repository/CommentaryRepository.php rename to src/Repository/CommentaryRepository.php diff --git a/src/Repository/Repository/PostRepository.php b/src/Repository/PostRepository.php similarity index 100% rename from src/Repository/Repository/PostRepository.php rename to src/Repository/PostRepository.php diff --git a/src/Repository/Repository/ProfilRepository.php b/src/Repository/ProfilRepository.php similarity index 100% rename from src/Repository/Repository/ProfilRepository.php rename to src/Repository/ProfilRepository.php diff --git a/src/Repository/Repository/.gitignore b/src/Repository/Repository/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/src/Repository/Repository/TagsRepository.php b/src/Repository/TagsRepository.php similarity index 95% rename from src/Repository/Repository/TagsRepository.php rename to src/Repository/TagsRepository.php index 01f3452..5116b48 100644 --- a/src/Repository/Repository/TagsRepository.php +++ b/src/Repository/TagsRepository.php @@ -9,7 +9,7 @@ use Doctrine\Persistence\ManagerRegistry; /** * @extends ServiceEntityRepository/home/aurian/3eme_annee/assassymfony/fukafukashita/src/Controller/ProfilController.php
/home/aurian/3eme_annee/assassymfony/fukafukashita/templates/profil/index.html.twig