56 lines
1.8 KiB
PHP
56 lines
1.8 KiB
PHP
<?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;
|
|
use App\Entity\UsersOrganizations;
|
|
|
|
/**
|
|
* @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.
|
|
*/
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* Returns active users that are NOT in any UsersOrganizations mapping.
|
|
* Returns User entities.
|
|
*
|
|
* @return User[]
|
|
*/
|
|
public function findUsersWithoutOrganization(): array
|
|
{
|
|
$qb = $this->createQueryBuilder('u')
|
|
->select('u')
|
|
->leftJoin(UsersOrganizations::class, 'uo', 'WITH', 'uo.users = u')
|
|
->andWhere('u.isDeleted = :uDeleted')
|
|
->andWhere('uo.id IS NULL')
|
|
->orderBy('u.surname', 'ASC')
|
|
->setParameter('uDeleted', false);
|
|
|
|
return $qb->getQuery()->getResult();
|
|
}
|
|
}
|