|
1
|
|
|
<?php namespace Arcanedev\LaravelAuth\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelAuth\Bases\Model; |
|
4
|
|
|
use Arcanedev\LaravelAuth\Exceptions\UserConfirmationException; |
|
5
|
|
|
use Arcanedev\LaravelAuth\Services\UserConfirmator; |
|
6
|
|
|
use Arcanedev\LaravelAuth\Traits\Activatable; |
|
7
|
|
|
use Arcanedev\LaravelAuth\Traits\AuthUserTrait; |
|
8
|
|
|
use Arcanesoft\Contracts\Auth\Models\User as UserContract; |
|
9
|
|
|
use Illuminate\Auth\Authenticatable; |
|
10
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
|
11
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
|
12
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
|
13
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
|
14
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
15
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class User |
|
19
|
|
|
* |
|
20
|
|
|
* @package Arcanedev\LaravelAuth\Models |
|
21
|
|
|
* @author ARCANEDEV <[email protected]> |
|
22
|
|
|
* |
|
23
|
|
|
* @property int id |
|
24
|
|
|
* @property string username |
|
25
|
|
|
* @property string first_name |
|
26
|
|
|
* @property string last_name |
|
27
|
|
|
* @property string full_name |
|
28
|
|
|
* @property string email |
|
29
|
|
|
* @property string password |
|
30
|
|
|
* @property string remember_token |
|
31
|
|
|
* @property bool is_admin |
|
32
|
|
|
* @property bool is_active |
|
33
|
|
|
* @property bool is_confirmed (Optional) |
|
34
|
|
|
* @property string confirmation_code (Optional) |
|
35
|
|
|
* @property \Carbon\Carbon confirmed_at (Optional) |
|
36
|
|
|
* @property \Carbon\Carbon created_at |
|
37
|
|
|
* @property \Carbon\Carbon updated_at |
|
38
|
|
|
* @property \Carbon\Carbon deleted_at |
|
39
|
|
|
* @property \Illuminate\Database\Eloquent\Collection roles |
|
40
|
|
|
* @property \Illuminate\Database\Eloquent\Collection permissions |
|
41
|
|
|
* |
|
42
|
|
|
* @method static bool insert(array $values) |
|
43
|
|
|
* @method \Illuminate\Database\Eloquent\Builder unconfirmed(string $code) |
|
44
|
|
|
*/ |
|
45
|
|
|
class User |
|
|
|
|
|
|
46
|
|
|
extends Model |
|
47
|
|
|
implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, UserContract |
|
48
|
|
|
{ |
|
49
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
50
|
|
|
| Traits |
|
51
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
52
|
|
|
*/ |
|
53
|
|
|
use Activatable, Authenticatable, Authorizable, AuthUserTrait, CanResetPassword, SoftDeletes; |
|
54
|
|
|
|
|
55
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
56
|
|
|
| Properties |
|
57
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
58
|
|
|
*/ |
|
59
|
|
|
/** |
|
60
|
|
|
* The attributes that are mass assignable. |
|
61
|
|
|
* |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $fillable = [ |
|
65
|
|
|
'username', 'first_name', 'last_name', 'email', 'password', |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The attributes excluded from the model's JSON form. |
|
70
|
|
|
* |
|
71
|
|
|
* @var array |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $hidden = [ |
|
74
|
|
|
'password', 'remember_token', 'confirmation_code', |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* The attributes that should be casted to native types. |
|
79
|
|
|
* |
|
80
|
|
|
* @var array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $casts = [ |
|
83
|
|
|
'is_admin' => 'boolean', |
|
84
|
|
|
'is_active' => 'boolean', |
|
85
|
|
|
'is_confirmed' => 'boolean', |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* The attributes that should be mutated to dates. |
|
90
|
|
|
* |
|
91
|
|
|
* @var array |
|
92
|
|
|
*/ |
|
93
|
|
|
protected $dates = [ |
|
94
|
|
|
'confirmed_at', 'deleted_at' |
|
95
|
|
|
]; |
|
96
|
|
|
|
|
97
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
98
|
|
|
| Constructor |
|
99
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
100
|
|
|
*/ |
|
101
|
|
|
/** |
|
102
|
|
|
* Create a new Eloquent model instance. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $attributes |
|
105
|
|
|
*/ |
|
106
|
528 |
|
public function __construct(array $attributes = []) |
|
107
|
|
|
{ |
|
108
|
528 |
|
$this->setTable(config('laravel-auth.users.table', 'users')); |
|
109
|
|
|
|
|
110
|
528 |
|
parent::__construct($attributes); |
|
111
|
528 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
114
|
|
|
| Scopes |
|
115
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
116
|
|
|
*/ |
|
117
|
|
|
/** |
|
118
|
|
|
* Scope unconfirmed users by code. |
|
119
|
|
|
* |
|
120
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
121
|
|
|
* @param string $code |
|
122
|
|
|
* |
|
123
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
124
|
|
|
*/ |
|
125
|
32 |
|
public function scopeUnconfirmed($query, $code) |
|
126
|
|
|
{ |
|
127
|
32 |
|
return $query->where('is_confirmed', false) |
|
128
|
32 |
|
->where('confirmation_code', $code) |
|
129
|
32 |
|
->whereNull('confirmed_at'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
133
|
|
|
| Getters & Setters |
|
134
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
135
|
|
|
*/ |
|
136
|
|
|
/** |
|
137
|
|
|
* Get the full name attribute. |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
8 |
|
public function getFullNameAttribute() |
|
142
|
|
|
{ |
|
143
|
8 |
|
return $this->first_name . ' ' . $this->last_name; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set the password attribute. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $password |
|
150
|
|
|
*/ |
|
151
|
160 |
|
public function setPasswordAttribute($password) |
|
152
|
|
|
{ |
|
153
|
160 |
|
$this->attributes['password'] = bcrypt($password); |
|
154
|
160 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
157
|
|
|
| CRUD Functions |
|
158
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
159
|
|
|
*/ |
|
160
|
|
|
/** |
|
161
|
|
|
* Confirm the unconfirmed user account by confirmation code. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $code |
|
164
|
|
|
* |
|
165
|
|
|
* @return \Arcanesoft\Contracts\Auth\Models\User |
|
166
|
|
|
* |
|
167
|
|
|
* @throws \Arcanedev\LaravelAuth\Exceptions\UserConfirmationException |
|
168
|
|
|
*/ |
|
169
|
32 |
|
public function findUnconfirmed($code) |
|
170
|
|
|
{ |
|
171
|
32 |
|
$unconfirmedUser = self::unconfirmed($code)->first(); |
|
172
|
|
|
|
|
173
|
32 |
|
if ( ! $unconfirmedUser instanceof self) { |
|
174
|
8 |
|
throw (new UserConfirmationException)->setModel(self::class); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
24 |
|
return $unconfirmedUser; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Confirm the new user account. |
|
182
|
|
|
* |
|
183
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User|string $code |
|
184
|
|
|
* |
|
185
|
|
|
* @return \Arcanesoft\Contracts\Auth\Models\User |
|
186
|
|
|
*/ |
|
187
|
16 |
|
public function confirm($code) |
|
188
|
|
|
{ |
|
189
|
16 |
|
if ($code instanceof self) { |
|
190
|
8 |
|
$code = $code->confirmation_code; |
|
191
|
6 |
|
} |
|
192
|
|
|
|
|
193
|
16 |
|
$user = $this->findUnconfirmed($code); |
|
194
|
|
|
|
|
195
|
16 |
|
return (new UserConfirmator)->confirm($user); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
199
|
|
|
| Check Functions |
|
200
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
201
|
|
|
*/ |
|
202
|
|
|
/** |
|
203
|
|
|
* Check if user is an administrator. |
|
204
|
|
|
* |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
40 |
|
public function isAdmin() |
|
208
|
|
|
{ |
|
209
|
40 |
|
return $this->is_admin; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Check if user has a confirmed account. |
|
214
|
|
|
* |
|
215
|
|
|
* @return bool |
|
216
|
|
|
*/ |
|
217
|
24 |
|
public function isConfirmed() |
|
218
|
|
|
{ |
|
219
|
24 |
|
return $this->is_confirmed; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Check if user is on force deleting. |
|
224
|
|
|
* |
|
225
|
|
|
* @return bool |
|
226
|
|
|
*/ |
|
227
|
16 |
|
public function isForceDeleting() |
|
228
|
|
|
{ |
|
229
|
16 |
|
return $this->forceDeleting; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|