1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the EloyekunlePermissionsBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Elijah Oyekunle <https://elijahoyekunle.com/> |
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 Eloyekunle\PermissionsBundle\Model; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Collections\Collection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Storage agnostic user object. |
19
|
|
|
*/ |
20
|
|
|
abstract class User implements UserInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var RoleInterface[]|Collection |
24
|
|
|
*/ |
25
|
|
|
protected $userRoles; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* User constructor. |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->userRoles = new ArrayCollection(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function getRoles() |
39
|
|
|
{ |
40
|
|
|
$roleNames = []; |
41
|
|
|
$roles = $this->getUserRoles(); |
42
|
|
|
|
43
|
|
|
foreach ($roles as $role) { |
44
|
|
|
$roleNames[] = $role->getRole(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return array_unique($roleNames); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function hasRole(RoleInterface $role) |
54
|
|
|
{ |
55
|
|
|
return $this->userRoles->contains($role); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function isSuperAdmin() |
62
|
|
|
{ |
63
|
|
|
$isSuperAdmin = false; |
64
|
|
|
foreach ($this->getUserRoles() as $role) { |
65
|
|
|
if ($role->isSuperAdmin()) { |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $isSuperAdmin; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function removeRole(RoleInterface $role) |
77
|
|
|
{ |
78
|
|
|
if (!$this->userRoles->contains($role)) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->userRoles->removeElement($role); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function addRole(RoleInterface $role) |
89
|
|
|
{ |
90
|
|
|
if ($this->userRoles->contains($role)) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->userRoles->add($role); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function setUserRoles(array $userRoles) |
101
|
|
|
{ |
102
|
|
|
foreach ($userRoles as $role) { |
103
|
|
|
$this->addRole($role); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function hasPermission($permission) |
111
|
|
|
{ |
112
|
|
|
$hasPermission = false; |
113
|
|
|
|
114
|
|
|
foreach ($this->getUserRoles() as $role) { |
115
|
|
|
if ($role->isSuperAdmin() || $role->hasPermission($permission)) { |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $hasPermission; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return RoleInterface[] |
125
|
|
|
*/ |
126
|
|
|
public function getUserRoles() |
127
|
|
|
{ |
128
|
|
|
return $this->userRoles; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|