Update underlying model representation for PATs
This commit is contained in:
parent
d60e8a193b
commit
1a3451fb0d
12 changed files with 135 additions and 24002 deletions
|
@ -2,9 +2,78 @@
|
|||
|
||||
namespace Pterodactyl\Models;
|
||||
|
||||
use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Sanctum\Contracts\HasAbilities;
|
||||
|
||||
class PersonalAccessToken extends SanctumPersonalAccessToken
|
||||
class PersonalAccessToken extends Model implements HasAbilities
|
||||
{
|
||||
public const RESOURCE_NAME = 'personal_access_token';
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected $casts = [
|
||||
'user_id' => 'int',
|
||||
'abilities' => 'json',
|
||||
'last_used_at' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected $fillable = [
|
||||
'description',
|
||||
'token',
|
||||
'token_id',
|
||||
'abilities',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the token has a given ability.
|
||||
*
|
||||
* @param string $ability
|
||||
* @return bool
|
||||
*/
|
||||
public function can($ability)
|
||||
{
|
||||
return in_array('*', $this->abilities) ||
|
||||
array_key_exists($ability, array_flip($this->abilities));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the token is missing a given ability.
|
||||
*
|
||||
* @param string $ability
|
||||
* @return bool
|
||||
*/
|
||||
public function cant($ability)
|
||||
{
|
||||
return !$this->can($ability);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the token instance matching the given token.
|
||||
*
|
||||
* @param string $token
|
||||
* @return \Pterodactyl\Models\PersonalAccessToken|null
|
||||
*/
|
||||
public static function findToken($token)
|
||||
{
|
||||
if (strpos($token, '_') === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$id = Str::substr($token, 0, 16);
|
||||
$token = Str::substr($token, strlen($id));
|
||||
|
||||
return static::where('token_id', $id)->where('token', hash('sha256', $token))->first();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue