From 06499ed24ed448e58c8049b42949f9cbfd00c12b Mon Sep 17 00:00:00 2001 From: Sebastien Curt <curt.sebastien@gmail.com> Date: Sat, 20 Nov 2021 14:09:10 +0100 Subject: [PATCH] Create simple user entity --- .env | 2 +- config/packages/doctrine.yaml | 2 +- config/packages/security.yaml | 11 ++- src/Entity/User.php | 152 ++++++++++++++++++++++++++++++ src/Repository/UserRepository.php | 67 +++++++++++++ 5 files changed, 230 insertions(+), 4 deletions(-) create mode 100644 src/Entity/User.php create mode 100644 src/Repository/UserRepository.php diff --git a/.env b/.env index 0a3de17..ef1e6c9 100644 --- a/.env +++ b/.env @@ -28,5 +28,5 @@ APP_SECRET=55ba185719845f708d18a2739b07c44a # # DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db" # DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7" -DATABASE_URL="postgresql://symfony:ChangeMe@127.0.0.1:5432/app?serverVersion=13&charset=utf8" +DATABASE_URL="mysql://rekallfrwsmember:sm4SyZVPu6wpjZfg@mysql:3306/rekallfrwsmember?serverVersion=5.6&charset=utf8" ###< doctrine/doctrine-bundle ### diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index c319176..cfffef1 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -4,7 +4,7 @@ doctrine: # IMPORTANT: You MUST configure your server version, # either here or in the DATABASE_URL env var (see .env file) - #server_version: '13' + #server_version: '5.6' orm: auto_generate_proxy_classes: true naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware diff --git a/config/packages/security.yaml b/config/packages/security.yaml index fc65577..e5ed9a7 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -3,16 +3,23 @@ security: # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords password_hashers: Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto' + App\Entity\User: + algorithm: auto + # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider providers: - users_in_memory: { memory: null } + # used to reload user from session & other features (e.g. switch_user) + app_user_provider: + entity: + class: App\Entity\User + property: email firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: lazy: true - provider: users_in_memory + provider: app_user_provider # activate different ways to authenticate # https://symfony.com/doc/current/security.html#the-firewall diff --git a/src/Entity/User.php b/src/Entity/User.php new file mode 100644 index 0000000..ad0e003 --- /dev/null +++ b/src/Entity/User.php @@ -0,0 +1,152 @@ +<?php + +namespace App\Entity; + +use App\Repository\UserRepository; +use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; +use Symfony\Component\Security\Core\User\UserInterface; + +/** + * @ORM\Entity(repositoryClass=UserRepository::class) + * @ORM\Table(name="`user`") + */ +class User implements UserInterface, PasswordAuthenticatedUserInterface +{ + /** + * @ORM\Id + * @ORM\GeneratedValue + * @ORM\Column(type="integer") + */ + private $id; + + /** + * @ORM\Column(type="string", length=255) + */ + private $email; + + /** + * @ORM\Column(type="string", length=255, unique=true) + */ + private $email_canonical; + + /** + * @ORM\Column(type="string") + */ + private $name; + + /** + * @ORM\Column(type="string") + */ + private $firstname; + + /** + * @ORM\Column(type="string") + */ + private $username; + + /** + * @ORM\Column(type="string", name="username_canonical", unique=true) + */ + private $username_canonical; + + /** + * @ORM\Column(type="json") + */ + private $roles = []; + + /** + * @var string The hashed password + * @ORM\Column(type="string") + */ + private $password; + + public function getId(): ?int + { + return $this->id; + } + + public function getEmail(): ?string + { + return $this->email; + } + + public function setEmail(string $email): self + { + $this->email = $email; + + return $this; + } + + /** + * A visual identifier that represents this user. + * + * @see UserInterface + */ + public function getUserIdentifier(): string + { + return (string) $this->email; + } + + /** + * @deprecated since Symfony 5.3, use getUserIdentifier instead + */ + public function getUsername(): string + { + return (string) $this->email; + } + + /** + * @see UserInterface + */ + public function getRoles(): array + { + $roles = $this->roles; + // guarantee every user at least has ROLE_USER + $roles[] = 'ROLE_USER'; + + return array_unique($roles); + } + + public function setRoles(array $roles): self + { + $this->roles = $roles; + + return $this; + } + + /** + * @see PasswordAuthenticatedUserInterface + */ + public function getPassword(): string + { + return $this->password; + } + + public function setPassword(string $password): self + { + $this->password = $password; + + return $this; + } + + /** + * Returning a salt is only needed, if you are not using a modern + * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml. + * + * @see UserInterface + */ + public function getSalt(): ?string + { + return null; + } + + /** + * @see UserInterface + */ + public function eraseCredentials() + { + // If you store any temporary, sensitive data on the user, clear it here + // $this->plainPassword = null; + } +} diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php new file mode 100644 index 0000000..5eaa8c5 --- /dev/null +++ b/src/Repository/UserRepository.php @@ -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; + +/** + * @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.', \get_class($user))); + } + + $user->setPassword($newHashedPassword); + $this->_em->persist($user); + $this->_em->flush(); + } + + // /** + // * @return User[] Returns an array of User objects + // */ + /* + public function findByExampleField($value) + { + 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() + ; + } + */ +} -- GitLab