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 Auth; |
28
|
|
|
use Hash; |
29
|
|
|
use Google2FA; |
30
|
|
|
use Illuminate\Auth\Authenticatable; |
31
|
|
|
use Illuminate\Database\Eloquent\Model; |
32
|
|
|
use Illuminate\Notifications\Notifiable; |
33
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
34
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
35
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
36
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
37
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
38
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
39
|
|
|
use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification; |
40
|
|
|
|
41
|
|
|
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract |
42
|
|
|
{ |
43
|
|
|
use Authenticatable, Authorizable, CanResetPassword, Notifiable; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The rules for user passwords. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
const PASSWORD_RULES = 'regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The regex rules for usernames. |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
const USERNAME_RULES = 'regex:/^([\w\d\.\-]{1,255})$/'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The table associated with the model. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $table = 'users'; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* A list of mass-assignable variables. |
68
|
|
|
* |
69
|
|
|
* @var [type] |
70
|
|
|
*/ |
71
|
|
|
protected $fillable = ['username', 'email', 'name_first', 'name_last', 'password', 'language', 'use_totp', 'totp_secret', 'gravatar', 'root_admin']; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Cast values to correct type. |
75
|
|
|
* |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
protected $casts = [ |
79
|
|
|
'root_admin' => 'integer', |
80
|
|
|
'use_totp' => 'integer', |
81
|
|
|
'gravatar' => 'integer', |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* The attributes excluded from the model's JSON form. |
86
|
|
|
* |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
protected $hidden = ['password', 'remember_token', 'totp_secret']; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Determines if a user has permissions. |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function permissions() |
97
|
|
|
{ |
98
|
|
|
return $this->hasMany(Permission::class); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Enables or disables TOTP on an account if the token is valid. |
103
|
|
|
* |
104
|
|
|
* @param int $token The token that we want to verify. |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function toggleTotp($token) |
108
|
|
|
{ |
109
|
|
|
if (! Google2FA::verifyKey($this->totp_secret, $token, 1)) { |
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->use_totp = ! $this->use_totp; |
|
|
|
|
114
|
|
|
$this->save(); |
115
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set a user password to a new value assuming it meets the following requirements: |
121
|
|
|
* - 8 or more characters in length |
122
|
|
|
* - at least one uppercase character |
123
|
|
|
* - at least one lowercase character |
124
|
|
|
* - at least one number. |
125
|
|
|
* |
126
|
|
|
* @param string $password The raw password to set the account password to. |
127
|
|
|
* @param string $regex The regex to use when validating the password. Defaults to '((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})'. |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function setPassword($password, $regex = '((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})') |
131
|
|
|
{ |
132
|
|
|
if (! preg_match($regex, $password)) { |
133
|
|
|
throw new DisplayException('The password passed did not meet the minimum password requirements.'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->password = Hash::make($password); |
|
|
|
|
137
|
|
|
$this->save(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Send the password reset notification. |
142
|
|
|
* |
143
|
|
|
* @param string $token |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function sendPasswordResetNotification($token) |
147
|
|
|
{ |
148
|
|
|
$this->notify(new ResetPasswordNotification($token)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return true or false depending on wether the user is root admin or not. |
153
|
|
|
* |
154
|
|
|
* @return bool the user is root admin |
155
|
|
|
*/ |
156
|
|
|
public function isRootAdmin() |
157
|
|
|
{ |
158
|
|
|
return $this->root_admin === 1; |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the user's daemon secret for a given server. |
163
|
|
|
* @param Server $server \Pterodactyl\Models\Server |
164
|
|
|
* @return null|string |
165
|
|
|
*/ |
166
|
|
|
public function daemonToken(Server $server) |
167
|
|
|
{ |
168
|
|
|
if ($this->id === $server->owner_id || $this->isRootAdmin()) { |
|
|
|
|
169
|
|
|
return $server->daemonSecret; |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$subuser = Subuser::where('server_id', $server->id)->where('user_id', $this->id)->first(); |
|
|
|
|
173
|
|
|
|
174
|
|
|
if (is_null($subuser)) { |
175
|
|
|
return null; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $subuser->daemonSecret; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
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.