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