Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Models/User.php
10262 views
1
<?php
2
3
namespace Pterodactyl\Models;
4
5
use Pterodactyl\Rules\Username;
6
use Pterodactyl\Facades\Activity;
7
use Illuminate\Support\Collection;
8
use Illuminate\Validation\Rules\In;
9
use Illuminate\Auth\Authenticatable;
10
use Illuminate\Notifications\Notifiable;
11
use Illuminate\Database\Eloquent\Builder;
12
use Pterodactyl\Models\Traits\HasAccessTokens;
13
use Illuminate\Auth\Passwords\CanResetPassword;
14
use Pterodactyl\Traits\Helpers\AvailableLanguages;
15
use Illuminate\Database\Eloquent\Relations\HasMany;
16
use Illuminate\Foundation\Auth\Access\Authorizable;
17
use Illuminate\Database\Eloquent\Factories\HasFactory;
18
use Illuminate\Database\Eloquent\Relations\MorphToMany;
19
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
20
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
21
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
22
use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification;
23
24
/**
25
* Pterodactyl\Models\User.
26
*
27
* @property int $id
28
* @property string|null $external_id
29
* @property string $uuid
30
* @property string $username
31
* @property string $email
32
* @property string|null $name_first
33
* @property string|null $name_last
34
* @property string $password
35
* @property string|null $remember_token
36
* @property string $language
37
* @property bool $root_admin
38
* @property bool $use_totp
39
* @property string|null $totp_secret
40
* @property \Illuminate\Support\Carbon|null $totp_authenticated_at
41
* @property bool $gravatar
42
* @property \Illuminate\Support\Carbon|null $created_at
43
* @property \Illuminate\Support\Carbon|null $updated_at
44
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\ApiKey[] $apiKeys
45
* @property int|null $api_keys_count
46
* @property string $name
47
* @property \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
48
* @property int|null $notifications_count
49
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\RecoveryToken[] $recoveryTokens
50
* @property int|null $recovery_tokens_count
51
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Server[] $servers
52
* @property int|null $servers_count
53
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\UserSSHKey[] $sshKeys
54
* @property int|null $ssh_keys_count
55
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\ApiKey[] $tokens
56
* @property int|null $tokens_count
57
*
58
* @method static \Database\Factories\UserFactory factory(...$parameters)
59
* @method static Builder|User newModelQuery()
60
* @method static Builder|User newQuery()
61
* @method static Builder|User query()
62
* @method static Builder|User whereCreatedAt($value)
63
* @method static Builder|User whereEmail($value)
64
* @method static Builder|User whereExternalId($value)
65
* @method static Builder|User whereGravatar($value)
66
* @method static Builder|User whereId($value)
67
* @method static Builder|User whereLanguage($value)
68
* @method static Builder|User whereNameFirst($value)
69
* @method static Builder|User whereNameLast($value)
70
* @method static Builder|User wherePassword($value)
71
* @method static Builder|User whereRememberToken($value)
72
* @method static Builder|User whereRootAdmin($value)
73
* @method static Builder|User whereTotpAuthenticatedAt($value)
74
* @method static Builder|User whereTotpSecret($value)
75
* @method static Builder|User whereUpdatedAt($value)
76
* @method static Builder|User whereUseTotp($value)
77
* @method static Builder|User whereUsername($value)
78
* @method static Builder|User whereUuid($value)
79
*
80
* @mixin \Eloquent
81
*/
82
class User extends Model implements
83
AuthenticatableContract,
84
AuthorizableContract,
85
CanResetPasswordContract
86
{
87
use Authenticatable;
88
use Authorizable;
89
use AvailableLanguages;
90
use CanResetPassword;
91
/** @use \Pterodactyl\Models\Traits\HasAccessTokens<\Pterodactyl\Models\ApiKey> */
92
use HasAccessTokens;
93
use Notifiable;
94
/** @use \Illuminate\Database\Eloquent\Factories\HasFactory<\Database\Factories\UserFactory> */
95
use HasFactory;
96
97
public const USER_LEVEL_USER = 0;
98
public const USER_LEVEL_ADMIN = 1;
99
100
/**
101
* The resource name for this model when it is transformed into an
102
* API representation using fractal.
103
*/
104
public const RESOURCE_NAME = 'user';
105
106
/**
107
* Level of servers to display when using access() on a user.
108
*/
109
protected string $accessLevel = 'all';
110
111
/**
112
* The table associated with the model.
113
*/
114
protected $table = 'users';
115
116
/**
117
* A list of mass-assignable variables.
118
*/
119
protected $fillable = [
120
'external_id',
121
'username',
122
'email',
123
'name_first',
124
'name_last',
125
'password',
126
'language',
127
'use_totp',
128
'totp_secret',
129
'totp_authenticated_at',
130
'gravatar',
131
'root_admin',
132
];
133
134
/**
135
* Cast values to correct type.
136
*/
137
protected $casts = [
138
'root_admin' => 'boolean',
139
'use_totp' => 'boolean',
140
'gravatar' => 'boolean',
141
'totp_authenticated_at' => 'datetime',
142
];
143
144
/**
145
* The attributes excluded from the model's JSON form.
146
*/
147
protected $hidden = ['password', 'remember_token', 'totp_secret', 'totp_authenticated_at'];
148
149
/**
150
* Default values for specific fields in the database.
151
*/
152
protected $attributes = [
153
'external_id' => null,
154
'root_admin' => false,
155
'language' => 'en',
156
'use_totp' => false,
157
'totp_secret' => null,
158
];
159
160
/**
161
* Rules verifying that the data being stored matches the expectations of the database.
162
*/
163
public static array $validationRules = [
164
'uuid' => 'required|string|size:36|unique:users,uuid',
165
'email' => 'required|email|between:1,191|unique:users,email',
166
'external_id' => 'sometimes|nullable|string|max:191|unique:users,external_id',
167
'username' => 'required|between:1,191|unique:users,username',
168
'name_first' => 'required|string|between:1,191',
169
'name_last' => 'required|string|between:1,191',
170
'password' => 'sometimes|nullable|string',
171
'root_admin' => 'boolean',
172
'language' => 'string',
173
'use_totp' => 'boolean',
174
'totp_secret' => 'nullable|string',
175
];
176
177
/**
178
* Implement language verification by overriding Eloquence's gather
179
* rules function.
180
*/
181
public static function getRules(): array
182
{
183
$rules = parent::getRules();
184
185
$rules['language'][] = new In(array_keys((new self())->getAvailableLanguages()));
186
$rules['username'][] = new Username();
187
188
return $rules;
189
}
190
191
/**
192
* Return the user model in a format that can be passed over to Vue templates.
193
*/
194
public function toVueObject(): array
195
{
196
return Collection::make($this->toArray())->except(['id', 'external_id'])->toArray();
197
}
198
199
/**
200
* Send the password reset notification.
201
*
202
* @param string $token
203
*/
204
public function sendPasswordResetNotification($token)
205
{
206
Activity::event('auth:reset-password')
207
->withRequestMetadata()
208
->subject($this)
209
->log('sending password reset email');
210
211
$this->notify(new ResetPasswordNotification($token));
212
}
213
214
/**
215
* Store the username as a lowercase string.
216
*/
217
public function setUsernameAttribute(string $value)
218
{
219
$this->attributes['username'] = mb_strtolower($value);
220
}
221
222
/**
223
* Return a concatenated result for the accounts full name.
224
*/
225
public function getNameAttribute(): string
226
{
227
return trim($this->name_first . ' ' . $this->name_last);
228
}
229
230
/**
231
* Returns all servers that a user owns.
232
*
233
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\Pterodactyl\Models\Server, $this>
234
*/
235
public function servers(): HasMany
236
{
237
return $this->hasMany(Server::class, 'owner_id');
238
}
239
240
/**
241
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\Pterodactyl\Models\ApiKey, $this>
242
*/
243
public function apiKeys(): HasMany
244
{
245
return $this->hasMany(ApiKey::class)
246
->where('key_type', ApiKey::TYPE_ACCOUNT);
247
}
248
249
/**
250
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\Pterodactyl\Models\RecoveryToken, $this>
251
*/
252
public function recoveryTokens(): HasMany
253
{
254
return $this->hasMany(RecoveryToken::class);
255
}
256
257
/**
258
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\Pterodactyl\Models\UserSSHKey, $this>
259
*/
260
public function sshKeys(): HasMany
261
{
262
return $this->hasMany(UserSSHKey::class);
263
}
264
265
/**
266
* Returns all the activity logs where this user is the subject — not to
267
* be confused by activity logs where this user is the _actor_.
268
*
269
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\Pterodactyl\Models\ActivityLog, $this>
270
*/
271
public function activity(): MorphToMany
272
{
273
return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects');
274
}
275
276
/**
277
* Returns all the servers that a user can access by way of being the owner of the
278
* server, or because they are assigned as a subuser for that server.
279
*
280
* @return \Illuminate\Database\Eloquent\Builder<\Pterodactyl\Models\Server>
281
*/
282
public function accessibleServers(): Builder
283
{
284
return Server::query()
285
->select('servers.*')
286
->leftJoin('subusers', 'subusers.server_id', '=', 'servers.id')
287
->where(function (Builder $builder) {
288
$builder->where('servers.owner_id', $this->id)->orWhere('subusers.user_id', $this->id);
289
})
290
->groupBy('servers.id');
291
}
292
}
293
294