From 72d09335c7cd1d6031c791745350370d0e199e0a Mon Sep 17 00:00:00 2001 From: n3wborn Date: Wed, 1 Nov 2023 10:38:21 +0100 Subject: [PATCH] feat(user): add migration, fixtures see: #20 --- migrations/Version20231101092817.php | 34 ++++++++++++++++++ src/DataFixtures/UserFixtures.php | 54 ++++++++++++++++++++++++++++ src/Entity/User.php | 37 ++++++++++++++++++- 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 migrations/Version20231101092817.php create mode 100644 src/DataFixtures/UserFixtures.php diff --git a/migrations/Version20231101092817.php b/migrations/Version20231101092817.php new file mode 100644 index 0000000..0d40349 --- /dev/null +++ b/migrations/Version20231101092817.php @@ -0,0 +1,34 @@ +addSql('CREATE TABLE "user" (id UUID NOT NULL, email VARCHAR(180) NOT NULL, roles JSON NOT NULL, password VARCHAR(255) NOT NULL, archived_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, slug VARCHAR(255) NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649E7927C74 ON "user" (email)'); + $this->addSql('COMMENT ON COLUMN "user".id IS \'(DC2Type:uuid)\''); + $this->addSql('COMMENT ON COLUMN "user".archived_at IS \'(DC2Type:datetime_immutable)\''); + $this->addSql('COMMENT ON COLUMN "user".created_at IS \'(DC2Type:datetime_immutable)\''); + $this->addSql('COMMENT ON COLUMN "user".updated_at IS \'(DC2Type:datetime_immutable)\''); + $this->addSql('ALTER TABLE category ALTER name SET NOT NULL'); + } + + public function down(Schema $schema): void + { + $this->addSql('CREATE SCHEMA public'); + $this->addSql('DROP TABLE "user"'); + $this->addSql('ALTER TABLE category ALTER name DROP NOT NULL'); + } +} diff --git a/src/DataFixtures/UserFixtures.php b/src/DataFixtures/UserFixtures.php new file mode 100644 index 0000000..2ad5250 --- /dev/null +++ b/src/DataFixtures/UserFixtures.php @@ -0,0 +1,54 @@ +createAdmin(); + $manager->persist($admin); + + for ($i = 1; $i <= self::QUANTITY; ++$i) { + $user = $this->createUser($i); + $manager->persist($user); + } + + $manager->flush(); + } + + private function createUser(int $number): User + { + return ($user = new User()) + ->setEmail("mail$number@mail.com") + ->setPassword($this->userPasswordHasher->hashPassword($user, 'dev')); + } + + private function createAdmin(): User + { + return ($user = new User()) + ->setEmail('admin@mail.com') + ->setRoles([User::ROLE_ADMIN]) + ->setPassword($this->userPasswordHasher->hashPassword($user, 'dev')); + } + + public function getOrder(): int + { + return 3; + } +} diff --git a/src/Entity/User.php b/src/Entity/User.php index f05ac36..f4aefa2 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -3,6 +3,7 @@ namespace App\Entity; use App\Repository\UserRepository; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Types\UuidType; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; @@ -10,7 +11,7 @@ use Symfony\Component\Uid\Uuid; #[ORM\Entity(repositoryClass: UserRepository::class)] -#[ORM\Table(name: 'user')] +#[ORM\Table(name: '`user`')] #[ORM\HasLifecycleCallbacks] class User implements UserInterface, PasswordAuthenticatedUserInterface { @@ -18,6 +19,13 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface use SluggableTrait; use CreatableUpdateableTrait; + public const ROLE_ADMIN = 'ROLE_ADMIN'; + public const ROLE_USER = 'ROLE_USER'; + public const ROLES = [ + self::ROLE_ADMIN => 'Administrateur', + self::ROLE_USER => 'Utilisateur', + ]; + #[ORM\Id] #[ORM\Column(type: UuidType::NAME, unique: true)] #[ORM\GeneratedValue(strategy: 'CUSTOM')] @@ -91,4 +99,31 @@ final public function eraseCredentials(): void // If you store any temporary, sensitive data on the user, clear it here // $this->plainPassword = null; } + + public function getProjects(): Collection + { + return $this->projects; + } + + public function addProject(Project $project): self + { + if (!$this->projects->contains($project)) { + $this->projects->add($project); + $project->setUser($this); + } + + return $this; + } + + public function removeProject(Project $project): self + { + if ($this->projects->removeElement($project)) { + // set the owning side to null (unless already changed) + if ($project->getUser() === $this) { + $project->setUser(null); + } + } + + return $this; + } }