Merge branch 'controllers' of https://codefirst.iut.uca.fr/git/Assasymfony/Fukafukashita into profilController

pull/16/head
Corentin RICHARD 10 months ago
commit 3fed1985dc

@ -1,21 +1,22 @@
security: security:
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers: password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto' Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface:
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider algorithm: bcrypt
cost: 13
providers: providers:
app_user_provider: app_user_provider:
entity: entity:
class: App\Entity\Profil class: App\Entity\Profil
property: name property: name
users_in_memory: { memory: null }
firewalls: firewalls:
dev: dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/ pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false security: false
main: main:
lazy: true lazy: true
provider: users_in_memory provider: app_user_provider
form_login: form_login:
login_path: app_login login_path: app_login
check_path: app_login check_path: app_login
@ -40,12 +41,6 @@ security:
when@test: when@test:
security: security:
password_hashers: 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: Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface:
algorithm: auto algorithm: bcrypt
cost: 4 # Lowest possible value for bcrypt cost: 4 # Lowest possible value for bcrypt
time_cost: 3 # Lowest possible value for argon
memory_cost: 10 # Lowest possible value for argon

@ -11,6 +11,9 @@ services:
autowire: true # Automatically injects dependencies in your services. autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. 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 # makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name # this creates a service per class whose id is the fully-qualified class name
App\: App\:

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240611131531 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() 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 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)');
}
}

@ -7,25 +7,40 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Post; use App\Entity\Post;
use Symfony\Component\Serializer\Serializer; use App\Entity\Profil;
use App\Form\Type\PostType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class PostController { class PostController extends AbstractController
{
private EntityManagerInterface $em; private EntityManagerInterface $em;
private Serializer $serializer;
public function __construct(EntityManagerInterface $em, Serializer $serializer) public function __construct(EntityManagerInterface $em)
{ {
$this->em = $em; $this->em = $em;
$this->serializer = $serializer;
} }
#[Route('/post/{id}', name: 'display post', methods: ['GET'])] # 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 public function getPost(int $id): Response
{ {
$post = $this->em->getRepository(Post::class)->find($id); $post = $this->em->getRepository(Post::class)->find($id);
if(!$post) { if (!$post) {
# Error 404 page # Error 404 page
} }
@ -33,26 +48,43 @@ class PostController {
return new Response(); return new Response();
} }
#[Route('/post/', name: 'add_post', methods: ['POST'])] #[Route('/post/new/', name: 'add_post', methods: ['GET', 'POST'])]
public function addPost(Request $request) :Response public function addPost(Request $request): Response
{ {
$data = json_decode($request->getContent(), true); $post = new Post();
try { $form = $this->createForm(PostType::class, $post);
$post = $this->serializer->deserialize($data, Post::class, 'json');
} catch (\Exception) {
return new Response("Invalid JSON data", Response::HTTP_BAD_REQUEST); $form->handleRequest($request);
} if ($form->isSubmitted() && $form->isValid()) {
$form = $form->getData();
$user = $this->getUser();
$profil = new Profil();
$profil->setId(11);
$profil->setName("coucou");
$post->setProfil($user);
# Handle error on data
$this->em->persist($post); $this->em->persist($post);
$this->em->flush(); $this->em->flush();
return new Response(); return new Response($user->getUserIdentifier());
}
return $this->render('post/new.html.twig', [
'form' => $form,
]);
# Handle error on data
} }
#[Route('/post/{id}', name: 'remove_post', methods: ['DELETE'])] #[Route('/post/{id}', name: 'remove_post', methods: ['DELETE'])]
public function removePost(int $id) :Response public function removePost(int $id): Response
{ {
$postRef = $this->em->getReference('Post', $id); $postRef = $this->em->getReference('Post', $id);
$this->em->remove($postRef); $this->em->remove($postRef);

@ -22,18 +22,17 @@ class RegistrationController extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password $user->setName($form->get('name')->getData());
$user->setPassword(
$userPasswordHasher->hashPassword( $hashedPassword = $userPasswordHasher->hashPassword(
$user, $user,
$form->get('plainPassword')->getData() $form->get('plainPassword')->getData()
)
); );
$user->setPassword($hashedPassword);
$entityManager->persist($user); $entityManager->persist($user);
$entityManager->flush(); $entityManager->flush();
// do anything else you need here, like send an email
return $security->login($user, 'form_login', 'main'); return $security->login($user, 'form_login', 'main');
} }

@ -24,11 +24,11 @@ class Post
#[ORM\Column] #[ORM\Column]
private ?bool $isDream = null; private ?bool $isDream = null;
#[ORM\Column] #[ORM\Column(options: ["default" => 0])]
private ?int $upVote = null; private int $upVote = 0;
#[ORM\Column] #[ORM\Column(options: ["default" => 0])]
private ?int $downVote = null; private int $downVote = 0;
#[ORM\ManyToOne(inversedBy: 'posts')] #[ORM\ManyToOne(inversedBy: 'posts')]
#[ORM\JoinColumn(nullable: false)] #[ORM\JoinColumn(nullable: false)]

@ -60,6 +60,11 @@ class Profil implements UserInterface, PasswordAuthenticatedUserInterface
return $this->id; return $this->id;
} }
public function setId(int $id): ?int
{
return $this->id = $id;
}
public function getName(): ?string public function getName(): ?string
{ {
return $this->name; return $this->name;

@ -2,14 +2,12 @@
namespace App\Entity; namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\TagsRepository; use App\Repository\TagsRepository;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TagsRepository::class)] #[ORM\Entity(repositoryClass: TagsRepository::class)]
#[ApiResource]
class Tags class Tags
{ {
#[ORM\Id] #[ORM\Id]

@ -0,0 +1,27 @@
<?php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class PostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title', TextType::class)
->add('text', TextareaType::class)
->add('dream', CheckboxType::class)
// ->add('tags', ChoiceType::class, [
// "multiple" => true
// ])
->add('submit', SubmitType::class)
;
}
}

@ -0,0 +1,9 @@
<h1>All website posts</h1>
{% for post in posts %}
<div>
<span>Id: {{ post.id }}</span>
<span>Title: {{ post.title }}</span>
<span>Content: {{ post.text }}</span>
</div>
{% endfor %}

@ -0,0 +1 @@
{{ form(form) }}

Binary file not shown.
Loading…
Cancel
Save