1
|
|
|
<?php namespace Usman\Guardian\AccessControl; |
2
|
|
|
|
3
|
|
|
use Illuminate\Auth\AuthManager; |
4
|
|
|
|
5
|
|
|
class Guardian { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The current logged in user |
9
|
|
|
* |
10
|
|
|
* @var User |
11
|
|
|
*/ |
12
|
|
|
protected $user; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The auth driver manager instance |
16
|
|
|
* |
17
|
|
|
* @var Illuminate\Auth\AuthManager |
18
|
|
|
*/ |
19
|
|
|
protected $auth; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var boolean |
23
|
|
|
*/ |
24
|
|
|
protected $loggedIn; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Creates an instance of Guardian |
28
|
|
|
* |
29
|
|
|
* @param Illuminate\Auth\AuthManager $auth |
30
|
|
|
*/ |
31
|
|
|
public function __construct(AuthManager $auth) |
32
|
|
|
{ |
33
|
|
|
$this->auth = $auth; |
|
|
|
|
34
|
|
|
$this->init(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Checks for a role |
39
|
|
|
* |
40
|
|
|
* @param string $roleName |
|
|
|
|
41
|
|
|
* @return boolean |
42
|
|
|
*/ |
43
|
|
|
public function hasRole($role) |
44
|
|
|
{ |
45
|
|
|
if( ! $this->loggedIn) return false; |
46
|
|
|
|
47
|
|
|
return $this->user->hasRole($role); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Checks for any role |
52
|
|
|
* |
53
|
|
|
* @param array $roleNames |
54
|
|
|
* @return boolean |
55
|
|
|
*/ |
56
|
|
|
public function hasAnyRole(array $roleNames) |
57
|
|
|
{ |
58
|
|
|
if( ! $this->loggedIn) return false; |
59
|
|
|
|
60
|
|
|
return $this->user->hasAnyRole($roleNames); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Checks for all roles |
65
|
|
|
* |
66
|
|
|
* @param array $roleNames |
67
|
|
|
* @return boolean |
68
|
|
|
*/ |
69
|
|
|
public function hasAllRoles(array $roleNames) |
70
|
|
|
{ |
71
|
|
|
if ( ! $this->loggedIn) return false; |
72
|
|
|
|
73
|
|
|
return $this->user->hasAllRoles($roleNames); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Checks if a user has a capability |
78
|
|
|
* |
79
|
|
|
* @param string $capabilityName |
|
|
|
|
80
|
|
|
* @return boolean |
81
|
|
|
*/ |
82
|
|
|
public function hasCapability($capability) |
83
|
|
|
{ |
84
|
|
|
if( ! $this->loggedIn) return false; |
85
|
|
|
|
86
|
|
|
return $this->user->hasCapability($capability); |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Checks if a user has any capability |
92
|
|
|
* |
93
|
|
|
* @param array $capabilityNames |
94
|
|
|
* @return boolean |
95
|
|
|
*/ |
96
|
|
|
public function hasAnyCapability(array $capabilityNames) |
97
|
|
|
{ |
98
|
|
|
if( ! $this->loggedIn) return false; |
99
|
|
|
|
100
|
|
|
return $this->user->hasAnyCapability($capabilityNames); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Checks if a user has all the capabilities |
105
|
|
|
* |
106
|
|
|
* @param array $capabilityNames |
107
|
|
|
* @return boolean |
108
|
|
|
*/ |
109
|
|
|
public function hasAllCapabilities(array $capabilityNames) |
110
|
|
|
{ |
111
|
|
|
if( ! $this->loggedIn) return false; |
112
|
|
|
|
113
|
|
|
return $this->user->hasAllCapabilities($capabilityNames); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Initializes the Guardian instance. |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
protected function init() |
122
|
|
|
{ |
123
|
|
|
$user = $this->auth->user(); |
124
|
|
|
|
125
|
|
|
if(is_null($user)) |
126
|
|
|
{ |
127
|
|
|
return $this->loggedIn = false; |
128
|
|
|
} |
129
|
|
|
else |
130
|
|
|
{ |
131
|
|
|
if( ! ($user instanceof AccessControlInterface)) |
132
|
|
|
{ |
133
|
|
|
throw new InvalidUserInstanceException('User Model Should Implement Usman\Guardian\AccessControl\AccessControlInterface'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->user = $user; |
|
|
|
|
137
|
|
|
$this->loggedIn = true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
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..