Browse Source

Ajout pseudo

Sangfroid 2 months ago
parent
commit
ab324e92b3
3 changed files with 49 additions and 2 deletions
  1. 31 0
      migrations/Version20241029200713.php
  2. 1 0
      src/Command/BlogUserCreateCommand.php
  3. 17 2
      src/Entity/User.php

+ 31 - 0
migrations/Version20241029200713.php

@@ -0,0 +1,31 @@
+<?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 Version20241029200713 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('ALTER TABLE user ADD pseudo VARCHAR(64) DEFAULT NULL');
+    }
+
+    public function down(Schema $schema): void
+    {
+        // this down() migration is auto-generated, please modify it to your needs
+        $this->addSql('ALTER TABLE user DROP pseudo');
+    }
+}

+ 1 - 0
src/Command/BlogUserCreateCommand.php

@@ -37,6 +37,7 @@ class BlogUserCreateCommand extends Command
         $user = new User();
 
         $user->setUsername($io->ask('Username'));
+        $user->setPseudo($io->ask('Pseudo'));
         $user->setEmail($io->ask('Email'));
         do {
             $password = $io->askHidden('Mot de passe');

+ 17 - 2
src/Entity/User.php

@@ -42,6 +42,9 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
     #[ORM\OneToMany(targetEntity: Article::class, mappedBy: 'author', orphanRemoval: true)]
     private Collection $articles;
 
+    #[ORM\Column(length: 64, nullable: true)]
+    private ?string $pseudo = null;
+
     public function __construct()
     {
         $this->articles = new ArrayCollection();
@@ -49,7 +52,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
 
     public function __toString(): string
     {
-        return $this->getUserIdentifier();
+        return $this->getPseudo() ?? $this->getUserIdentifier();
     }
 
     public function getId(): ?int
@@ -88,7 +91,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
     {
         $roles = $this->roles;
         // guarantee every user at least has ROLE_USER
-        $roles[] = 'ROLE_USER';
+        $roles[] = 'ROLE_AUTHOR';
 
         return array_unique($roles);
     }
@@ -168,4 +171,16 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
 
         return $this;
     }
+
+    public function getPseudo(): ?string
+    {
+        return $this->pseudo;
+    }
+
+    public function setPseudo(?string $pseudo): static
+    {
+        $this->pseudo = $pseudo;
+
+        return $this;
+    }
 }