|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of CarcelUserBundle. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016 Damien Carcel <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Carcel\Bundle\UserBundle\Manager; |
|
13
|
|
|
|
|
14
|
|
|
use Carcel\Bundle\UserBundle\Entity\User; |
|
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
16
|
|
|
use FOS\UserBundle\Model\UserInterface; |
|
17
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
18
|
|
|
use Symfony\Component\Validator\Exception\InvalidArgumentException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* User manager. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Damien Carcel <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class UserManager |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var EntityManagerInterface */ |
|
28
|
|
|
protected $entityManager; |
|
29
|
|
|
|
|
30
|
|
|
/** @var RolesManager */ |
|
31
|
|
|
protected $rolesManager; |
|
32
|
|
|
|
|
33
|
|
|
/** @var TokenStorageInterface */ |
|
34
|
|
|
protected $tokenStorage; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param TokenStorageInterface $tokenStorage |
|
38
|
|
|
* @param EntityManagerInterface $entityManager |
|
39
|
|
|
* @param RolesManager $rolesManager |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( |
|
42
|
|
|
TokenStorageInterface $tokenStorage, |
|
43
|
|
|
EntityManagerInterface $entityManager, |
|
44
|
|
|
RolesManager $rolesManager |
|
45
|
|
|
) { |
|
46
|
|
|
$this->tokenStorage = $tokenStorage; |
|
47
|
|
|
$this->entityManager = $entityManager; |
|
48
|
|
|
$this->rolesManager = $rolesManager; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return UserInterface[] |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getAdministrableUsers() |
|
55
|
|
|
{ |
|
56
|
|
|
$users = []; |
|
57
|
|
|
|
|
58
|
|
|
$userRepository = $this->entityManager->getRepository(User::class); |
|
59
|
|
|
$currentUser = $this->tokenStorage->getToken()->getUser(); |
|
60
|
|
|
$users[] = $currentUser; |
|
61
|
|
|
|
|
62
|
|
|
if (!$currentUser->isSuperAdmin()) { |
|
63
|
|
|
$superAdmin = $userRepository->findByRole('ROLE_SUPER_ADMIN'); |
|
|
|
|
|
|
64
|
|
|
$users = array_merge($users, $superAdmin); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $userRepository->findAllBut($users); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Sets a user role. |
|
72
|
|
|
* |
|
73
|
|
|
* New role is provided as an key-value array: |
|
74
|
|
|
* [ |
|
75
|
|
|
* 'roles' => 'ROLE_TO_SET', |
|
76
|
|
|
* ] |
|
77
|
|
|
* |
|
78
|
|
|
* @param UserInterface $user |
|
79
|
|
|
* @param array $selectedRole |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setRole(UserInterface $user, array $selectedRole) |
|
82
|
|
|
{ |
|
83
|
|
|
$choices = $this->rolesManager->getChoices(); |
|
84
|
|
|
|
|
85
|
|
|
if (!isset($choices[$selectedRole['roles']])) { |
|
86
|
|
|
throw new InvalidArgumentException( |
|
87
|
|
|
sprintf('Impossible to set role %s', $selectedRole['roles']) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$user->setRoles([$choices[$selectedRole['roles']]]); |
|
92
|
|
|
|
|
93
|
|
|
$this->entityManager->flush(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.