1
|
|
|
<?php |
2
|
|
|
namespace App\Model; |
3
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
5
|
|
|
use App\Common\Helper; |
6
|
|
|
use App\Common\Auth; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class User |
10
|
|
|
* |
11
|
|
|
* @property integer $id |
12
|
|
|
* @property string $email |
13
|
|
|
* @property string $full_name |
14
|
|
|
* @property string $password |
15
|
|
|
* @property string $password_reset_token |
16
|
|
|
* @property integer $role_id |
17
|
|
|
* @property integer $created_by |
18
|
|
|
* @property integer $updated_by |
19
|
|
|
* @property \Carbon\Carbon $created_at |
20
|
|
|
* @property \Carbon\Carbon $updated_at |
21
|
|
|
* @property \Carbon\Carbon $deleted_at |
22
|
|
|
* @property integer $status |
23
|
|
|
* @property-read Role $role |
24
|
|
|
* |
25
|
|
|
* @package App\Model |
26
|
|
|
*/ |
27
|
|
|
final class User extends BaseModel |
28
|
|
|
{ |
29
|
|
|
use SoftDeletes; |
30
|
|
|
|
31
|
|
|
const STATUS_BLOCKED = 0; |
32
|
|
|
const STATUS_ACTIVE = 1; |
33
|
|
|
const STATUS_WAIT = 2; |
34
|
|
|
|
35
|
|
|
const ROLE_ADMIN = 1; |
36
|
|
|
const ROLE_USER = 2; |
37
|
|
|
|
38
|
|
|
const EXPIRE_RESET_TOKEN = 3600; |
39
|
|
|
|
40
|
|
|
protected $table = 'users'; |
41
|
|
|
|
42
|
|
|
protected $fillable = [ |
43
|
|
|
'full_name', |
44
|
|
|
'email', |
45
|
|
|
'role_id', |
46
|
|
|
'status' |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
protected $hidden = [ |
50
|
|
|
'password', |
51
|
|
|
'password_reset_token', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
public static $rules = [ |
55
|
|
|
'create' => [ |
56
|
|
|
'email' => 'required|email', |
57
|
|
|
'role_id' => 'required', |
58
|
|
|
'password' => 'required', |
59
|
|
|
], |
60
|
|
|
'update' => [ |
61
|
|
|
'email' => 'required|email', |
62
|
|
|
'role_id' => 'required', |
63
|
|
|
] |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
public function role() |
67
|
|
|
{ |
68
|
|
|
return $this->hasOne('App\Model\Role', 'id', 'role_id'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function access_tokens() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
return $this->hasMany('App\Model\AccessToken', 'user_id', 'id'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function refresh_tokens() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
return $this->hasMany('App\Model\RefreshToken', 'user_id', 'id'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function scopeCurrentUser($query) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$user = Auth::getUser(); |
84
|
|
|
|
85
|
|
|
if ($user) { |
86
|
|
|
if ($user->role_id == self::ROLE_ADMIN) { |
87
|
|
|
return $query; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$query->where('id', $user->id); |
91
|
|
|
} else { |
92
|
|
|
$query->where('id', 0); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $query; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create new User instance and save it |
100
|
|
|
* |
101
|
|
|
* @param $attributes |
102
|
|
|
* @param $password |
103
|
|
|
* @return User|null |
104
|
|
|
*/ |
105
|
|
|
public static function create($attributes, $password) |
106
|
|
|
{ |
107
|
|
|
$user = new self($attributes); |
108
|
|
|
$user->setPassword($password); |
109
|
|
|
if (!$user->save()) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $user; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $email |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public static function exist($email) |
123
|
|
|
{ |
124
|
|
|
return self::where('email', $email)->count() > 0; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $email |
129
|
|
|
* |
130
|
|
|
* @return User|null |
131
|
|
|
*/ |
132
|
|
|
public static function findUserByEmail($email) |
133
|
|
|
{ |
134
|
|
|
return self::where('email', $email)->where('status', self::STATUS_ACTIVE)->first(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $resetToken |
139
|
|
|
* |
140
|
|
|
* @return User|null |
141
|
|
|
*/ |
142
|
|
|
public static function findByPasswordResetToken($resetToken) |
143
|
|
|
{ |
144
|
|
|
if (!self::isPasswordResetTokenValid($resetToken)) { |
145
|
|
|
return null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return self::where('password_reset_token', $resetToken)->where('status', self::STATUS_ACTIVE)->first(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $token |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public static function isPasswordResetTokenValid($token) |
157
|
|
|
{ |
158
|
|
|
if (empty($token)) { |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$timestamp = (int)substr($token, strrpos($token, '_') + 1); |
163
|
|
|
$expire = self::EXPIRE_RESET_TOKEN; |
164
|
|
|
return $timestamp + $expire >= time(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @void |
169
|
|
|
*/ |
170
|
|
|
public function generatePasswordResetToken() |
171
|
|
|
{ |
172
|
|
|
$this->password_reset_token = Helper::generateRandomString().'_'.time(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @void |
177
|
|
|
*/ |
178
|
|
|
public function removePasswordResetToken() |
179
|
|
|
{ |
180
|
|
|
$this->password_reset_token = null; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $password |
185
|
|
|
*/ |
186
|
|
|
public function setPassword($password) |
187
|
|
|
{ |
188
|
|
|
// we need to invalidate tokens when changing password |
189
|
|
|
AccessToken::where('user_id', $this->id)->delete(); |
190
|
|
|
RefreshToken::where('user_id', $this->id)->delete(); |
191
|
|
|
|
192
|
|
|
$this->password = password_hash($password, PASSWORD_DEFAULT, ['cost' => 13]); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
This check looks for method names that are not written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes
databaseConnectionSeeker
.