1
|
|
|
<?php namespace Anomaly\UsersModule\User; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Model\Users\UsersUsersEntryModel; |
4
|
|
|
use Anomaly\Streams\Platform\Support\Collection; |
5
|
|
|
use Anomaly\UsersModule\Role\Command\GetRole; |
6
|
|
|
use Anomaly\UsersModule\Role\Contract\RoleInterface; |
7
|
|
|
use Anomaly\UsersModule\Role\RoleCollection; |
8
|
|
|
use Anomaly\UsersModule\User\Contract\UserInterface; |
9
|
|
|
use Illuminate\Auth\Authenticatable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class UserModel |
13
|
|
|
* |
14
|
|
|
* @link http://anomaly.is/streams-platform |
15
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
16
|
|
|
* @author Ryan Thompson <[email protected]> |
17
|
|
|
* @package Anomaly\UsersModule\User |
18
|
|
|
*/ |
19
|
|
|
class UserModel extends UsersUsersEntryModel implements UserInterface, \Illuminate\Contracts\Auth\Authenticatable |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
use Authenticatable; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The eager loaded relationships. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $with = [ |
30
|
|
|
'roles' |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The hidden attributes. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $hidden = [ |
39
|
|
|
'password' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the email. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getEmail() |
48
|
|
|
{ |
49
|
|
|
return $this->email; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the username. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getUsername() |
58
|
|
|
{ |
59
|
|
|
return $this->username; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the display name. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getDisplayName() |
68
|
|
|
{ |
69
|
|
|
return $this->display_name; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the first name. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getFirstName() |
78
|
|
|
{ |
79
|
|
|
return $this->first_name; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the last name. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getLastName() |
88
|
|
|
{ |
89
|
|
|
return $this->last_name; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get related roles. |
94
|
|
|
* |
95
|
|
|
* @return RoleCollection |
96
|
|
|
*/ |
97
|
|
|
public function getRoles() |
98
|
|
|
{ |
99
|
|
|
return $this->roles; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return whether a user is in a role. |
104
|
|
|
* |
105
|
|
|
* @param $role |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function hasRole($role) |
109
|
|
|
{ |
110
|
|
|
if (!is_object($role)) { |
111
|
|
|
$role = $this->dispatch(new GetRole($role)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!$role) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/* @var RoleInterface $role */ |
119
|
|
|
foreach ($roles = $this->getRoles() as $attached) { |
120
|
|
|
if ($attached->getId() === $role->getId()) { |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Return whether a user is in |
130
|
|
|
* any of the provided roles. |
131
|
|
|
* |
132
|
|
|
* @param $roles |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public function hasAnyRole($roles) |
136
|
|
|
{ |
137
|
|
|
if ($roles instanceof Collection) { |
|
|
|
|
138
|
|
|
$roles = $roles->all(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (!$roles) { |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
foreach ($roles as $role) { |
146
|
|
|
if ($this->hasRole($role)) { |
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Return whether the user |
156
|
|
|
* is an admin or not. |
157
|
|
|
* |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
|
|
public function isAdmin() |
161
|
|
|
{ |
162
|
|
|
/* @var RoleInterface $role */ |
163
|
|
|
foreach ($roles = $this->getRoles() as $role) { |
164
|
|
|
if ($role->getSlug() === 'admin') { |
165
|
|
|
return true; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the permissions. |
174
|
|
|
* |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
|
|
public function getPermissions() |
178
|
|
|
{ |
179
|
|
|
return $this->permissions; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Return whether a user or it's roles has a permission. |
184
|
|
|
* |
185
|
|
|
* @param $permission |
186
|
|
|
* @param bool $checkRoles |
187
|
|
|
* @return mixed |
188
|
|
|
*/ |
189
|
|
|
public function hasPermission($permission, $checkRoles = true) |
190
|
|
|
{ |
191
|
|
|
if (!$permission) { |
192
|
|
|
return true; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (in_array($permission, $this->getPermissions())) { |
196
|
|
|
return true; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if ($checkRoles) { |
200
|
|
|
|
201
|
|
|
/* @var RoleInterface $role */ |
202
|
|
|
foreach ($this->getRoles() as $role) { |
203
|
|
|
if ($role->hasPermission($permission) || $role->getSlug() === 'admin') { |
204
|
|
|
return true; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return false; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Return whether a user has any of provided permission. |
214
|
|
|
* |
215
|
|
|
* @param $permissions |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
|
|
public function hasAnyPermission(array $permissions) |
219
|
|
|
{ |
220
|
|
|
foreach ($permissions as $permission) { |
221
|
|
|
if ($this->hasPermission($permission)) { |
222
|
|
|
return true; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Hash the password whenever setting it. |
231
|
|
|
* |
232
|
|
|
* @param $password |
233
|
|
|
*/ |
234
|
|
|
public function setPasswordAttribute($password) |
235
|
|
|
{ |
236
|
|
|
$this->attributes['password'] = app('hash')->make($password); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Return whether the model is deletable or not. |
241
|
|
|
* |
242
|
|
|
* @return bool |
243
|
|
|
*/ |
244
|
|
|
public function isDeletable() |
245
|
|
|
{ |
246
|
|
|
// You can't delete yourself. |
247
|
|
|
if ($this->getId() == app('auth')->id()) { |
248
|
|
|
return false; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
// Only admins can delete admins |
252
|
|
|
if (!app('auth')->user()->isAdmin() && $this->isAdmin()) { |
253
|
|
|
return false; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return true; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Return the activated flag. |
261
|
|
|
* |
262
|
|
|
* @return bool |
263
|
|
|
*/ |
264
|
|
|
public function isActivated() |
265
|
|
|
{ |
266
|
|
|
return $this->activated; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Return the enabled flag. |
271
|
|
|
* |
272
|
|
|
* @return bool |
273
|
|
|
*/ |
274
|
|
|
public function isEnabled() |
275
|
|
|
{ |
276
|
|
|
return $this->enabled; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Get the reset code. |
281
|
|
|
* |
282
|
|
|
* @return string |
283
|
|
|
*/ |
284
|
|
|
public function getResetCode() |
285
|
|
|
{ |
286
|
|
|
return $this->reset_code; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Get the activation code. |
291
|
|
|
* |
292
|
|
|
* @return string |
293
|
|
|
*/ |
294
|
|
|
public function getActivationCode() |
295
|
|
|
{ |
296
|
|
|
return $this->activation_code; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Return the full name. |
301
|
|
|
* |
302
|
|
|
* @return string |
303
|
|
|
*/ |
304
|
|
|
public function name() |
305
|
|
|
{ |
306
|
|
|
return $this->getFirstName() . ' ' . $this->getLastName(); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Attach a role to the user. |
311
|
|
|
* |
312
|
|
|
* @param RoleInterface $role |
313
|
|
|
*/ |
314
|
|
|
public function attachRole(RoleInterface $role) |
315
|
|
|
{ |
316
|
|
|
$this->roles()->attach($role); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|