diff --git a/config/packages/security.yaml b/config/packages/security.yaml index fbc97f9..a5f1925 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -21,10 +21,10 @@ security: login_path: app_login check_path: app_login enable_csrf: true + default_target_path: all post logout: path: app_logout - # where to redirect after logout - # target: app_any_route + target: /login # activate different ways to authenticate # https://symfony.com/doc/current/security.html#the-firewall @@ -35,8 +35,9 @@ security: # Easy way to control access for large sections of your site # Note: Only the *first* access control that matches will be used access_control: - # - { path: ^/admin, roles: ROLE_ADMIN } - # - { path: ^/profile, roles: ROLE_USER } + #- { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY } + #- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } + #- { path: ^/logout, role: ROLE_USER} when@test: security: diff --git a/migrations/Version20240612112105.php b/migrations/Version20240612112105.php new file mode 100644 index 0000000..451d975 --- /dev/null +++ b/migrations/Version20240612112105.php @@ -0,0 +1,35 @@ +addSql('ALTER TABLE profil ADD COLUMN roles CLOB DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE TEMPORARY TABLE __temp__profil AS SELECT id, name, description, password FROM profil'); + $this->addSql('DROP TABLE profil'); + $this->addSql('CREATE TABLE profil (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(255) DEFAULT NULL, description VARCHAR(255) DEFAULT NULL, password VARCHAR(255) DEFAULT NULL)'); + $this->addSql('INSERT INTO profil (id, name, description, password) SELECT id, name, description, password FROM __temp__profil'); + $this->addSql('DROP TABLE __temp__profil'); + } +} diff --git a/migrations/Version20240612121601.php b/migrations/Version20240612121601.php new file mode 100644 index 0000000..f40fbff --- /dev/null +++ b/migrations/Version20240612121601.php @@ -0,0 +1,35 @@ +addSql('ALTER TABLE profil ADD COLUMN roles CLOB DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE TEMPORARY TABLE __temp__profil AS SELECT id, name, description, password FROM profil'); + $this->addSql('DROP TABLE profil'); + $this->addSql('CREATE TABLE profil (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(255) DEFAULT NULL, description VARCHAR(255) DEFAULT NULL, password VARCHAR(255) DEFAULT NULL)'); + $this->addSql('INSERT INTO profil (id, name, description, password) SELECT id, name, description, password FROM __temp__profil'); + $this->addSql('DROP TABLE __temp__profil'); + } +} diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 8ba7bf5..73a0396 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -21,7 +21,7 @@ class PostController extends AbstractController } # DEBUG: Ne doit pas ĂȘtre laissĂ© en production. - #[Route('/post/all', name: 'all post', methods: ['GET'])] + #[Route('/', name: 'all post', methods: ['GET'])] public function getAllPost(): Response { $posts = $this->em->getRepository(Post::class)->findAll(); @@ -52,6 +52,8 @@ class PostController extends AbstractController #[Route('/post/new/', name: 'add_post', methods: ['GET', 'POST'])] public function addPost(Request $request): Response { + $this->denyAccessUnlessGranted('IS_AUTHENTICATED'); + $post = new Post(); $form = $this->createForm(PostType::class, $post); @@ -65,8 +67,7 @@ class PostController extends AbstractController $this->em->persist($post); $this->em->flush(); - - return new Response($user->getUserIdentifier()); + return $this->redirectToRoute('display post', ['id' => $post->getId()]); } return $this->render('post/new.html.twig', [ diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index 3028b08..690fb57 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -28,8 +28,8 @@ class RegistrationController extends AbstractController $user, $form->get('plainPassword')->getData() ); + $user->setRoles(['ROLE_USER']); $user->setPassword($hashedPassword); - $entityManager->persist($user); $entityManager->flush(); diff --git a/src/Entity/Profil.php b/src/Entity/Profil.php index 1683e00..ca45e7d 100644 --- a/src/Entity/Profil.php +++ b/src/Entity/Profil.php @@ -19,6 +19,7 @@ class Profil implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\Column] private ?int $id = null; + #[ORM\Column(type: 'json', nullable: true)] private array $roles = []; #[ORM\Column(length: 255, nullable: true)] @@ -176,7 +177,7 @@ class Profil implements UserInterface, PasswordAuthenticatedUserInterface { $roles = $this->roles; // guarantee every user at least has ROLE_USER - $roles[] = 'ROLE_USER'; + // $roles[] = 'ROLE_USER'; return array_unique($roles); } @@ -184,10 +185,14 @@ class Profil implements UserInterface, PasswordAuthenticatedUserInterface public function setRoles(array $roles): self { $this->roles = $roles; - return $this; } + public function getUserIdentifier(): string + { + return $this->name; + } + public function eraseCredentials(): void { // TODO: Implement eraseCredentials() method. diff --git a/src/Repository/TagsRepository.php b/src/Repository/TagsRepository.php index 5116b48..01f3452 100644 --- a/src/Repository/TagsRepository.php +++ b/src/Repository/TagsRepository.php @@ -9,7 +9,7 @@ use Doctrine\Persistence\ManagerRegistry; /** * @extends ServiceEntityRepository */ -class agsRepository extends ServiceEntityRepository +class TagsRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry) {