From f0f720ae3e8ff9284ce27a4138eb4b7335d50764 Mon Sep 17 00:00:00 2001 From: rem Date: Fri, 7 Jun 2024 14:01:20 +0200 Subject: [PATCH 1/2] add entities and repositories --- migrations/Version20240605095530.php | 56 ++++++ src/Controller/PostController.php | 17 ++ src/Entity/Entity/.gitignore | 0 src/Entity/Entity/Commentary.php | 69 +++++++ src/Entity/Entity/Post.php | 185 ++++++++++++++++++ src/Entity/Entity/Profil.php | 176 +++++++++++++++++ src/Entity/Entity/Tags.php | 92 +++++++++ src/Repository/Repository/.gitignore | 0 .../Repository/CommentaryRepository.php | 43 ++++ src/Repository/Repository/PostRepository.php | 43 ++++ .../Repository/ProfilRepository.php | 43 ++++ src/Repository/Repository/TagsRepository.php | 43 ++++ src/services/api.php | 1 + var/data.db | Bin 0 -> 98304 bytes 14 files changed, 768 insertions(+) create mode 100644 migrations/Version20240605095530.php create mode 100644 src/Controller/PostController.php create mode 100644 src/Entity/Entity/.gitignore create mode 100644 src/Entity/Entity/Commentary.php create mode 100644 src/Entity/Entity/Post.php create mode 100644 src/Entity/Entity/Profil.php create mode 100644 src/Entity/Entity/Tags.php create mode 100644 src/Repository/Repository/.gitignore create mode 100644 src/Repository/Repository/CommentaryRepository.php create mode 100644 src/Repository/Repository/PostRepository.php create mode 100644 src/Repository/Repository/ProfilRepository.php create mode 100644 src/Repository/Repository/TagsRepository.php create mode 100644 src/services/api.php create mode 100644 var/data.db diff --git a/migrations/Version20240605095530.php b/migrations/Version20240605095530.php new file mode 100644 index 0000000..94173ef --- /dev/null +++ b/migrations/Version20240605095530.php @@ -0,0 +1,56 @@ +addSql('CREATE TABLE commentary (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, post_id INTEGER NOT NULL, profil_id INTEGER NOT NULL, text VARCHAR(255) DEFAULT NULL, CONSTRAINT FK_1CAC12CA4B89032C FOREIGN KEY (post_id) REFERENCES post (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_1CAC12CA275ED078 FOREIGN KEY (profil_id) REFERENCES profil (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_1CAC12CA4B89032C ON commentary (post_id)'); + $this->addSql('CREATE INDEX IDX_1CAC12CA275ED078 ON commentary (profil_id)'); + $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('CREATE INDEX IDX_5A8A6C8D275ED078 ON post (profil_id)'); + $this->addSql('CREATE TABLE post_tags (post_id INTEGER NOT NULL, tags_id INTEGER NOT NULL, PRIMARY KEY(post_id, tags_id), CONSTRAINT FK_A6E9F32D4B89032C FOREIGN KEY (post_id) REFERENCES post (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_A6E9F32D8D7B4FB4 FOREIGN KEY (tags_id) REFERENCES tags (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_A6E9F32D4B89032C ON post_tags (post_id)'); + $this->addSql('CREATE INDEX IDX_A6E9F32D8D7B4FB4 ON post_tags (tags_id)'); + $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('CREATE TABLE profil_profil (profil_source INTEGER NOT NULL, profil_target INTEGER NOT NULL, PRIMARY KEY(profil_source, profil_target), CONSTRAINT FK_97293BC52E75F621 FOREIGN KEY (profil_source) REFERENCES profil (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_97293BC53790A6AE FOREIGN KEY (profil_target) REFERENCES profil (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('CREATE INDEX IDX_97293BC52E75F621 ON profil_profil (profil_source)'); + $this->addSql('CREATE INDEX IDX_97293BC53790A6AE ON profil_profil (profil_target)'); + $this->addSql('CREATE TABLE tags (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(255) DEFAULT NULL, color VARCHAR(255) DEFAULT NULL)'); + $this->addSql('CREATE TABLE messenger_messages (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, body CLOB NOT NULL, headers CLOB NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at DATETIME NOT NULL --(DC2Type:datetime_immutable) + , available_at DATETIME NOT NULL --(DC2Type:datetime_immutable) + , delivered_at DATETIME DEFAULT NULL --(DC2Type:datetime_immutable) + )'); + $this->addSql('CREATE INDEX IDX_75EA56E0FB7336F0 ON messenger_messages (queue_name)'); + $this->addSql('CREATE INDEX IDX_75EA56E0E3BD61CE ON messenger_messages (available_at)'); + $this->addSql('CREATE INDEX IDX_75EA56E016BA31DB ON messenger_messages (delivered_at)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE commentary'); + $this->addSql('DROP TABLE post'); + $this->addSql('DROP TABLE post_tags'); + $this->addSql('DROP TABLE profil'); + $this->addSql('DROP TABLE profil_profil'); + $this->addSql('DROP TABLE tags'); + $this->addSql('DROP TABLE messenger_messages'); + } +} diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php new file mode 100644 index 0000000..9ca4c76 --- /dev/null +++ b/src/Controller/PostController.php @@ -0,0 +1,17 @@ +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/Entity/Entity/.gitignore b/src/Entity/Entity/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/Entity/Entity/Commentary.php b/src/Entity/Entity/Commentary.php new file mode 100644 index 0000000..f54f60c --- /dev/null +++ b/src/Entity/Entity/Commentary.php @@ -0,0 +1,69 @@ +id; + } + + public function getText(): ?string + { + return $this->text; + } + + public function setText(?string $text): static + { + $this->text = $text; + + return $this; + } + + public function getPost(): ?Post + { + return $this->post; + } + + public function setPost(?Post $post): static + { + $this->post = $post; + + return $this; + } + + public function getProfil(): ?Profil + { + return $this->profil; + } + + public function setProfil(?Profil $profil): static + { + $this->profil = $profil; + + return $this; + } +} diff --git a/src/Entity/Entity/Post.php b/src/Entity/Entity/Post.php new file mode 100644 index 0000000..b709ed0 --- /dev/null +++ b/src/Entity/Entity/Post.php @@ -0,0 +1,185 @@ + + */ + #[ORM\OneToMany(targetEntity: Commentary::class, mappedBy: 'post')] + private Collection $commentaries; + + /** + * @var Collection + */ + #[ORM\ManyToMany(targetEntity: Tags::class, inversedBy: 'posts')] + private Collection $tags; + + public function __construct() + { + $this->commentaries = new ArrayCollection(); + $this->tags = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getTitle(): ?string + { + return $this->title; + } + + public function setTitle(?string $title): static + { + $this->title = $title; + + return $this; + } + + public function getText(): ?string + { + return $this->text; + } + + public function setText(?string $text): static + { + $this->text = $text; + + return $this; + } + + public function isDream(): ?bool + { + return $this->isDream; + } + + public function setDream(bool $isDream): static + { + $this->isDream = $isDream; + + return $this; + } + + public function getUpVote(): ?int + { + return $this->upVote; + } + + public function setUpVote(int $upVote): static + { + $this->upVote = $upVote; + + return $this; + } + + public function getDownVote(): ?int + { + return $this->downVote; + } + + public function setDownVote(int $downVote): static + { + $this->downVote = $downVote; + + return $this; + } + + public function getProfil(): ?Profil + { + return $this->profil; + } + + public function setProfil(?Profil $profil): static + { + $this->profil = $profil; + + return $this; + } + + /** + * @return Collection + */ + public function getCommentaries(): Collection + { + return $this->commentaries; + } + + public function addCommentary(Commentary $commentary): static + { + if (!$this->commentaries->contains($commentary)) { + $this->commentaries->add($commentary); + $commentary->setPost($this); + } + + return $this; + } + + public function removeCommentary(Commentary $commentary): static + { + if ($this->commentaries->removeElement($commentary)) { + // set the owning side to null (unless already changed) + if ($commentary->getPost() === $this) { + $commentary->setPost(null); + } + } + + return $this; + } + + /** + * @return Collection + */ + public function getTags(): Collection + { + return $this->tags; + } + + public function addTag(Tags $tag): static + { + if (!$this->tags->contains($tag)) { + $this->tags->add($tag); + } + + return $this; + } + + public function removeTag(Tags $tag): static + { + $this->tags->removeElement($tag); + + return $this; + } +} diff --git a/src/Entity/Entity/Profil.php b/src/Entity/Entity/Profil.php new file mode 100644 index 0000000..1aabf6d --- /dev/null +++ b/src/Entity/Entity/Profil.php @@ -0,0 +1,176 @@ + + */ + #[ORM\OneToMany(targetEntity: Post::class, mappedBy: 'profil')] + private Collection $posts; + + /** + * @var Collection + */ + #[ORM\OneToMany(targetEntity: Commentary::class, mappedBy: 'profil')] + private Collection $commentaries; + + /** + * @var Collection + */ + #[ORM\ManyToMany(targetEntity: self::class, inversedBy: 'followers')] + private Collection $followers; + + public function __construct() + { + $this->posts = new ArrayCollection(); + $this->commentaries = new ArrayCollection(); + $this->followers = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(?string $name): static + { + $this->name = $name; + + return $this; + } + + public function getDescription(): ?string + { + return $this->description; + } + + public function setDescription(?string $description): static + { + $this->description = $description; + + return $this; + } + + public function getPassword(): ?string + { + return $this->password; + } + + public function setPassword(?string $password): static + { + $this->password = $password; + + return $this; + } + + /** + * @return Collection + */ + public function getPosts(): Collection + { + return $this->posts; + } + + public function addPost(Post $post): static + { + if (!$this->posts->contains($post)) { + $this->posts->add($post); + $post->setProfil($this); + } + + return $this; + } + + public function removePost(Post $post): static + { + if ($this->posts->removeElement($post)) { + // set the owning side to null (unless already changed) + if ($post->getProfil() === $this) { + $post->setProfil(null); + } + } + + return $this; + } + + /** + * @return Collection + */ + public function getCommentaries(): Collection + { + return $this->commentaries; + } + + public function addCommentary(Commentary $commentary): static + { + if (!$this->commentaries->contains($commentary)) { + $this->commentaries->add($commentary); + $commentary->setProfil($this); + } + + return $this; + } + + public function removeCommentary(Commentary $commentary): static + { + if ($this->commentaries->removeElement($commentary)) { + // set the owning side to null (unless already changed) + if ($commentary->getProfil() === $this) { + $commentary->setProfil(null); + } + } + + return $this; + } + + /** + * @return Collection + */ + public function getFollowers(): Collection + { + return $this->followers; + } + + public function addFollower(self $follower): static + { + if (!$this->followers->contains($follower)) { + $this->followers->add($follower); + } + + return $this; + } + + public function removeFollower(self $follower): static + { + $this->followers->removeElement($follower); + + return $this; + } +} diff --git a/src/Entity/Entity/Tags.php b/src/Entity/Entity/Tags.php new file mode 100644 index 0000000..817c672 --- /dev/null +++ b/src/Entity/Entity/Tags.php @@ -0,0 +1,92 @@ + + */ + #[ORM\ManyToMany(targetEntity: Post::class, mappedBy: 'tags')] + private Collection $posts; + + public function __construct() + { + $this->posts = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(?string $name): static + { + $this->name = $name; + + return $this; + } + + public function getColor(): ?string + { + return $this->color; + } + + public function setColor(?string $color): static + { + $this->color = $color; + + return $this; + } + + /** + * @return Collection + */ + public function getPosts(): Collection + { + return $this->posts; + } + + public function addPost(Post $post): static + { + if (!$this->posts->contains($post)) { + $this->posts->add($post); + $post->addTag($this); + } + + return $this; + } + + public function removePost(Post $post): static + { + if ($this->posts->removeElement($post)) { + $post->removeTag($this); + } + + return $this; + } +} diff --git a/src/Repository/Repository/.gitignore b/src/Repository/Repository/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/Repository/Repository/CommentaryRepository.php b/src/Repository/Repository/CommentaryRepository.php new file mode 100644 index 0000000..c957d36 --- /dev/null +++ b/src/Repository/Repository/CommentaryRepository.php @@ -0,0 +1,43 @@ + + */ +class CommentaryRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Commentary::class); + } + + // /** + // * @return Commentary[] Returns an array of Commentary objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('c') + // ->andWhere('c.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('c.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Commentary + // { + // return $this->createQueryBuilder('c') + // ->andWhere('c.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +} diff --git a/src/Repository/Repository/PostRepository.php b/src/Repository/Repository/PostRepository.php new file mode 100644 index 0000000..3d76065 --- /dev/null +++ b/src/Repository/Repository/PostRepository.php @@ -0,0 +1,43 @@ + + */ +class PostRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Post::class); + } + + // /** + // * @return Post[] Returns an array of Post objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('p') + // ->andWhere('p.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('p.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Post + // { + // return $this->createQueryBuilder('p') + // ->andWhere('p.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +} diff --git a/src/Repository/Repository/ProfilRepository.php b/src/Repository/Repository/ProfilRepository.php new file mode 100644 index 0000000..926958c --- /dev/null +++ b/src/Repository/Repository/ProfilRepository.php @@ -0,0 +1,43 @@ + + */ +class ProfilRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Profil::class); + } + + // /** + // * @return Profil[] Returns an array of Profil objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('p') + // ->andWhere('p.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('p.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Profil + // { + // return $this->createQueryBuilder('p') + // ->andWhere('p.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +} diff --git a/src/Repository/Repository/TagsRepository.php b/src/Repository/Repository/TagsRepository.php new file mode 100644 index 0000000..01f3452 --- /dev/null +++ b/src/Repository/Repository/TagsRepository.php @@ -0,0 +1,43 @@ + + */ +class TagsRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Tags::class); + } + + // /** + // * @return Tags[] Returns an array of Tags objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('t') + // ->andWhere('t.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('t.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Tags + // { + // return $this->createQueryBuilder('t') + // ->andWhere('t.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } +} diff --git a/src/services/api.php b/src/services/api.php new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/services/api.php @@ -0,0 +1 @@ + diff --git a/var/data.db b/var/data.db new file mode 100644 index 0000000000000000000000000000000000000000..6237051ebba684d6509e08829d5e167c640f649f GIT binary patch literal 98304 zcmeI(Pj4FO9l&uWb|7}*ILXFgb7{sW8^u}i$6ynwQifqj(PG#oWVMzyLKB#{BN_0* zIEkxPl@n#(!d~{Wub`)%D)rEpP*v)?=%uRETl)+!0|ShA+|8jQeUT9`^E~rB^ZEUr zXXXjy*_Rd5GUQjien+?DOypKXk|IxJITDFn5zkM>(|OzxZ@OC009ILKmY** z5I_I{1Q0;r|5D(IwEQ9?6?-kKZ+49X^Qf;|X0JQ=<}0H=5O31S^lmbj%qH{MY$j=c zdX&sPN@nF`{&6<@csI4YD$Yw<|64@-@jw6p1Q0*~0R#|0009ILKmdUd2|QeSu@dy( z1nlqs$G^=F(HYWJ1Q0*~0R#|0009ILKmY**5V(v0>;KDWQ5FFN5I_I{1Q0*~0R#|0 z0D*7{*uVeh`~Pspj$R{x00IagfB*srAb=009ILKmY**5I_I{1P}?Jc-==Nfe%XoZXAXvnlEP zzS(UXZ_CB6n|oPJ$>y|VDpyc4sbZmH3U=quJFvM&kX}C$V@cC4D%v?7qymGQZ{SxZQ?Q zsAvmS&9(?dLwl;#fb-{gqPRPUVw{sfw+>ps{pkCxs)GOWbOEx#7i+FrJlOQ$^5Z~M0B z6!&j&&^zt7#5noo%~*VEOZqN9)b||T^I_c+56>?5EJnw4$H-(PjEj%Xa>4$o(d3AX zjje&M#wN`!Bs4HR|%B9bnj<9>arCMDpKdlbOrT0(?xvrJ8x>i-SXXB&V8I`b! zidsbz^Q@{oQhYFE)+3WfbLXiOaVDP5bZ~K=?=r^MA>;vg{?l@I)TE3J?7x#;Mh22tN_oO$ln)V#B zHaB8siw;MOQ(WG*D>X%y=+}Lk*e&emlbN*YHgUtZ38&~x6T_OKiT5ijvG{`r(g%GM z52q9EkypJ@arTShXTP1A947;B-2I;#a-;e?vU9ex>Fpr7XW&imQFZSC6Wc!C@N(LB z;^`e;POqm2mc8wNZ$FV*jmEVHzQf8s;bE&OH#pZ1uZaxVklwgSm{sv!$Hma3@uf8Sh@lhL^|>H8(TtgI$};hf9yq@x;71cTDT}!t1AHytUj> znN6iZooBaY%zV7+$*h^3+xC6c zj;NV<|BIzqd~;L!E;c;oq?40(%bp`mwqHI|M`y7hV=m0$=@eaX3+T-#Ct$9mH>1Xm zo_rG9M$^nsXVdJY>a^yhl6O*b&+6yo@3|?(;;JgW8wI`HYgv7>YcxCNQD3(&yiKu4 z9EjJ!1-q_zLU=(zd3$^`cTz~@Q`2i07FXR6U0ki*N literal 0 HcmV?d00001 From e83a9fc647b2b0d1a49eb544b7bbed4da79eabb5 Mon Sep 17 00:00:00 2001 From: "aurian.jault" Date: Fri, 7 Jun 2024 15:19:31 +0200 Subject: [PATCH 2/2] added Security --- .env | 4 +- .gitignore | 1 + composer.json | 4 +- composer.lock | 4 +- config/packages/security.yaml | 12 ++++ config/routes/security.yaml | 2 +- src/Controller/.gitignore | 0 src/Controller/PostController.php | 17 ------ src/Controller/ProfilController.php | 18 ++++++ src/Controller/RegistrationController.php | 45 ++++++++++++++ src/Controller/SecurityController.php | 32 ++++++++++ src/Entity/{Entity => }/Commentary.php | 0 src/Entity/Entity/.gitignore | 0 src/Entity/{Entity => }/Post.php | 0 src/Entity/{Entity => }/Profil.php | 34 ++++++++++- src/Entity/{Entity => }/Tags.php | 0 src/Form/RegistrationFormType.php | 55 ++++++++++++++++++ .../{Repository => }/CommentaryRepository.php | 0 .../{Repository => }/PostRepository.php | 0 .../{Repository => }/ProfilRepository.php | 0 src/Repository/Repository/.gitignore | 0 .../{Repository => }/TagsRepository.php | 2 +- templates/profil/index.html.twig | 20 +++++++ templates/registration/register.html.twig | 19 ++++++ templates/security/login.html.twig | 42 +++++++++++++ var/data.db | Bin 98304 -> 98304 bytes 26 files changed, 285 insertions(+), 26 deletions(-) delete mode 100644 src/Controller/.gitignore delete mode 100644 src/Controller/PostController.php create mode 100644 src/Controller/ProfilController.php create mode 100644 src/Controller/RegistrationController.php create mode 100644 src/Controller/SecurityController.php rename src/Entity/{Entity => }/Commentary.php (100%) delete mode 100644 src/Entity/Entity/.gitignore rename src/Entity/{Entity => }/Post.php (100%) rename src/Entity/{Entity => }/Profil.php (81%) rename src/Entity/{Entity => }/Tags.php (100%) create mode 100644 src/Form/RegistrationFormType.php rename src/Repository/{Repository => }/CommentaryRepository.php (100%) rename src/Repository/{Repository => }/PostRepository.php (100%) rename src/Repository/{Repository => }/ProfilRepository.php (100%) delete mode 100644 src/Repository/Repository/.gitignore rename src/Repository/{Repository => }/TagsRepository.php (95%) create mode 100644 templates/profil/index.html.twig create mode 100644 templates/registration/register.html.twig create mode 100644 templates/security/login.html.twig 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 */ -class TagsRepository extends ServiceEntityRepository +class agsRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry) { diff --git a/templates/profil/index.html.twig b/templates/profil/index.html.twig new file mode 100644 index 0000000..e2c425e --- /dev/null +++ b/templates/profil/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello ProfilController!{% endblock %} + +{% block body %} + + +
+

Hello {{ controller_name }}! ✅

+ + This friendly message is coming from: +
    +
  • Your controller at /home/aurian/3eme_annee/assassymfony/fukafukashita/src/Controller/ProfilController.php
  • +
  • Your template at /home/aurian/3eme_annee/assassymfony/fukafukashita/templates/profil/index.html.twig
  • +
+
+{% endblock %} diff --git a/templates/registration/register.html.twig b/templates/registration/register.html.twig new file mode 100644 index 0000000..c4218e0 --- /dev/null +++ b/templates/registration/register.html.twig @@ -0,0 +1,19 @@ +{% extends 'base.html.twig' %} + +{% block title %}Register{% endblock %} + +{% block body %} +

Register

+ + {{ form_errors(registrationForm) }} + + {{ form_start(registrationForm) }} + {{ form_row(registrationForm.name) }} + {{ form_row(registrationForm.plainPassword, { + label: 'Password' + }) }} + {{ form_row(registrationForm.agreeTerms) }} + + + {{ form_end(registrationForm) }} +{% endblock %} diff --git a/templates/security/login.html.twig b/templates/security/login.html.twig new file mode 100644 index 0000000..ca79723 --- /dev/null +++ b/templates/security/login.html.twig @@ -0,0 +1,42 @@ +{% extends 'base.html.twig' %} + +{% block title %}Log in!{% endblock %} + +{% block body %} +
+ {% if error %} +
{{ error.messageKey|trans(error.messageData, 'security') }}
+ {% endif %} + + {% if app.user %} +
+ You are logged in as {{ app.user.userIdentifier }}, Logout +
+ {% endif %} + +

Please sign in

+ + + + + + + + {# + Uncomment this section and add a remember_me option below your firewall to activate remember me functionality. + See https://symfony.com/doc/current/security/remember_me.html + +
+ +
+ #} + + +
+{% endblock %} diff --git a/var/data.db b/var/data.db index 6237051ebba684d6509e08829d5e167c640f649f..2101675596f80503dc060017c70bb9f6c4522bdc 100644 GIT binary patch delta 901 zcmYMz&5oO96b4}2%+SUf4<1!j6Qxorb+W}6FyNhS#sfCSU-NsQcSF zbbQUz2R(bdYQ95$X`W$l4S5eC?;o$QKafVf_T+gjN-EQ4@1K_sy{{j7*n`~s zzIpbn-asJaS0q4QUH*Rg!zJJNyYW-wO#}Hk{`2B6t;5hO=-czSTz#Btytr-?*MRMa z%gEafBuCaLUIA!S&QXH0r%vE|F2{S*C2gjej3x}>n?@w2Ss*-xp_eD`1GkD-6{Z5G z>&|aTTMO^T1hY?uE#9=<1CiO9eoKSNono$qc2Metv0((;>k`;-BYp-$-+a_QM6A3) z9-<^i`Gh(3?KDqHZ4uZgyWX>fJdq3%=rK{uI}?3R-%tiVzoXDTE__;r^F82&i5p)Z z{#&)L%rbI2Yexm;MhdBJUu$MZ@>&CWyxxWqna|7d0y7v^R(6RqPC?i4v=IzLFa8f# z1>@RFLC0xq&9M47P0`Jgz7uVgvF1MO=9^IErT(qBqe`r6li@+#y{7l9Fg%B$&yNag#BUTLNj__L(9FeXv-N+3sNCqg|%YaVHOPs#byu$++b_DyuRmrWe7mrN-a)g8>cp%I`#Op T_gCxKlZW1MPKEg~sx|)w_N)>q delta 76 zcmZo@U~6b#n;^}|G*QM`kcmOBs(_UN2n0X`gT!V*feHMZ1Xv6