Files
pterodactyl-panel/app/Models/User.php
T

284 lines
8.5 KiB
PHP
Raw Normal View History

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;
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;
use Pterodactyl\Contracts\Models\Identifiable;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\Traits\HasAccessTokens;
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
/**
* Pterodactyl\Models\User.
*
* @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
* @property string|null $remember_token
* @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
* @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
* @property int|null $api_keys_count
* @property string $name
2026-10-05 18:10:18 -04:00
* @property DatabaseNotificationCollection<int, DatabaseNotification>|DatabaseNotification[] $notifications
* @property int|null $notifications_count
2026-10-05 18:10:18 -04:00
* @property \Illuminate\Database\Eloquent\Collection|RecoveryToken[] $recoveryTokens
* @property int|null $recovery_tokens_count
2026-10-05 18:10:18 -04:00
* @property \Illuminate\Database\Eloquent\Collection|Server[] $servers
* @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
* @property int|null $ssh_keys_count
2026-10-05 18:10:18 -04:00
* @property \Illuminate\Database\Eloquent\Collection|ApiKey[] $tokens
* @property int|null $tokens_count
*
2026-10-05 18:10:18 -04:00
* @method static UserFactory factory(...$parameters)
* @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
*/
#[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
{
use AvailableLanguages;
2026-10-05 18:10:18 -04:00
/** @use HasAccessTokens<ApiKey|TransientToken> */
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
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;
/**
* 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
/**
* Default values for specific fields in the database.
*/
protected $attributes = [
'external_id' => null,
'root_admin' => false,
'language' => 'en',
'use_totp' => false,
'totp_secret' => null,
];
/**
2026-10-05 18:10:18 -04:00
* @return array<string, string>
*/
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
/**
2026-10-05 18:10:18 -04:00
* The column holding the automatically generated v4 UUID for the user.
*
* @return list<string>
*/
2026-10-05 18:10:18 -04:00
public function uniqueIds(): array
{
2026-10-05 18:10:18 -04:00
return ['uuid'];
}
/**
2026-10-05 18:10:18 -04:00
* @return ApiPayload
*/
public function toVueObject(): array
{
2026-10-05 18:10:18 -04:00
$payload = Collection::make($this->toArray())->except(['id', 'external_id'])
->merge(['identifier' => $this->identifier])
->toArray();
2026-10-05 18:10:18 -04:00
JsonValueGuard::assertPayload($payload);
return $payload;
}
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')
->withRequestMetadata()
->subject($this)
->log('sending password reset email');
2016-09-03 17:09:00 -04:00
$this->notify(new ResetPasswordNotification($token));
}
/**
* Returns all servers that a user owns.
*
2026-10-05 18:10:18 -04:00
* @return HasMany<Server, $this>
*/
2022-10-14 10:59:20 -06:00
public function servers(): HasMany
{
return $this->hasMany(Server::class, 'owner_id');
}
/**
2026-10-05 18:10:18 -04:00
* @return HasMany<ApiKey, $this>
*/
2022-10-14 10:59:20 -06:00
public function apiKeys(): HasMany
{
return $this->hasMany(ApiKey::class)
->where('key_type', ApiKey::TYPE_ACCOUNT);
}
/**
2026-10-05 18:10:18 -04:00
* @return HasMany<RecoveryToken, $this>
*/
2022-10-14 10:59:20 -06:00
public function recoveryTokens(): HasMany
{
return $this->hasMany(RecoveryToken::class);
}
/**
2026-10-05 18:10:18 -04:00
* @return HasMany<UserSSHKey, $this>
*/
public function sshKeys(): HasMany
{
return $this->hasMany(UserSSHKey::class);
}
/**
2026-10-05 18:10:18 -04:00
* Returns all the activity logs where this user is the subject - not to
* be confused by activity logs where this user is the _actor_.
*
2026-10-05 18:10:18 -04:00
* @return MorphToMany<ActivityLog, $this>
*/
public function activity(): MorphToMany
{
return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects');
}
/**
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
* server, or because they are assigned as a subuser for that server.
*
2026-10-05 18:10:18 -04:00
* @return Builder<Server>
*/
2022-10-14 10:59:20 -06:00
public function accessibleServers(): Builder
{
return Server::query()
->select('servers.*')
->leftJoin('subusers', 'subusers.server_id', '=', 'servers.id')
2026-10-05 18:10:18 -04:00
->where(function (Builder $builder): void {
$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
}