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\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
|
use Illuminate\Notifications\DatabaseNotification;
|
|
|
|
|
use Illuminate\Notifications\DatabaseNotificationCollection;
|
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
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\Support\JsonValueGuard;
|
|
|
|
|
use Pterodactyl\Traits\Helpers\AvailableLanguages;
|
2015-12-06 13:58:49 -05:00
|
|
|
|
2019-09-05 21:53:33 -07:00
|
|
|
/**
|
2022-05-22 14:57:06 -04:00
|
|
|
* Pterodactyl\Models\User.
|
2022-05-14 17:31:53 -04:00
|
|
|
*
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property int $id
|
|
|
|
|
* @property string|null $external_id
|
|
|
|
|
* @property string $uuid
|
|
|
|
|
* @property string $username
|
|
|
|
|
* @property string $email
|
|
|
|
|
* @property string|null $name_first
|
|
|
|
|
* @property string|null $name_last
|
|
|
|
|
* @property string $password
|
2022-05-22 14:57:06 -04:00
|
|
|
* @property string|null $remember_token
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property string $language
|
|
|
|
|
* @property bool $root_admin
|
|
|
|
|
* @property bool $use_totp
|
|
|
|
|
* @property string|null $totp_secret
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Carbon|null $totp_authenticated_at
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property bool $gravatar
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property Carbon|null $created_at
|
|
|
|
|
* @property Carbon|null $updated_at
|
|
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|ApiKey[] $apiKeys
|
2022-05-14 17:31:53 -04:00
|
|
|
* @property int|null $api_keys_count
|
2022-05-22 14:57:06 -04:00
|
|
|
* @property string $name
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property DatabaseNotificationCollection<int, DatabaseNotification>|DatabaseNotification[] $notifications
|
2022-05-14 17:31:53 -04:00
|
|
|
* @property int|null $notifications_count
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|RecoveryToken[] $recoveryTokens
|
2022-05-14 17:31:53 -04:00
|
|
|
* @property int|null $recovery_tokens_count
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|Server[] $servers
|
2022-05-14 17:31:53 -04:00
|
|
|
* @property int|null $servers_count
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property int|null $subuser_of_count
|
|
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|UserSSHKey[] $sshKeys
|
2022-05-14 17:31:53 -04:00
|
|
|
* @property int|null $ssh_keys_count
|
2026-10-05 18:10:18 -04:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|ApiKey[] $tokens
|
2022-05-22 14:57:06 -04:00
|
|
|
* @property int|null $tokens_count
|
2022-05-14 17:31:53 -04:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @method static UserFactory factory(...$parameters)
|
2022-05-14 17:31:53 -04:00
|
|
|
* @method static Builder|User newModelQuery()
|
|
|
|
|
* @method static Builder|User newQuery()
|
|
|
|
|
* @method static Builder|User query()
|
|
|
|
|
* @method static Builder|User whereCreatedAt($value)
|
|
|
|
|
* @method static Builder|User whereEmail($value)
|
|
|
|
|
* @method static Builder|User whereExternalId($value)
|
|
|
|
|
* @method static Builder|User whereGravatar($value)
|
|
|
|
|
* @method static Builder|User whereId($value)
|
|
|
|
|
* @method static Builder|User whereLanguage($value)
|
|
|
|
|
* @method static Builder|User whereNameFirst($value)
|
|
|
|
|
* @method static Builder|User whereNameLast($value)
|
|
|
|
|
* @method static Builder|User wherePassword($value)
|
|
|
|
|
* @method static Builder|User whereRememberToken($value)
|
|
|
|
|
* @method static Builder|User whereRootAdmin($value)
|
|
|
|
|
* @method static Builder|User whereTotpAuthenticatedAt($value)
|
|
|
|
|
* @method static Builder|User whereTotpSecret($value)
|
|
|
|
|
* @method static Builder|User whereUpdatedAt($value)
|
|
|
|
|
* @method static Builder|User whereUseTotp($value)
|
|
|
|
|
* @method static Builder|User whereUsername($value)
|
|
|
|
|
* @method static Builder|User whereUuid($value)
|
2022-10-14 10:59:20 -06:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @mixin Model
|
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
|
|
|
|
2018-01-25 21:26:06 -06:00
|
|
|
/**
|
|
|
|
|
* The resource name for this model when it is transformed into an
|
|
|
|
|
* API representation using fractal.
|
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
|
* Default values for specific fields in the database.
|
|
|
|
|
*/
|
|
|
|
|
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-05 18:10:18 -04:00
|
|
|
* @return ApiPayload
|
2018-07-14 22:42:58 -07:00
|
|
|
*/
|
|
|
|
|
public function toVueObject(): array
|
|
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$payload = Collection::make($this->toArray())->except(['id', 'external_id'])
|
2026-02-14 11:21:57 -08:00
|
|
|
->merge(['identifier' => $this->identifier])
|
|
|
|
|
->toArray();
|
2026-10-05 18:10:18 -04:00
|
|
|
JsonValueGuard::assertPayload($payload);
|
|
|
|
|
|
|
|
|
|
return $payload;
|
2018-07-14 22:42:58 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-03 17:09:00 -04:00
|
|
|
/**
|
|
|
|
|
* Send the password reset notification.
|
|
|
|
|
*
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 a concatenated result for the accounts full name.
|
|
|
|
|
*
|
|
|
|
|
* @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
|
|
|
}
|