1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JhUser\Controller; |
4
|
|
|
|
5
|
|
|
use JhUser\Entity\HierarchicalRole; |
6
|
|
|
use JhUser\Entity\Permission; |
7
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
8
|
|
|
use Zend\Console\Request as ConsoleRequest; |
9
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
10
|
|
|
use JhUser\Repository\UserRepositoryInterface; |
11
|
|
|
use JhUser\Repository\RoleRepositoryInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class RoleController |
15
|
|
|
* @package JhUser\Controller |
16
|
|
|
* @author Aydin Hassan <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class RoleController extends AbstractActionController |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \ObjectManager |
23
|
|
|
*/ |
24
|
|
|
protected $objectManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \JhUser\Repository\UserRepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
protected $userRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \JhUser\Repository\RoleRepositoryInterface |
33
|
|
|
*/ |
34
|
|
|
protected $roleRepository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ObjectManager $objectManager |
38
|
|
|
* @param UserRepositoryInterface $userRepository |
39
|
|
|
* @param RoleRepositoryInterface $roleRepository |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
ObjectManager $objectManager, |
43
|
|
|
UserRepositoryInterface $userRepository, |
44
|
|
|
RoleRepositoryInterface $roleRepository |
45
|
|
|
) { |
46
|
|
|
$this->objectManager = $objectManager; |
|
|
|
|
47
|
|
|
$this->userRepository = $userRepository; |
48
|
|
|
$this->roleRepository = $roleRepository; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set a Users role, |
53
|
|
|
* Removes all existing roles |
54
|
|
|
* |
55
|
|
|
* @throws \RuntimeException |
56
|
|
|
*/ |
57
|
|
|
public function setRoleAction() |
58
|
|
|
{ |
59
|
|
|
$request = $this->getRequest(); |
60
|
|
|
|
61
|
|
|
$email = $request->getParam('userEmail'); |
62
|
|
|
$roleId = $request->getParam('role'); |
63
|
|
|
|
64
|
|
|
$user = $this->userRepository->findOneByEmail($email); |
65
|
|
|
|
66
|
|
|
if (!$user) { |
67
|
|
|
throw new \RuntimeException(sprintf('User with email: "%s" could not be found', $email)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$newRole = $this->roleRepository->findByName($roleId); |
71
|
|
|
|
72
|
|
|
if (!$newRole) { |
73
|
|
|
throw new \RuntimeException(sprintf('Role: "%s" could not be found', $roleId)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
foreach ($user->getRoles() as $role) { |
77
|
|
|
$user->removeRole($role); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$user->addRole($newRole); |
81
|
|
|
$this->objectManager->flush(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..