diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 73a0545..6a613e0 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -1,21 +1,22 @@ security: # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords password_hashers: - Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto' - # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider + Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: + algorithm: bcrypt + cost: 13 + providers: app_user_provider: entity: class: App\Entity\Profil property: name - users_in_memory: { memory: null } firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: lazy: true - provider: users_in_memory + provider: app_user_provider form_login: login_path: app_login check_path: app_login @@ -40,12 +41,6 @@ security: when@test: security: password_hashers: - # By default, password hashers are resource intensive and take time. This is - # important to generate secure password hashes. In tests however, secure hashes - # are not important, waste resources and increase test times. The following - # reduces the work factor to the lowest possible values. Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: - algorithm: auto + algorithm: bcrypt cost: 4 # Lowest possible value for bcrypt - time_cost: 3 # Lowest possible value for argon - memory_cost: 10 # Lowest possible value for argon diff --git a/config/services.yaml b/config/services.yaml index 2d6a76f..09f3146 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -11,6 +11,9 @@ services: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. + Symfony\Component\Serializer\Serializer: + autowire: true + # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name App\: diff --git a/migrations/Version20240611131531.php b/migrations/Version20240611131531.php new file mode 100644 index 0000000..838e4bf --- /dev/null +++ b/migrations/Version20240611131531.php @@ -0,0 +1,41 @@ +addSql('CREATE TEMPORARY TABLE __temp__post AS SELECT id, profil_id, title, text, is_dream, up_vote, down_vote FROM post'); + $this->addSql('DROP TABLE post'); + $this->addSql('CREATE TABLE post (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, profil_id INTEGER NOT NULL, title VARCHAR(255) DEFAULT NULL, text VARCHAR(512) DEFAULT NULL, is_dream BOOLEAN NOT NULL, up_vote INTEGER DEFAULT 0 NOT NULL, down_vote INTEGER DEFAULT 0 NOT NULL, CONSTRAINT FK_5A8A6C8D275ED078 FOREIGN KEY (profil_id) REFERENCES profil (id) ON UPDATE NO ACTION ON DELETE NO ACTION NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('INSERT INTO post (id, profil_id, title, text, is_dream, up_vote, down_vote) SELECT id, profil_id, title, text, is_dream, up_vote, down_vote FROM __temp__post'); + $this->addSql('DROP TABLE __temp__post'); + $this->addSql('CREATE INDEX IDX_5A8A6C8D275ED078 ON post (profil_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE TEMPORARY TABLE __temp__post AS SELECT id, profil_id, title, text, is_dream, up_vote, down_vote FROM post'); + $this->addSql('DROP TABLE post'); + $this->addSql('CREATE TABLE post (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, profil_id INTEGER NOT NULL, title VARCHAR(255) DEFAULT NULL, text VARCHAR(512) DEFAULT NULL, is_dream BOOLEAN NOT NULL, up_vote INTEGER NOT NULL, down_vote INTEGER NOT NULL, CONSTRAINT FK_5A8A6C8D275ED078 FOREIGN KEY (profil_id) REFERENCES profil (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('INSERT INTO post (id, profil_id, title, text, is_dream, up_vote, down_vote) SELECT id, profil_id, title, text, is_dream, up_vote, down_vote FROM __temp__post'); + $this->addSql('DROP TABLE __temp__post'); + $this->addSql('CREATE INDEX IDX_5A8A6C8D275ED078 ON post (profil_id)'); + } +} diff --git a/public/css/components/post.css b/public/css/components/post.css new file mode 100644 index 0000000..7c0bada --- /dev/null +++ b/public/css/components/post.css @@ -0,0 +1,56 @@ +:root { + font-family: "Helvetica", 'Courier New', Courier, monospace; +} + +#post-wrapper { + width: 70%; + background-color: #f2f2f7; + margin: 0 auto; + border: 3px solid black; + border-radius: 1rem; + padding: 20px; + margin-top: 25vh; +} + +#post-info { + display: flex; + flex-direction: row; + align-items: center; + gap: 10px; +} + +h1 { + font-size: 3em; +} + +p { + white-space: pre-wrap; + font-size: 1.2em; +} + +#comments { + h2 { + font-size: 30px; + } +} + +hr { + color: black; +} + +html { + --s: 257px; + /* control the size */ + --c1: #38476b; + --c2: #bda3b6; + + --_c: var(--c1) calc(100% - var(--s)/2) 99%, #0000; + --_g: var(--s), #0000 calc(99% - var(--s)/2), var(--_c); + background: + radial-gradient(var(--s) at 100% var(--_g)), + radial-gradient(calc(var(--s)/4) at 50% calc(100%/3), var(--_c)) var(--s) 0, + radial-gradient(var(--s) at 0% var(--_g)) 0 calc(3*var(--s)) var(--c2); + background-size: + calc(2*var(--s)) calc(9*var(--s)/4), + calc(2*var(--s)) calc(3*var(--s)/4); +} diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php new file mode 100644 index 0000000..7548d15 --- /dev/null +++ b/src/Controller/PostController.php @@ -0,0 +1,86 @@ +em = $em; + } + + # DEBUG: Ne doit pas ĂȘtre laissĂ© en production. + #[Route('/post/all', name: 'all post', methods: ['GET'])] + public function getAllPost(): Response + { + $posts = $this->em->getRepository(Post::class)->findAll(); + + return $this->render('post/all.html.twig', [ + "posts" => $posts + ]); + } + + #[Route( + '/post/{id}', + name: 'display post', + methods: ['GET'], + requirements: ['id' => '\d+'] + )] + public function getPost(int $id): Response + { + $post = $this->em->getRepository(Post::class)->find($id); + + if (!$post) { + } + + return $this->render('post/post.html.twig', [ + 'post' => $post + ]); + } + + #[Route('/post/new/', name: 'add_post', methods: ['GET', 'POST'])] + public function addPost(Request $request): Response + { + $post = new Post(); + + $form = $this->createForm(PostType::class, $post); + + + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $form = $form->getData(); + $user = $this->getUser(); + $post->setProfil($user); + + $this->em->persist($post); + $this->em->flush(); + + return new Response($user->getUserIdentifier()); + } + + return $this->render('post/new.html.twig', [ + 'form' => $form, + ]); + } + + #[Route('/post/{id}', name: 'remove_post', methods: ['DELETE'])] + public function removePost(int $id): Response + { + $post = $this->em->getRepository(Post::class)->find($id); + $this->em->remove($post); + $this->em->flush(); + + return new Response(); + } +} diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index e0138c4..690fb57 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -22,20 +22,17 @@ class RegistrationController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - // encode the plain password - $user->setPassword( - $userPasswordHasher->hashPassword( - $user, - $form->get('plainPassword')->getData() - ) + $user->setName($form->get('name')->getData()); + + $hashedPassword = $userPasswordHasher->hashPassword( + $user, + $form->get('plainPassword')->getData() ); $user->setRoles(['ROLE_USER']); - + $user->setPassword($hashedPassword); $entityManager->persist($user); $entityManager->flush(); - // do anything else you need here, like send an email - return $security->login($user, 'form_login', 'main'); } diff --git a/src/Entity/Post.php b/src/Entity/Post.php index b709ed0..7d7a2db 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -24,11 +24,11 @@ class Post #[ORM\Column] private ?bool $isDream = null; - #[ORM\Column] - private ?int $upVote = null; + #[ORM\Column(options: ["default" => 0])] + private int $upVote = 0; - #[ORM\Column] - private ?int $downVote = null; + #[ORM\Column(options: ["default" => 0])] + private int $downVote = 0; #[ORM\ManyToOne(inversedBy: 'posts')] #[ORM\JoinColumn(nullable: false)] diff --git a/src/Entity/Profil.php b/src/Entity/Profil.php index 18a493d..571fba6 100644 --- a/src/Entity/Profil.php +++ b/src/Entity/Profil.php @@ -61,6 +61,11 @@ class Profil implements UserInterface, PasswordAuthenticatedUserInterface return $this->id; } + public function setId(int $id): ?int + { + return $this->id = $id; + } + public function getName(): ?string { return $this->name; diff --git a/src/Entity/Tags.php b/src/Entity/Tags.php index 817c672..0ce2dc4 100644 --- a/src/Entity/Tags.php +++ b/src/Entity/Tags.php @@ -2,14 +2,12 @@ namespace App\Entity; -use ApiPlatform\Metadata\ApiResource; use App\Repository\TagsRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: TagsRepository::class)] -#[ApiResource] class Tags { #[ORM\Id] diff --git a/src/Form/Type/PostType.php b/src/Form/Type/PostType.php new file mode 100644 index 0000000..3578cea --- /dev/null +++ b/src/Form/Type/PostType.php @@ -0,0 +1,27 @@ +add('title', TextType::class) + ->add('text', TextareaType::class) + ->add('dream', CheckboxType::class) + // ->add('tags', ChoiceType::class, [ + // "multiple" => true + // ]) + ->add('submit', SubmitType::class) + ; + } +} diff --git a/templates/post/all.html.twig b/templates/post/all.html.twig new file mode 100644 index 0000000..13180f2 --- /dev/null +++ b/templates/post/all.html.twig @@ -0,0 +1,9 @@ +
{{ post.text }}
+
Comments
+