mirror of
https://github.com/MarijnDoeve/TijdVoorDeTest.git
synced 2026-03-07 21:17:57 +01:00
42 lines
1.3 KiB
PHP
42 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repository;
|
|
|
|
use App\Entity\User;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
|
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
|
|
|
|
/** @extends ServiceEntityRepository<User> */
|
|
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.
|
|
* @param User $user
|
|
* */
|
|
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
|
|
{
|
|
$user->setPassword($newHashedPassword);
|
|
$this->getEntityManager()->persist($user);
|
|
$this->getEntityManager()->flush();
|
|
}
|
|
|
|
public function makeAdmin(string $email): void
|
|
{
|
|
$user = $this->findOneBy(['email' => $email]);
|
|
if (!$user instanceof User) {
|
|
throw new \InvalidArgumentException('User not found');
|
|
}
|
|
|
|
$user->setRoles(['ROLE_ADMIN']);
|
|
$this->getEntityManager()->flush();
|
|
}
|
|
}
|