Repository navigation
Expand file tree
/
Copy pathUserRepository.php
More file actions
105 lines (89 loc) · 3.05 KB
/
Copy pathUserRepository.php
File metadata and controls
105 lines (89 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace SyncEngine\Repository;
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 SyncEngine\Entity\User;
use function get_class;
/**
* @extends ServiceEntityRepository<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
{
private ApiTokenRepository $apiTokenRepository;
public function __construct( ManagerRegistry $registry, ApiTokenRepository $apiTokenRepository )
{
parent::__construct( $registry, User::class );
$this->apiTokenRepository = $apiTokenRepository;
}
public function remove( User $entity, bool $flush = false ): void
{
$this->getEntityManager()->remove( $entity );
if ( $flush ) {
$this->getEntityManager()->flush();
}
}
/**
* 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->save( $user, true );
}
public function save( User $entity, bool $flush = false ): void
{
$this->getEntityManager()->persist( $entity );
if ( $flush ) {
$this->getEntityManager()->flush();
}
}
public function findByApiToken( string $apiToken ): ?User
{
return $this->apiTokenRepository->findOneBy( [ 'token' => $apiToken ] )?->getUser();
}
public function findByRole( string $role ): array
{
if ( ! preg_match( '/^ROLE_[A-Z]+$/', $role ) ) {
throw new \InvalidArgumentException( 'Invalid role.' );
}
return $this->createQueryBuilder( 'u' )
->andWhere( 'u.roles LIKE :role' )
->setParameter( 'role', '%"'.$role.'"%')
->getQuery()
->getResult();
}
// /**
// * @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()
// ;
// }
}