2026-02-14 11:21:57 -08:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2026-02-14 11:21:57 -08:00
namespace Pterodactyl\Models\Traits ;
2026-10-05 18:10:18 -04:00
use Illuminate\Database\Eloquent\Attributes\Scope ;
use Illuminate\Database\Eloquent\Builder ;
use Illuminate\Database\Eloquent\Casts\Attribute ;
use Illuminate\Database\Eloquent\Model ;
2026-10-11 16:47:03 -04:00
use Illuminate\Support\Arr ;
2026-02-14 11:21:57 -08:00
use Illuminate\Support\Str ;
2026-10-05 18:10:18 -04:00
use InvalidArgumentException ;
2026-02-14 11:21:57 -08:00
use ParagonIE\ConstantTime\Base32 ;
use Pterodactyl\Models\Attributes\Identifiable ;
2026-10-05 18:10:18 -04:00
use Ramsey\Uuid\Uuid ;
use ReflectionClass ;
2026-02-14 11:21:57 -08:00
/**
* Support realtime identifiers on models that do not track an "identifier" column in
* the database. This allows us to make use of the existing data reliant on UUID columns
* while still allowing for output and querying against a more human readable identifier
* value.
*/
trait HasRealtimeIdentifier
{
private static string $identifierPrefix ;
private static string $identifierDataColumn ;
2026-10-05 18:10:18 -04:00
/**
* @param Builder<Model> $builder matches the Pterodactyl\Contracts\Models\Identifiable
* contract; Eloquent always hands the scope a builder for
* the model the trait is used on
*/
#[ Scope ]
public function whereIdentifier ( Builder $builder , string $identifier ) : void
2026-02-14 11:21:57 -08:00
{
2026-10-05 18:10:18 -04:00
if ( ! str_starts_with ( $identifier , $prefix = self :: $identifierPrefix . '_' )) {
2026-02-14 11:21:57 -08:00
$builder -> whereRaw ( '0 = 1' );
return ;
}
2026-10-05 18:10:18 -04:00
$bytes = rescue ( fn () : string => Base32 :: decode ( Str :: replaceFirst ( $prefix , '' , $identifier )), report : false );
2026-02-14 11:21:57 -08:00
if ( empty ( $bytes )) {
$builder -> whereRaw ( '0 = 1' );
return ;
}
$builder -> where ( self :: $identifierDataColumn , Uuid :: fromBytes ( $bytes ) -> toString ());
}
protected static function bootHasRealtimeIdentifier () : void
{
2026-10-05 18:10:18 -04:00
$attrs = ( new ReflectionClass ( static :: class )) -> getAttributes ( Identifiable :: class );
2026-02-14 11:21:57 -08:00
2026-10-05 18:10:18 -04:00
throw_if ( count ( $attrs ) !== 1 , InvalidArgumentException :: class , 'The #[' . Identifiable :: class . '] attribute must be set on ' . static :: class . ' to use realtime identifiers.' );
2026-02-14 11:21:57 -08:00
$instance = $attrs [ 0 ] -> newInstance ();
self :: $identifierPrefix = $instance -> prefix ;
self :: $identifierDataColumn = $instance -> column ;
}
2026-10-05 18:10:18 -04:00
/**
* @return Attribute<non-falsy-string, never>
*/
protected function identifier () : Attribute
{
return Attribute :: get ( function () : string {
2026-10-11 16:47:03 -04:00
$bytes = Uuid :: fromString ( Arr :: string ( $this -> getRawOriginal (), self :: $identifierDataColumn )) -> getBytes ();
2026-10-05 18:10:18 -04:00
return sprintf ( '%s_%s' , self :: $identifierPrefix , Base32 :: encodeUnpadded ( $bytes ));
});
}
2026-02-14 11:21:57 -08:00
}