Skip to content

Commit

Permalink
feat(user): add username
Browse files Browse the repository at this point in the history
* add UserNameableTrait

* use it in User

* add migrations

* update fixtures

see: #20, #30
  • Loading branch information
n3wborn committed Nov 1, 2023
1 parent 243e1d3 commit a0bbc6d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
29 changes: 29 additions & 0 deletions migrations/Version20231101200039.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

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

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE "user" ADD username VARCHAR(80) NOT NULL');
$this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649F85E0677 ON "user" (username)');
}

public function down(Schema $schema): void
{
$this->addSql('CREATE SCHEMA public');
$this->addSql('DROP INDEX UNIQ_8D93D649F85E0677');
$this->addSql('ALTER TABLE "user" DROP username');
}
}
2 changes: 2 additions & 0 deletions src/DataFixtures/UserFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ private function createUser(int $number): User
{
return ($user = new User())
->setEmail("mail$number@mail.com")
->setUsername("user-$number")
->setPassword($this->userPasswordHasher->hashPassword($user, 'dev'));
}

private function createAdmin(): User
{
return ($user = new User())
->setEmail('[email protected]')
->setUsername('admin')
->setRoles([User::ROLE_ADMIN])
->setPassword($this->userPasswordHasher->hashPassword($user, 'dev'));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
use ArchivableEntity;
use SluggableTrait;
use CreatableUpdateableTrait;
use UsernameableTrait;

public const ROLE_ADMIN = 'ROLE_ADMIN';
public const ROLE_USER = 'ROLE_USER';
Expand Down Expand Up @@ -70,7 +71,7 @@ final public function setEmail(string $email): self
/** @see UserInterface */
final public function getUserIdentifier(): string
{
return $this->email;
return $this->getUsername();
}

/** @see UserInterface */
Expand Down
23 changes: 23 additions & 0 deletions src/Entity/UsernameableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

trait UsernameableTrait
{
#[ORM\Column(type: 'string', length: 80, unique: true)]
private ?string $username = null;

final public function getUsername(): string
{
return $this->username;
}

final public function setUsername(string $username): self
{
$this->username = $username;

return $this;
}
}

0 comments on commit a0bbc6d

Please sign in to comment.