1
|
|
|
<?php namespace Arcanesoft\Auth\Models; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelAuth\Models\User as BaseUser; |
4
|
|
|
use Arcanedev\LaravelImpersonator\Contracts\Impersonatable; |
5
|
|
|
use Arcanedev\LaravelImpersonator\Traits\CanImpersonate; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class User |
10
|
|
|
* |
11
|
|
|
* @package Arcanesoft\Auth\Models |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @property \Arcanesoft\Auth\Models\PasswordReset passwordReset |
15
|
|
|
*/ |
16
|
|
|
class User extends BaseUser implements Impersonatable |
17
|
|
|
{ |
18
|
|
|
/* ----------------------------------------------------------------- |
19
|
|
|
| Traits |
20
|
|
|
| ----------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
use Presenters\UserPresenter, |
24
|
|
|
CanImpersonate; |
25
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
27
|
|
|
| Relationships |
28
|
|
|
| ----------------------------------------------------------------- |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Password reset relationship. |
33
|
|
|
* |
34
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
35
|
|
|
*/ |
36
|
|
|
public function passwordReset() |
37
|
|
|
{ |
38
|
|
|
return $this->hasOne(PasswordReset::class, 'email', 'email'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/* ----------------------------------------------------------------- |
42
|
|
|
| Scopes |
43
|
|
|
| ----------------------------------------------------------------- |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Protect admins. |
48
|
|
|
* |
49
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
50
|
|
|
* |
51
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
52
|
|
|
*/ |
53
|
|
|
public function scopeProtectAdmins(Builder $query) |
54
|
|
|
{ |
55
|
|
|
/** @var self $user */ |
56
|
|
|
$user = auth()->user(); |
|
|
|
|
57
|
|
|
|
58
|
|
|
return ($user && $user->is_admin) |
59
|
|
|
? $query |
60
|
|
|
: $query->where('is_admin', false); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/* ----------------------------------------------------------------- |
64
|
|
|
| Main Methods |
65
|
|
|
| ----------------------------------------------------------------- |
66
|
|
|
*/ |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get a user from a hashed id or fail if not found. |
70
|
|
|
* |
71
|
|
|
* @param string $hashedId |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
* |
75
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
76
|
|
|
*/ |
77
|
|
|
public static function firstHashedOrFail($hashedId) |
78
|
|
|
{ |
79
|
|
|
return self::withTrashed()->protectAdmins()->withHashedId($hashedId)->firstOrFail(); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/* ----------------------------------------------------------------- |
83
|
|
|
| Check Methods |
84
|
|
|
| ----------------------------------------------------------------- |
85
|
|
|
*/ |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Check if user is an administrator. |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function isAdmin() |
93
|
|
|
{ |
94
|
|
|
return parent::isAdmin() || $this->hasRoleSlug(Role::ADMINISTRATOR); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check if user is a moderator. |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function isModerator() |
103
|
|
|
{ |
104
|
|
|
return $this->hasRoleSlug(Role::MODERATOR); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Check if user is a member. |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function isMember() |
113
|
|
|
{ |
114
|
|
|
return $this->hasRoleSlug(Role::MEMBER); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Check if the user is deletable. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function isDeletable() |
123
|
|
|
{ |
124
|
|
|
return ! $this->isAdmin(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Check if user has a password reset. |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
public function hasPasswordReset() |
133
|
|
|
{ |
134
|
|
|
return ! is_null($this->passwordReset); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Check if the current modal can impersonate other models. |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function canImpersonate() |
143
|
|
|
{ |
144
|
|
|
return impersonator()->isEnabled() && $this->isAdmin(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Check if the current model can be impersonated. |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function canBeImpersonated() |
153
|
|
|
{ |
154
|
|
|
return impersonator()->isEnabled() && ! $this->isAdmin(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: