-
Notifications
You must be signed in to change notification settings - Fork 5
/
UserRepository.php
46 lines (40 loc) · 1.07 KB
/
UserRepository.php
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
<?php
namespace AppBundle\Repository;
use AppBundle\Entity\AppRole;
use AppBundle\Entity\AppUser;
use AppBundle\Exception\DuplicateRoleAssignmentException;
use Ramsey\Uuid\Uuid;
class UserRepository extends AppRepository
{
/**
* @param Uuid $userId
* @return AppUser
*/
public function getUserById(Uuid $userId)
{
return $this->em->createQuery(
'select u from E:AppUser u where u.id = :userId'
)
->setParameter('userId', $userId)
->getSingleResult();
}
public function assignRoleToUser(AppUser $user, AppRole $role)
{
if ($user->hasRole($role)) {
throw new DuplicateRoleAssignmentException();
}
$role = $this->getRoleReference($role);
$user->assignRole($role);
$this->save($user);
}
/**
* @param AppRole $role
* @return AppRole
*/
private function getRoleReference(AppRole $role)
{
/** @var AppRole $role */
$role = $this->em->getReference(AppRole::class, $role->getId());
return $role;
}
}