|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Laravel Platfourm package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\User\Models\Eloquent; |
|
12
|
|
|
|
|
13
|
|
|
use Illuminate\Auth\Authenticatable; |
|
14
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
|
15
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
|
16
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
|
17
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
|
18
|
|
|
use Longman\Platfourm\Auth\Traits\LoginAsPossibility; |
|
19
|
|
|
use Longman\Platfourm\Database\Eloquent\ActionLog\ActionLogTrait; |
|
20
|
|
|
use Longman\Platfourm\Database\Eloquent\EntityLock\EntityLockTrait; |
|
21
|
|
|
use Longman\Platfourm\Database\Eloquent\Model; |
|
22
|
|
|
use Longman\Platfourm\Database\Eloquent\Traits\SoftDeletes; |
|
23
|
|
|
use Longman\Platfourm\Database\Eloquent\Traits\UuidForPrimary; |
|
24
|
|
|
use Longman\Platfourm\User\Models\Eloquent\EntrustUserTrait; |
|
25
|
|
|
|
|
26
|
|
|
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract |
|
27
|
|
|
{ |
|
28
|
|
|
use Authenticatable, CanResetPassword, SoftDeletes, UuidForPrimary, |
|
29
|
|
|
ActionLogTrait, EntityLockTrait, LoginAsPossibility; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The database table used by the model. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $table = 'users'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The attributes that are mass assignable. |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $fillable = [ |
|
44
|
|
|
'email', |
|
45
|
|
|
'password', |
|
46
|
|
|
'firstname', |
|
47
|
|
|
'avatar', |
|
48
|
|
|
'lastname', |
|
49
|
|
|
'role_id', |
|
50
|
|
|
'is_developer', |
|
51
|
|
|
'country_id', |
|
52
|
|
|
'mobile_number', |
|
53
|
|
|
'gender', |
|
54
|
|
|
'birth_date', |
|
55
|
|
|
'address', |
|
56
|
|
|
'activate_token', |
|
57
|
|
|
'status', |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
protected $dates = ['deleted_at', 'birth_date']; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* The attributes excluded from the model's JSON form. |
|
64
|
|
|
* |
|
65
|
|
|
* @var array |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $hidden = ['password', 'remember_token']; |
|
68
|
|
|
|
|
69
|
|
|
public $incrementing = false; |
|
70
|
|
|
|
|
71
|
|
|
protected $keyType = 'string'; |
|
72
|
|
|
|
|
73
|
|
|
protected $searchableFields = ['email', 'firstname', 'lastname']; |
|
74
|
|
|
|
|
75
|
|
|
protected $filterableFields = ['status' => '=']; |
|
76
|
|
|
|
|
77
|
|
|
protected $sortableFields = ['email', 'firstname', 'lastname', 'status', 'created_at', 'updated_at']; |
|
78
|
|
|
|
|
79
|
|
|
public function isDeveloper() |
|
80
|
|
|
{ |
|
81
|
|
|
return (bool)$this->getAttribute('is_developer'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getAvatar() |
|
85
|
|
|
{ |
|
86
|
|
|
$avatar = $this->getAttribute('avatar'); |
|
87
|
|
|
if (!empty($avatar)) { |
|
88
|
|
|
$path = config('cms.user.avatar.path', 'cache/avatar'); |
|
89
|
|
|
return '/' . $path . '/' . $avatar; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$default_path = config('cms.user.avatar.default'); |
|
93
|
|
|
if (empty($default_path)) { |
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return '/' . $default_path; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getFullname() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->getAttribute('firstname') . ' ' . $this->getAttribute('lastname'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function canLogin() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->getAttribute('status') == 1; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function role() |
|
111
|
|
|
{ |
|
112
|
|
|
$model = Role::class; |
|
113
|
|
|
if (class_exists(\App\Models\Role::class)) { |
|
114
|
|
|
$model = \App\Models\Role::class; |
|
115
|
|
|
} |
|
116
|
|
|
return $this->belongsTo($model); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function permissions() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->role->permissions; |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/*$roleModel = Role::class; |
|
|
|
|
|
|
124
|
|
|
if (class_exists(\App\Models\Role::class)) { |
|
125
|
|
|
$roleModel = \App\Models\Role::class; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$permissionModel = Permission::class; |
|
129
|
|
|
if (class_exists(\App\Models\Permission::class)) { |
|
130
|
|
|
$permissionModel = \App\Models\Permission::class; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $this->hasManyThrough($roleModel, $permissionModel);*/ |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
View Code Duplication |
public function hasRole($name, $requireAll = false) |
|
|
|
|
|
|
137
|
|
|
{ |
|
138
|
|
|
if (is_array($name)) { |
|
139
|
|
|
foreach ($name as $roleName) { |
|
140
|
|
|
$hasRole = $this->hasRole($roleName); |
|
141
|
|
|
|
|
142
|
|
|
if ($hasRole && !$requireAll) { |
|
143
|
|
|
return true; |
|
144
|
|
|
} elseif (!$hasRole && $requireAll) { |
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
return $requireAll; |
|
149
|
|
|
} else { |
|
150
|
|
|
return $this->role->name == $name; |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return false; |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
View Code Duplication |
public function can($permission, $requireAll = false) |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
|
|
if (is_array($permission)) { |
|
159
|
|
|
foreach ($permission as $permName) { |
|
160
|
|
|
$hasPerm = $this->can($permName); |
|
161
|
|
|
|
|
162
|
|
|
if ($hasPerm && !$requireAll) { |
|
163
|
|
|
return true; |
|
164
|
|
|
} elseif (!$hasPerm && $requireAll) { |
|
165
|
|
|
return false; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// If we've made it this far and $requireAll is FALSE, then NONE of the perms were found |
|
170
|
|
|
// If we've made it this far and $requireAll is TRUE, then ALL of the perms were found. |
|
171
|
|
|
// Return the value of $requireAll; |
|
172
|
|
|
return $requireAll; |
|
173
|
|
|
} else { |
|
174
|
|
|
// Validate against the Permission table |
|
175
|
|
|
foreach ($this->role->cachedPermissions() as $perm) { |
|
|
|
|
|
|
176
|
|
|
if (str_is($permission, $perm->name)) { |
|
177
|
|
|
return true; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function toArray() |
|
186
|
|
|
{ |
|
187
|
|
|
$this->permissions = $this->role->permissions()->get(['id', 'name'])->toArray(); |
|
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
$array = parent::toArray(); |
|
190
|
|
|
|
|
191
|
|
|
if (property_exists($this, 'loginasData')) { |
|
192
|
|
|
$array['loginasData'] = $this->getLoginAsData(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$array['avatar'] = $this->getAvatar(); |
|
196
|
|
|
|
|
197
|
|
|
return $array; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function country() |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->hasOne('App\Models\Country'); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function accessLog() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->hasMany('App\Models\AccessLog'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.