2017-09-24 01:45:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Eloquent;
|
|
|
|
|
2017-12-03 20:00:47 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2017-09-24 01:45:25 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
use Pterodactyl\Models\DaemonKey;
|
2017-12-03 20:00:47 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2018-01-05 04:49:50 +00:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2017-09-24 01:45:25 +00:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
|
|
|
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
|
|
|
|
|
|
|
class DaemonKeyRepository extends EloquentRepository implements DaemonKeyRepositoryInterface
|
|
|
|
{
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Return the model backing this repository.
|
|
|
|
*
|
|
|
|
* @return string
|
2017-09-24 01:45:25 +00:00
|
|
|
*/
|
|
|
|
public function model()
|
|
|
|
{
|
|
|
|
return DaemonKey::class;
|
|
|
|
}
|
|
|
|
|
2017-11-05 22:07:50 +00:00
|
|
|
/**
|
|
|
|
* Load the server and user relations onto a key model.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\DaemonKey $key
|
|
|
|
* @param bool $refresh
|
|
|
|
* @return \Pterodactyl\Models\DaemonKey
|
|
|
|
*/
|
|
|
|
public function loadServerAndUserRelations(DaemonKey $key, bool $refresh = false): DaemonKey
|
|
|
|
{
|
|
|
|
if (! $key->relationLoaded('server') || $refresh) {
|
|
|
|
$key->load('server');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $key->relationLoaded('user') || $refresh) {
|
|
|
|
$key->load('user');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
2017-09-24 01:45:25 +00:00
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Return a daemon key with the associated server relation attached.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return \Pterodactyl\Models\DaemonKey
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-09-24 01:45:25 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function getKeyWithServer(string $key): DaemonKey
|
2017-09-24 01:45:25 +00:00
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
Assert::notEmpty($key, 'Expected non-empty string as first argument passed to ' . __METHOD__);
|
2017-09-24 01:45:25 +00:00
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
try {
|
|
|
|
return $this->getBuilder()->with('server')->where('secret', '=', $key)->firstOrFail($this->getColumns());
|
|
|
|
} catch (ModelNotFoundException $exception) {
|
2017-09-24 01:45:25 +00:00
|
|
|
throw new RecordNotFoundException;
|
|
|
|
}
|
|
|
|
}
|
2017-12-03 20:00:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all of the keys for a specific user including the information needed
|
|
|
|
* from their server relation for revocation on the daemon.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\User $user
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
public function getKeysForRevocation(User $user): Collection
|
|
|
|
{
|
|
|
|
return $this->getBuilder()->with('server:id,uuid,node_id')->where('user_id', $user->id)->get($this->getColumns());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an array of daemon keys from the database. Used primarily in
|
|
|
|
* conjunction with getKeysForRevocation.
|
|
|
|
*
|
|
|
|
* @param array $ids
|
|
|
|
* @return bool|int
|
|
|
|
*/
|
|
|
|
public function deleteKeys(array $ids)
|
|
|
|
{
|
|
|
|
return $this->getBuilder()->whereIn('id', $ids)->delete();
|
|
|
|
}
|
2017-09-24 01:45:25 +00:00
|
|
|
}
|