1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
14
|
|
|
* copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22
|
|
|
* SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Pterodactyl\Models; |
26
|
|
|
|
27
|
|
|
use Hash; |
28
|
|
|
use Google2FA; |
29
|
|
|
use Illuminate\Auth\Authenticatable; |
30
|
|
|
use Illuminate\Database\Eloquent\Model; |
31
|
|
|
use Illuminate\Notifications\Notifiable; |
32
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
33
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
34
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
35
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
36
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
37
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
38
|
|
|
use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification; |
39
|
|
|
|
40
|
|
|
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract |
41
|
|
|
{ |
42
|
|
|
use Authenticatable, Authorizable, CanResetPassword, Notifiable; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The rules for user passwords. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
const PASSWORD_RULES = 'regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The regex rules for usernames. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
const USERNAME_RULES = 'regex:/^([\w\d\.\-]{1,255})$/'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The table associated with the model. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $table = 'users'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* A list of mass-assignable variables. |
67
|
|
|
* |
68
|
|
|
* @var [type] |
69
|
|
|
*/ |
70
|
|
|
protected $fillable = ['username', 'email', 'name_first', 'name_last', 'password', 'language', 'use_totp', 'totp_secret', 'gravatar', 'root_admin']; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Cast values to correct type. |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
protected $casts = [ |
78
|
|
|
'root_admin' => 'integer', |
79
|
|
|
'use_totp' => 'integer', |
80
|
|
|
'gravatar' => 'integer', |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The attributes excluded from the model's JSON form. |
85
|
|
|
* |
86
|
|
|
* @var array |
87
|
|
|
*/ |
88
|
|
|
protected $hidden = ['password', 'remember_token', 'totp_secret']; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Determines if a user has permissions. |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function permissions() |
96
|
|
|
{ |
97
|
|
|
return $this->hasMany(Permission::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Enables or disables TOTP on an account if the token is valid. |
102
|
|
|
* |
103
|
|
|
* @param int $token The token that we want to verify. |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function toggleTotp($token) |
107
|
|
|
{ |
108
|
|
|
if (! Google2FA::verifyKey($this->totp_secret, $token, 1)) { |
|
|
|
|
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->use_totp = ! $this->use_totp; |
|
|
|
|
113
|
|
|
$this->save(); |
114
|
|
|
|
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set a user password to a new value assuming it meets the following requirements: |
120
|
|
|
* - 8 or more characters in length |
121
|
|
|
* - at least one uppercase character |
122
|
|
|
* - at least one lowercase character |
123
|
|
|
* - at least one number. |
124
|
|
|
* |
125
|
|
|
* @param string $password The raw password to set the account password to. |
126
|
|
|
* @param string $regex The regex to use when validating the password. Defaults to '((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})'. |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function setPassword($password, $regex = '((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})') |
130
|
|
|
{ |
131
|
|
|
if (! preg_match($regex, $password)) { |
132
|
|
|
throw new DisplayException('The password passed did not meet the minimum password requirements.'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->password = Hash::make($password); |
|
|
|
|
136
|
|
|
$this->save(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Send the password reset notification. |
141
|
|
|
* |
142
|
|
|
* @param string $token |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
public function sendPasswordResetNotification($token) |
146
|
|
|
{ |
147
|
|
|
$this->notify(new ResetPasswordNotification($token)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return true or false depending on wether the user is root admin or not. |
152
|
|
|
* |
153
|
|
|
* @return bool the user is root admin |
154
|
|
|
*/ |
155
|
|
|
public function isRootAdmin() |
156
|
|
|
{ |
157
|
|
|
return $this->root_admin === 1; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns the user's daemon secret for a given server. |
162
|
|
|
* @param Server $server \Pterodactyl\Models\Server |
163
|
|
|
* @return null|string |
164
|
|
|
*/ |
165
|
|
|
public function daemonToken(Server $server) |
166
|
|
|
{ |
167
|
|
|
if ($this->id === $server->owner_id || $this->isRootAdmin()) { |
|
|
|
|
168
|
|
|
return $server->daemonSecret; |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$subuser = Subuser::where('server_id', $server->id)->where('user_id', $this->id)->first(); |
|
|
|
|
172
|
|
|
|
173
|
|
|
if (is_null($subuser)) { |
174
|
|
|
return null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $subuser->daemonSecret; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
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@property
annotation 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.