|
1
|
|
|
<?php namespace Arcanedev\LaravelAuth\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelAuth\Bases\User as Authenticatable; |
|
4
|
|
|
use Arcanedev\LaravelAuth\Exceptions\UserConfirmationException; |
|
5
|
|
|
use Arcanedev\LaravelAuth\Services\SocialAuthenticator; |
|
6
|
|
|
use Arcanedev\LaravelAuth\Services\UserConfirmator; |
|
7
|
|
|
use Arcanedev\LaravelAuth\Models\Traits\Activatable; |
|
8
|
|
|
use Arcanedev\LaravelAuth\Models\Traits\AuthUserTrait; |
|
9
|
|
|
use Arcanedev\Support\Traits\PrefixedModel; |
|
10
|
|
|
use Arcanesoft\Contracts\Auth\Models\User as UserContract; |
|
11
|
|
|
use Carbon\Carbon; |
|
12
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
13
|
|
|
use Illuminate\Support\Str; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class User |
|
17
|
|
|
* |
|
18
|
|
|
* @package Arcanedev\LaravelAuth\Models |
|
19
|
|
|
* @author ARCANEDEV <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @property int id |
|
22
|
|
|
* @property string username |
|
23
|
|
|
* @property string first_name |
|
24
|
|
|
* @property string last_name |
|
25
|
|
|
* @property string full_name |
|
26
|
|
|
* @property string email |
|
27
|
|
|
* @property string password |
|
28
|
|
|
* @property string remember_token |
|
29
|
|
|
* @property bool is_admin |
|
30
|
|
|
* @property bool is_active |
|
31
|
|
|
* @property bool is_confirmed (Optional) |
|
32
|
|
|
* @property string confirmation_code (Optional) |
|
33
|
|
|
* @property \Carbon\Carbon confirmed_at (Optional) |
|
34
|
|
|
* @property \Carbon\Carbon last_activity |
|
35
|
|
|
* @property \Carbon\Carbon created_at |
|
36
|
|
|
* @property \Carbon\Carbon updated_at |
|
37
|
|
|
* @property \Carbon\Carbon deleted_at |
|
38
|
|
|
* |
|
39
|
|
|
* @property \Illuminate\Database\Eloquent\Collection roles |
|
40
|
|
|
* @property \Illuminate\Database\Eloquent\Collection permissions |
|
41
|
|
|
* @property \Arcanedev\LaravelAuth\Models\Pivots\RoleUser pivot |
|
42
|
|
|
* |
|
43
|
|
|
* @method static bool insert(array $values) |
|
44
|
|
|
* @method \Illuminate\Database\Eloquent\Builder unconfirmed(string $code) |
|
45
|
|
|
* @method \Illuminate\Database\Eloquent\Builder lastActive(int $minutes = null) |
|
46
|
|
|
*/ |
|
47
|
|
|
class User extends Authenticatable implements UserContract |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
50
|
|
|
| Traits |
|
51
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
52
|
|
|
*/ |
|
53
|
|
|
use AuthUserTrait, |
|
54
|
|
|
Activatable, |
|
55
|
|
|
SoftDeletes; |
|
56
|
|
|
|
|
57
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
58
|
|
|
| Properties |
|
59
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
60
|
|
|
*/ |
|
61
|
|
|
/** |
|
62
|
|
|
* The attributes that are mass assignable. |
|
63
|
|
|
* |
|
64
|
|
|
* @var array |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $fillable = [ |
|
67
|
|
|
'username', |
|
68
|
|
|
'first_name', |
|
69
|
|
|
'last_name', |
|
70
|
|
|
'email', |
|
71
|
|
|
'password', |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* The attributes excluded from the model's JSON form. |
|
76
|
|
|
* |
|
77
|
|
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $hidden = [ |
|
80
|
|
|
'password', |
|
81
|
|
|
'remember_token', |
|
82
|
|
|
'confirmation_code', |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* The attributes that should be casted to native types. |
|
87
|
|
|
* |
|
88
|
|
|
* @var array |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $casts = [ |
|
91
|
|
|
'is_admin' => 'boolean', |
|
92
|
|
|
'is_active' => 'boolean', |
|
93
|
|
|
'is_confirmed' => 'boolean', |
|
94
|
|
|
]; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* The attributes that should be mutated to dates. |
|
98
|
|
|
* |
|
99
|
|
|
* @var array |
|
100
|
|
|
*/ |
|
101
|
|
|
protected $dates = [ |
|
102
|
|
|
'confirmed_at', |
|
103
|
|
|
'last_activity', |
|
104
|
|
|
'deleted_at', |
|
105
|
|
|
]; |
|
106
|
|
|
|
|
107
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
108
|
|
|
| Constructor |
|
109
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
110
|
|
|
*/ |
|
111
|
|
|
/** |
|
112
|
|
|
* Create a new Eloquent model instance. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $attributes |
|
115
|
|
|
*/ |
|
116
|
225 |
|
public function __construct(array $attributes = []) |
|
117
|
|
|
{ |
|
118
|
225 |
|
parent::__construct($attributes); |
|
119
|
|
|
|
|
120
|
225 |
|
$this->setupModel(); |
|
121
|
225 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Setup the model. |
|
125
|
|
|
*/ |
|
126
|
225 |
|
protected function setupModel() |
|
127
|
|
|
{ |
|
128
|
225 |
|
$this->setTable(config('laravel-auth.users.table', 'users')); |
|
129
|
|
|
|
|
130
|
225 |
|
if (SocialAuthenticator::isEnabled()) { |
|
131
|
225 |
|
$this->hidden = array_merge($this->hidden, ['social_provider_id']); |
|
132
|
225 |
|
$this->fillable = array_merge($this->fillable, ['social_provider', 'social_provider_id']); |
|
133
|
|
|
} |
|
134
|
225 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
137
|
|
|
| Relationships |
|
138
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
139
|
|
|
*/ |
|
140
|
|
|
/** |
|
141
|
|
|
* User belongs to many roles. |
|
142
|
|
|
* |
|
143
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
144
|
|
|
*/ |
|
145
|
39 |
|
public function roles() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this |
|
148
|
39 |
|
->belongsToMany( |
|
149
|
39 |
|
config('laravel-auth.roles.model', Role::class), |
|
150
|
39 |
|
$this->getPrefix().config('laravel-auth.role-user.table', 'role_user') |
|
151
|
|
|
) |
|
152
|
39 |
|
->using(Pivots\RoleUser::class) |
|
153
|
39 |
|
->withTimestamps(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
157
|
|
|
| Scopes |
|
158
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
159
|
|
|
*/ |
|
160
|
|
|
/** |
|
161
|
|
|
* Scope unconfirmed users by code. |
|
162
|
|
|
* |
|
163
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
164
|
|
|
* @param string $code |
|
165
|
|
|
* |
|
166
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
167
|
|
|
*/ |
|
168
|
12 |
|
public function scopeUnconfirmed($query, $code) |
|
169
|
|
|
{ |
|
170
|
12 |
|
return $query->where('is_confirmed', false) |
|
171
|
12 |
|
->where('confirmation_code', $code) |
|
172
|
12 |
|
->whereNull('confirmed_at'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Scope last active users. |
|
177
|
|
|
* |
|
178
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
179
|
|
|
* @param int|null $minutes |
|
180
|
|
|
* |
|
181
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
182
|
|
|
*/ |
|
183
|
3 |
|
public function scopeLastActive($query, $minutes = null) |
|
184
|
|
|
{ |
|
185
|
3 |
|
$minutes = $minutes ?: config('laravel_auth.track-activity.minutes', 5); |
|
186
|
|
|
|
|
187
|
3 |
|
$date = Carbon::now()->subMinutes($minutes); |
|
188
|
|
|
|
|
189
|
3 |
|
return $query->where('last_activity', '>=', $date->toDateTimeString()); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
193
|
|
|
| Getters & Setters |
|
194
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
195
|
|
|
*/ |
|
196
|
|
|
/** |
|
197
|
|
|
* Set the `username` attribute. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $username |
|
200
|
|
|
*/ |
|
201
|
69 |
|
public function setUsernameAttribute($username) |
|
202
|
|
|
{ |
|
203
|
69 |
|
$this->attributes['username'] = $this->slugify($username); |
|
204
|
69 |
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get the `full_name` attribute. |
|
208
|
|
|
* |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
3 |
|
public function getFullNameAttribute() |
|
212
|
|
|
{ |
|
213
|
3 |
|
return $this->first_name.' '.$this->last_name; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Set the `password` attribute. |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $password |
|
220
|
|
|
*/ |
|
221
|
69 |
|
public function setPasswordAttribute($password) |
|
222
|
|
|
{ |
|
223
|
69 |
|
$this->attributes['password'] = bcrypt($password); |
|
224
|
69 |
|
} |
|
225
|
|
|
|
|
226
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
227
|
|
|
| CRUD Functions |
|
228
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
229
|
|
|
*/ |
|
230
|
|
|
/** |
|
231
|
|
|
* Confirm the unconfirmed user account by confirmation code. |
|
232
|
|
|
* |
|
233
|
|
|
* @param string $code |
|
234
|
|
|
* |
|
235
|
|
|
* @return \Arcanesoft\Contracts\Auth\Models\User |
|
236
|
|
|
* |
|
237
|
|
|
* @throws \Arcanedev\LaravelAuth\Exceptions\UserConfirmationException |
|
238
|
|
|
*/ |
|
239
|
12 |
|
public function findUnconfirmed($code) |
|
240
|
|
|
{ |
|
241
|
12 |
|
$unconfirmedUser = self::unconfirmed($code)->first(); |
|
242
|
|
|
|
|
243
|
12 |
|
if ( ! $unconfirmedUser instanceof self) |
|
244
|
3 |
|
throw (new UserConfirmationException)->setModel(self::class); |
|
245
|
|
|
|
|
246
|
9 |
|
return $unconfirmedUser; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Confirm the new user account. |
|
251
|
|
|
* |
|
252
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User|string $code |
|
253
|
|
|
* |
|
254
|
|
|
* @return \Arcanesoft\Contracts\Auth\Models\User |
|
255
|
|
|
*/ |
|
256
|
6 |
|
public function confirm($code) |
|
257
|
|
|
{ |
|
258
|
6 |
|
if ($code instanceof self) |
|
259
|
3 |
|
$code = $code->confirmation_code; |
|
260
|
|
|
|
|
261
|
6 |
|
$user = $this->findUnconfirmed($code); |
|
262
|
|
|
|
|
263
|
6 |
|
return (new UserConfirmator)->confirm($user); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Update the user's last activity. |
|
268
|
|
|
* |
|
269
|
|
|
* @param bool $save |
|
270
|
|
|
*/ |
|
271
|
6 |
|
public function updateLastActivity($save = true) |
|
272
|
|
|
{ |
|
273
|
6 |
|
$this->forceFill(['last_activity' => Carbon::now()]); |
|
274
|
|
|
|
|
275
|
6 |
|
if ($save) $this->save(); |
|
276
|
6 |
|
} |
|
277
|
|
|
|
|
278
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
279
|
|
|
| Check Functions |
|
280
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
281
|
|
|
*/ |
|
282
|
|
|
/** |
|
283
|
|
|
* Check if user is an administrator. |
|
284
|
|
|
* |
|
285
|
|
|
* @return bool |
|
286
|
|
|
*/ |
|
287
|
18 |
|
public function isAdmin() |
|
288
|
|
|
{ |
|
289
|
18 |
|
return $this->is_admin; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Check if user is a moderator. |
|
294
|
|
|
* |
|
295
|
|
|
* @return bool |
|
296
|
|
|
*/ |
|
297
|
6 |
|
public function isModerator() |
|
298
|
|
|
{ |
|
299
|
|
|
// Override this method to give more privileges than members. |
|
300
|
6 |
|
return false; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Check if user is a member. |
|
305
|
|
|
* |
|
306
|
|
|
* @return bool |
|
307
|
|
|
*/ |
|
308
|
9 |
|
public function isMember() |
|
309
|
|
|
{ |
|
310
|
9 |
|
return ! $this->isAdmin(); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Check if user has a confirmed account. |
|
315
|
|
|
* |
|
316
|
|
|
* @return bool |
|
317
|
|
|
*/ |
|
318
|
9 |
|
public function isConfirmed() |
|
319
|
|
|
{ |
|
320
|
9 |
|
return $this->is_confirmed; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Check if user can be impersonated. |
|
325
|
|
|
* |
|
326
|
|
|
* @return bool |
|
327
|
|
|
*/ |
|
328
|
3 |
|
public function canBeImpersonated() |
|
329
|
|
|
{ |
|
330
|
3 |
|
return $this->isMember(); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
334
|
|
|
| Other Functions |
|
335
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
336
|
|
|
*/ |
|
337
|
|
|
/** |
|
338
|
|
|
* Slugify the value. |
|
339
|
|
|
* |
|
340
|
|
|
* @param string $value |
|
341
|
|
|
* |
|
342
|
|
|
* @return string |
|
343
|
|
|
*/ |
|
344
|
69 |
|
protected function slugify($value) |
|
345
|
|
|
{ |
|
346
|
69 |
|
return Str::slug($value, config('laravel-auth.users.slug-separator', '.')); |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
|