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
|
|
|
protected $table = 'users'; |
39
|
|
|
|
40
|
|
|
protected $fillable = [ |
41
|
|
|
'full_name', |
42
|
|
|
'email', |
43
|
|
|
'role_id', |
44
|
|
|
'status' |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
protected $hidden = [ |
48
|
|
|
'password', |
49
|
|
|
'password_reset_token', |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
public static $rules = [ |
53
|
|
|
'create' => [ |
54
|
|
|
'email' => 'required|email', |
55
|
|
|
'role_id' => 'required', |
56
|
|
|
'password' => 'required', |
57
|
|
|
], |
58
|
|
|
'update' => [ |
59
|
|
|
'email' => 'required|email', |
60
|
|
|
'role_id' => 'required', |
61
|
|
|
] |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
public function role() |
65
|
|
|
{ |
66
|
|
|
return $this->hasOne('App\Model\Role', 'id', 'role_id'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function access_tokens(){ |
|
|
|
|
70
|
|
|
return $this->hasMany('App\Model\AccessToken', 'user_id', 'id'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function refresh_tokens(){ |
|
|
|
|
74
|
|
|
return $this->hasMany('App\Model\RefreshToken', 'user_id', 'id'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function scopeCurrentUser($query) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$user = Auth::getUser(); |
80
|
|
|
|
81
|
|
|
if ($user) { |
82
|
|
|
if ($user->role_id == self::ROLE_ADMIN) { |
83
|
|
|
return $query; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$query->where('id', $user->id); |
87
|
|
|
} else { |
88
|
|
|
$query->where('id', 0); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $query; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $email |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public static function exist($email) |
100
|
|
|
{ |
101
|
|
|
return self::where('email', $email)->count() > 0; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $email |
106
|
|
|
* |
107
|
|
|
* @return User|null |
108
|
|
|
*/ |
109
|
|
|
public static function findUserByEmail($email) |
110
|
|
|
{ |
111
|
|
|
return self::where('email', $email)->where('status', self::STATUS_ACTIVE)->first(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $resetToken |
116
|
|
|
* |
117
|
|
|
* @return User|null |
118
|
|
|
*/ |
119
|
|
|
public static function findByPasswordResetToken($resetToken) |
120
|
|
|
{ |
121
|
|
|
if (!self::isPasswordResetTokenValid($resetToken)) { |
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return self::where('password_reset_token', $resetToken)->where('status', self::STATUS_ACTIVE)->first(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $token |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public static function isPasswordResetTokenValid($token) |
134
|
|
|
{ |
135
|
|
|
if (empty($token)) { |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$timestamp = (int) substr($token, strrpos($token, '_') + 1); |
140
|
|
|
$expire = 3600; |
141
|
|
|
return $timestamp + $expire >= time(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @void |
146
|
|
|
*/ |
147
|
|
|
public function generatePasswordResetToken() |
148
|
|
|
{ |
149
|
|
|
$this->password_reset_token = Helper::generateRandomString() . '_' . time(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @void |
154
|
|
|
*/ |
155
|
|
|
public function removePasswordResetToken() |
156
|
|
|
{ |
157
|
|
|
$this->password_reset_token = null; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $password |
162
|
|
|
*/ |
163
|
|
|
public function setPassword($password) |
164
|
|
|
{ |
165
|
|
|
$this->access_token = null; |
|
|
|
|
166
|
|
|
$this->password = password_hash($password, PASSWORD_DEFAULT, ['cost' => 13]); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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
.