Skip to content

Commit

Permalink
feat(user): add migration, fixtures
Browse files Browse the repository at this point in the history
see: #20
  • Loading branch information
n3wborn committed Jan 13, 2024
1 parent 20addc4 commit 72d0933
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
34 changes: 34 additions & 0 deletions migrations/Version20231101092817.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20231101092817 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add User';
}

public function up(Schema $schema): void
{
$this->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');
}
}
54 changes: 54 additions & 0 deletions src/DataFixtures/UserFixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\DataFixtures;

use App\Entity\User;
use App\Repository\ProjectRepository;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

final class UserFixtures extends Fixture implements OrderedFixtureInterface
{
public const QUANTITY = 10;

public function __construct(
private readonly ProjectRepository $projectRepository,
private readonly UserPasswordHasherInterface $userPasswordHasher
) {
}

public function load(ObjectManager $manager): void
{
$admin = $this->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('[email protected]')
->setRoles([User::ROLE_ADMIN])
->setPassword($this->userPasswordHasher->hashPassword($user, 'dev'));
}

public function getOrder(): int
{
return 3;
}
}
37 changes: 36 additions & 1 deletion src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@
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;
use Symfony\Component\Security\Core\User\UserInterface;
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
{
use ArchivableEntity;
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')]
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 72d0933

Please sign in to comment.