2016-01-16 00:26:50 +00:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-01-16 00:26:50 +00:00
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
2018-01-12 04:49:46 +00:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2016-01-16 00:26:50 +00:00
|
|
|
|
2020-03-23 01:15:38 +00:00
|
|
|
/**
|
2021-01-28 04:52:11 +00:00
|
|
|
* @property int $id
|
|
|
|
* @property int $user_id
|
|
|
|
* @property int $key_type
|
|
|
|
* @property string $identifier
|
|
|
|
* @property string $token
|
|
|
|
* @property array $allowed_ips
|
|
|
|
* @property string $memo
|
2020-03-23 01:15:38 +00:00
|
|
|
* @property \Carbon\Carbon|null $last_used_at
|
2021-01-28 04:52:11 +00:00
|
|
|
* @property \Carbon\Carbon $created_at
|
|
|
|
* @property \Carbon\Carbon $updated_at
|
2020-03-23 01:15:38 +00:00
|
|
|
*/
|
2020-04-04 06:22:35 +00:00
|
|
|
class ApiKey extends Model
|
2016-01-16 00:26:50 +00:00
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
/**
|
|
|
|
* The resource name for this model when it is transformed into an
|
|
|
|
* API representation using fractal.
|
|
|
|
*/
|
2021-01-23 20:33:34 +00:00
|
|
|
public const RESOURCE_NAME = 'api_key';
|
2020-03-23 01:15:38 +00:00
|
|
|
|
2018-01-14 18:11:04 +00:00
|
|
|
/**
|
|
|
|
* Different API keys that can exist on the system.
|
|
|
|
*/
|
2021-01-23 20:33:34 +00:00
|
|
|
public const TYPE_NONE = 0;
|
|
|
|
public const TYPE_ACCOUNT = 1;
|
|
|
|
public const TYPE_APPLICATION = 2;
|
|
|
|
public const TYPE_DAEMON_USER = 3;
|
|
|
|
public const TYPE_DAEMON_APPLICATION = 4;
|
2018-01-14 18:11:04 +00:00
|
|
|
|
2018-01-13 22:06:19 +00:00
|
|
|
/**
|
|
|
|
* The length of API key identifiers.
|
|
|
|
*/
|
2021-01-23 20:33:34 +00:00
|
|
|
public const IDENTIFIER_LENGTH = 16;
|
2018-01-13 22:06:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The length of the actual API key that is encrypted and stored
|
|
|
|
* in the database.
|
|
|
|
*/
|
2021-01-23 20:33:34 +00:00
|
|
|
public const KEY_LENGTH = 32;
|
2017-11-19 19:32:17 +00:00
|
|
|
|
2016-01-16 00:26:50 +00:00
|
|
|
/**
|
|
|
|
* The table associated with the model.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $table = 'api_keys';
|
|
|
|
|
2017-04-02 04:11:52 +00:00
|
|
|
/**
|
|
|
|
* Cast values to correct type.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
2020-03-28 23:06:36 +00:00
|
|
|
'allowed_ips' => 'array',
|
2018-01-12 04:49:46 +00:00
|
|
|
'user_id' => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_USERS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int',
|
2018-01-26 03:26:06 +00:00
|
|
|
'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'int',
|
2018-01-12 04:49:46 +00:00
|
|
|
'r_' . AdminAcl::RESOURCE_EGGS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_LOCATIONS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_NESTS => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_NODES => 'int',
|
|
|
|
'r_' . AdminAcl::RESOURCE_SERVERS => 'int',
|
2020-12-28 19:47:08 +00:00
|
|
|
'r_' . AdminAcl::RESOURCE_ROLES => 'int',
|
2017-04-02 04:11:52 +00:00
|
|
|
];
|
|
|
|
|
2016-01-16 06:20:27 +00:00
|
|
|
/**
|
2018-01-12 04:49:46 +00:00
|
|
|
* Fields that are mass assignable.
|
2016-01-16 06:20:27 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-01-12 04:49:46 +00:00
|
|
|
protected $fillable = [
|
2018-01-13 22:06:19 +00:00
|
|
|
'identifier',
|
2018-01-12 04:49:46 +00:00
|
|
|
'token',
|
|
|
|
'allowed_ips',
|
|
|
|
'memo',
|
2018-01-14 19:30:55 +00:00
|
|
|
'last_used_at',
|
2018-01-12 04:49:46 +00:00
|
|
|
];
|
2017-02-10 22:29:10 +00:00
|
|
|
|
2018-01-13 22:06:19 +00:00
|
|
|
/**
|
|
|
|
* Fields that should not be included when calling toArray() or toJson()
|
|
|
|
* on this model.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = ['token'];
|
|
|
|
|
2017-06-25 20:31:50 +00:00
|
|
|
/**
|
2018-05-13 14:50:56 +00:00
|
|
|
* Rules to protect against invalid data entry to DB.
|
2017-06-25 20:31:50 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-07-17 21:18:05 +00:00
|
|
|
public static array $validationRules = [
|
2019-09-05 05:19:57 +00:00
|
|
|
'user_id' => 'required|exists:users,id',
|
|
|
|
'key_type' => 'present|integer|min:0|max:4',
|
|
|
|
'identifier' => 'required|string|size:16|unique:api_keys,identifier',
|
|
|
|
'token' => 'required|string',
|
|
|
|
'memo' => 'required|nullable|string|max:500',
|
2020-03-28 23:06:36 +00:00
|
|
|
'allowed_ips' => 'nullable|array',
|
|
|
|
'allowed_ips.*' => 'string',
|
2018-01-13 22:06:19 +00:00
|
|
|
'last_used_at' => 'nullable|date',
|
2018-01-12 04:49:46 +00:00
|
|
|
'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'integer|min:0|max:3',
|
2018-01-26 03:26:06 +00:00
|
|
|
'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'integer|min:0|max:3',
|
2018-01-12 04:49:46 +00:00
|
|
|
'r_' . AdminAcl::RESOURCE_EGGS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_LOCATIONS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_NESTS => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_NODES => 'integer|min:0|max:3',
|
|
|
|
'r_' . AdminAcl::RESOURCE_SERVERS => 'integer|min:0|max:3',
|
2020-12-28 19:47:08 +00:00
|
|
|
'r_' . AdminAcl::RESOURCE_ROLES => 'integer|min:0|max:3',
|
2018-01-12 04:49:46 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dates = [
|
|
|
|
self::CREATED_AT,
|
|
|
|
self::UPDATED_AT,
|
2018-01-13 22:06:19 +00:00
|
|
|
'last_used_at',
|
2017-06-25 20:31:50 +00:00
|
|
|
];
|
2016-01-16 00:26:50 +00:00
|
|
|
}
|