diff --git a/.env b/.env index 0a3de178cf858dbdd628f89c13683f44d6a6ba41..ef1e6c93c58eacd95151359bb080da3a9a6e127d 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 c319176505d2ae638a6ff4be5fd9dcf789f836cb..cfffef14543be5fa972d4eefed996ad5ac384d59 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 fc6557705ff8a28737d0bb6374851ce9fa9a6191..e5ed9a7a0d085e9aca36581e802604d7ef3b4480 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 0000000000000000000000000000000000000000..ad0e00349c7fce0564a1891b3877678e7df88125 --- /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 0000000000000000000000000000000000000000..5eaa8c59fc33d95b587925d9977f162c4eb1c215 --- /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() + ; + } + */ +}