2015-12-06 13:58:49 -05:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2015-12-06 13:58:49 -05:00
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Database\Factories\UserFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\RouteKey;
|
2020-07-06 21:25:00 -07:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
|
use Illuminate\Database\Eloquent\Concerns\HasVersion4Uuids;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
|
use Laravel\Sanctum\TransientToken;
|
2026-02-14 11:21:57 -08:00
|
|
|
use Pterodactyl\Contracts\Models\Identifiable;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Facades\Activity;
|
2022-05-22 19:03:51 -04:00
|
|
|
use Pterodactyl\Models\Traits\HasAccessTokens;
|
2026-02-14 11:21:57 -08:00
|
|
|
use Pterodactyl\Models\Traits\HasRealtimeIdentifier;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Traits\Helpers\AvailableLanguages;
|
2015-12-06 13:58:49 -05:00
|
|
|
|
2019-09-05 21:53:33 -07:00
|
|
|
/**
|
2026-10-11 22:55:07 -04:00
|
|
|
* @property-read int|null $subuser_of_count
|
2019-09-05 21:53:33 -07:00
|
|
|
*/
|
2026-02-14 11:21:57 -08:00
|
|
|
#[Attributes\Identifiable('user')]
|
2026-10-05 18:10:18 -04:00
|
|
|
#[Fillable([
|
|
|
|
|
'external_id',
|
|
|
|
|
'username',
|
|
|
|
|
'email',
|
|
|
|
|
'name_first',
|
|
|
|
|
'name_last',
|
|
|
|
|
'password',
|
|
|
|
|
'language',
|
|
|
|
|
'use_totp',
|
|
|
|
|
'totp_secret',
|
|
|
|
|
'totp_authenticated_at',
|
|
|
|
|
'gravatar',
|
|
|
|
|
'root_admin',
|
|
|
|
|
])]
|
|
|
|
|
#[Hidden(['password', 'remember_token', 'totp_secret', 'totp_authenticated_at'])]
|
|
|
|
|
#[RouteKey('uuid')]
|
|
|
|
|
class User extends Authenticatable implements Identifiable
|
2015-12-06 13:58:49 -05:00
|
|
|
{
|
2020-04-03 23:22:35 -07:00
|
|
|
use AvailableLanguages;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
/** @use HasAccessTokens<ApiKey|TransientToken> */
|
2022-05-22 19:03:51 -04:00
|
|
|
use HasAccessTokens;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
/** @use HasFactory<UserFactory> */
|
2024-10-21 19:18:20 -06:00
|
|
|
use HasFactory;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
2026-02-14 11:21:57 -08:00
|
|
|
use HasRealtimeIdentifier;
|
2026-10-05 18:10:18 -04:00
|
|
|
use HasVersion4Uuids;
|
|
|
|
|
use Notifiable;
|
|
|
|
|
|
|
|
|
|
public const int USER_LEVEL_USER = 0;
|
2015-12-06 13:58:49 -05:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
public const int USER_LEVEL_ADMIN = 1;
|
2017-10-26 23:49:54 -05:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
public const string RESOURCE_NAME = 'user';
|
2015-12-06 13:58:49 -05:00
|
|
|
|
2017-06-24 19:49:09 -05:00
|
|
|
protected $attributes = [
|
2018-02-25 16:08:01 -06:00
|
|
|
'external_id' => null,
|
2017-06-24 19:49:09 -05:00
|
|
|
'root_admin' => false,
|
|
|
|
|
'language' => 'en',
|
|
|
|
|
'use_totp' => false,
|
|
|
|
|
'totp_secret' => null,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return array<string, string>
|
2017-06-24 19:49:09 -05:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'root_admin' => 'boolean',
|
|
|
|
|
'use_totp' => 'boolean',
|
|
|
|
|
'gravatar' => 'boolean',
|
|
|
|
|
'password' => 'hashed',
|
|
|
|
|
'totp_authenticated_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
}
|
2017-04-01 13:14:49 -04:00
|
|
|
|
2018-01-01 15:11:44 -06:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* The column holding the automatically generated v4 UUID for the user.
|
|
|
|
|
*
|
|
|
|
|
* @return list<string>
|
2018-01-01 15:11:44 -06:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function uniqueIds(): array
|
2018-01-01 15:11:44 -06:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
return ['uuid'];
|
2018-01-01 15:11:44 -06:00
|
|
|
}
|
|
|
|
|
|
2018-07-14 22:42:58 -07:00
|
|
|
/**
|
2026-10-11 20:11:13 -04:00
|
|
|
* @return array<mixed>
|
2018-07-14 22:42:58 -07:00
|
|
|
*/
|
|
|
|
|
public function toVueObject(): array
|
|
|
|
|
{
|
2026-10-11 20:11:13 -04:00
|
|
|
$attributes = $this->toArray();
|
|
|
|
|
unset($attributes['id'], $attributes['external_id']);
|
2026-10-05 18:10:18 -04:00
|
|
|
|
2026-10-11 20:11:13 -04:00
|
|
|
return [...$attributes, 'identifier' => $this->identifier];
|
2018-07-14 22:42:58 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-03 17:09:00 -04:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @param string $token
|
2016-09-03 17:09:00 -04:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function sendPasswordResetNotification($token): void
|
2016-09-03 17:09:00 -04:00
|
|
|
{
|
2022-05-29 18:48:35 -04:00
|
|
|
Activity::event('auth:reset-password')
|
2022-05-28 17:03:58 -04:00
|
|
|
->withRequestMetadata()
|
|
|
|
|
->subject($this)
|
|
|
|
|
->log('sending password reset email');
|
|
|
|
|
|
2016-09-03 17:09:00 -04:00
|
|
|
$this->notify(new ResetPasswordNotification($token));
|
|
|
|
|
}
|
2017-01-18 21:13:05 +01:00
|
|
|
|
2017-02-09 17:43:54 -05:00
|
|
|
/**
|
|
|
|
|
* Returns all servers that a user owns.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<Server, $this>
|
2017-02-09 17:43:54 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function servers(): HasMany
|
2017-02-09 17:43:54 -05:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(Server::class, 'owner_id');
|
|
|
|
|
}
|
2017-04-13 23:49:47 -04:00
|
|
|
|
2025-12-20 15:55:13 -08:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<ApiKey, $this>
|
2025-12-20 15:55:13 -08:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function apiKeys(): HasMany
|
2020-03-22 18:15:38 -07:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(ApiKey::class)
|
|
|
|
|
->where('key_type', ApiKey::TYPE_ACCOUNT);
|
|
|
|
|
}
|
2020-07-02 21:55:25 -07:00
|
|
|
|
2025-12-20 15:55:13 -08:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<RecoveryToken, $this>
|
2025-12-20 15:55:13 -08:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function recoveryTokens(): HasMany
|
2020-07-02 21:55:25 -07:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(RecoveryToken::class);
|
|
|
|
|
}
|
2020-07-06 21:25:00 -07:00
|
|
|
|
2025-12-20 15:55:13 -08:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return HasMany<UserSSHKey, $this>
|
2025-12-20 15:55:13 -08:00
|
|
|
*/
|
2022-05-14 17:31:53 -04:00
|
|
|
public function sshKeys(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(UserSSHKey::class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-29 16:19:04 -04:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* Returns all the activity logs where this user is the subject - not to
|
2022-05-29 16:19:04 -04:00
|
|
|
* be confused by activity logs where this user is the _actor_.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return MorphToMany<ActivityLog, $this>
|
2022-05-29 16:19:04 -04:00
|
|
|
*/
|
|
|
|
|
public function activity(): MorphToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects');
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-06 21:25:00 -07:00
|
|
|
/**
|
2022-10-14 10:59:20 -06:00
|
|
|
* Returns all the servers that a user can access by way of being the owner of the
|
2020-07-06 21:25:00 -07:00
|
|
|
* server, or because they are assigned as a subuser for that server.
|
2025-12-20 15:55:13 -08:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @return Builder<Server>
|
2020-07-06 21:25:00 -07:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function accessibleServers(): Builder
|
2020-07-06 21:25:00 -07:00
|
|
|
{
|
2020-07-09 21:32:31 -07:00
|
|
|
return Server::query()
|
2020-07-06 21:25:00 -07:00
|
|
|
->select('servers.*')
|
|
|
|
|
->leftJoin('subusers', 'subusers.server_id', '=', 'servers.id')
|
2026-10-05 18:10:18 -04:00
|
|
|
->where(function (Builder $builder): void {
|
2020-07-06 21:25:00 -07:00
|
|
|
$builder->where('servers.owner_id', $this->id)->orWhere('subusers.user_id', $this->id);
|
|
|
|
|
})
|
|
|
|
|
->groupBy('servers.id');
|
|
|
|
|
}
|
2026-10-05 18:10:18 -04:00
|
|
|
|
2026-10-12 00:21:31 -04:00
|
|
|
/**
|
|
|
|
|
* The owner and root admins hold every permission, and root admins also receive the
|
|
|
|
|
* admin websocket events. Anyone else holds what their subuser entry grants, if any.
|
|
|
|
|
*
|
|
|
|
|
* @return array<array-key, string>
|
|
|
|
|
*/
|
|
|
|
|
public function permissionsOn(Server $server): array
|
|
|
|
|
{
|
|
|
|
|
if ($this->root_admin || $this->id === $server->owner_id) {
|
|
|
|
|
$permissions = ['*'];
|
|
|
|
|
|
|
|
|
|
if ($this->root_admin) {
|
|
|
|
|
$permissions[] = 'admin.websocket.errors';
|
|
|
|
|
$permissions[] = 'admin.websocket.install';
|
|
|
|
|
$permissions[] = 'admin.websocket.transfer';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $permissions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $server->subusers()->where('user_id', $this->id)->first()->permissions ?? [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/**
|
|
|
|
|
* Store the username as a lowercase string. Reads fall through to the stored
|
|
|
|
|
* column, so the get side is the string it was written as.
|
|
|
|
|
*
|
|
|
|
|
* @return Attribute<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected function username(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
set: fn (string $value): string => mb_strtolower($value),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Attribute<string, never>
|
|
|
|
|
*/
|
|
|
|
|
protected function name(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
get: fn (): string => mb_trim($this->name_first.' '.$this->name_last),
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-12-06 13:58:49 -05:00
|
|
|
}
|