Skip to content

Commit

Permalink
feat(entity): add User
Browse files Browse the repository at this point in the history
see: #20
  • Loading branch information
n3wborn committed Jan 13, 2024
1 parent e3b5f49 commit 2371698
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Entity;

use App\Repository\UserRepository;
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\HasLifecycleCallbacks]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
use ArchivableEntity;
use SluggableTrait;
use CreatableUpdateableTrait;

#[ORM\Id]
#[ORM\Column(type: UuidType::NAME, unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
private ?Uuid $id = null;

#[ORM\Column(length: 180, unique: true)]
private ?string $email = null;

#[ORM\Column(type: 'json')]
private array $roles = [];

#[ORM\Column]
private ?string $password = null;

final public function getId(): ?Uuid
{
return $this->id;
}

final public function getEmail(): ?string
{
return $this->email;
}

final public function setEmail(string $email): self
{
$this->email = $email;

return $this;
}

/** @see UserInterface */
final public function getUserIdentifier(): string
{
return $this->email;
}

/** @see UserInterface */
final public function getRoles(): array
{
$roles = $this->roles;
$roles[] = 'ROLE_USER';

return array_unique($roles);
}

final public function setRoles(array $roles): self
{
$this->roles = $roles;

return $this;
}

/** @see PasswordAuthenticatedUserInterface */
final public function getPassword(): string
{
return $this->password;
}

final public function setPassword(#[\SensitiveParameter] string $password): self
{
$this->password = $password;

return $this;
}

/** @see UserInterface */
final public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
}
67 changes: 67 additions & 0 deletions src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Repository;

use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;

/**
* @extends ServiceEntityRepository<User>
*
* @implements PasswordUpgraderInterface<User>
*
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}

/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $user::class));
}

$user->setPassword($newHashedPassword);
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
}

// /**
// * @return User[] Returns an array of User objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('u.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }

// public function findOneBySomeField($value): ?User
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

0 comments on commit 2371698

Please sign in to comment.