|
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
|
|
|
* Enables or disables TOTP on an account if the token is valid. |
|
92
|
|
|
* |
|
93
|
|
|
* @param int $token The token that we want to verify. |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function toggleTotp($token) |
|
97
|
|
|
{ |
|
98
|
|
|
if (! Google2FA::verifyKey($this->totp_secret, $token, 1)) { |
|
|
|
|
|
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->use_totp = ! $this->use_totp; |
|
|
|
|
|
|
103
|
|
|
$this->save(); |
|
104
|
|
|
|
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Set a user password to a new value assuming it meets the following requirements: |
|
110
|
|
|
* - 8 or more characters in length |
|
111
|
|
|
* - at least one uppercase character |
|
112
|
|
|
* - at least one lowercase character |
|
113
|
|
|
* - at least one number. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $password The raw password to set the account password to. |
|
116
|
|
|
* @param string $regex The regex to use when validating the password. Defaults to '((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})'. |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setPassword($password, $regex = '((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})') |
|
120
|
|
|
{ |
|
121
|
|
|
if (! preg_match($regex, $password)) { |
|
122
|
|
|
throw new DisplayException('The password passed did not meet the minimum password requirements.'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->password = Hash::make($password); |
|
|
|
|
|
|
126
|
|
|
$this->save(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Send the password reset notification. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $token |
|
133
|
|
|
* @return void |
|
134
|
|
|
*/ |
|
135
|
|
|
public function sendPasswordResetNotification($token) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->notify(new ResetPasswordNotification($token)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Return true or false depending on wether the user is root admin or not. |
|
142
|
|
|
* |
|
143
|
|
|
* @return bool the user is root admin |
|
144
|
|
|
*/ |
|
145
|
|
|
public function isRootAdmin() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->root_admin === 1; |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns the user's daemon secret for a given server. |
|
152
|
|
|
* @param Server $server \Pterodactyl\Models\Server |
|
153
|
|
|
* @return null|string |
|
154
|
|
|
*/ |
|
155
|
|
|
public function daemonToken(Server $server) |
|
156
|
|
|
{ |
|
157
|
|
|
if ($this->id === $server->owner_id || $this->isRootAdmin()) { |
|
|
|
|
|
|
158
|
|
|
return $server->daemonSecret; |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$subuser = Subuser::where('server_id', $server->id)->where('user_id', $this->id)->first(); |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
if (is_null($subuser)) { |
|
164
|
|
|
return null; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $subuser->daemonSecret; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Returns an array of all servers a user is able to access. |
|
172
|
|
|
* Note: does not account for user admin status. |
|
173
|
|
|
* |
|
174
|
|
|
* @return array |
|
175
|
|
|
*/ |
|
176
|
|
|
public function serverAccessArray() |
|
177
|
|
|
{ |
|
178
|
|
|
$union = Subuser::select('server_id')->where('user_id', $this->id); |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
return Server::select('id')->where('owner_id', $this->id)->union($union)->pluck('id')->all(); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns an array of all servers a user is able to access. |
|
185
|
|
|
* Note: does not account for user admin status. |
|
186
|
|
|
* |
|
187
|
|
|
* @return Collection |
|
188
|
|
|
*/ |
|
189
|
|
|
public function serverAccessCollection($paginate = null) |
|
190
|
|
|
{ |
|
191
|
|
|
$query = Server::with('service', 'node'); |
|
192
|
|
|
if (! $this->isRootAdmin()) { |
|
193
|
|
|
$query->whereIn('id', $this->serverAccessArray()); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return (is_numeric($paginate)) ? $query->paginate($paginate) : $query->get(); |
|
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Returns all permissions that a user has. |
|
201
|
|
|
* |
|
202
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
|
203
|
|
|
*/ |
|
204
|
|
|
public function permissions() |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->hasManyThrough(Permission::class, Subuser::class); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Returns all servers that a user owns. |
|
211
|
|
|
* |
|
212
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
213
|
|
|
*/ |
|
214
|
|
|
public function servers() |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->hasMany(Server::class, 'owner_id'); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
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@propertyannotation 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.