2017-06-25 20:31:50 +00:00
|
|
|
<?php
|
|
|
|
|
2017-07-09 17:29:18 +00:00
|
|
|
namespace Pterodactyl\Services\Api;
|
2017-06-25 20:31:50 +00:00
|
|
|
|
2017-11-19 19:32:17 +00:00
|
|
|
use Pterodactyl\Models\APIKey;
|
2017-07-08 19:07:51 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
2017-06-25 20:31:50 +00:00
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
class KeyCreationService
|
2017-06-25 20:31:50 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-07-08 19:07:51 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
2017-06-25 20:31:50 +00:00
|
|
|
*/
|
2017-11-19 19:32:17 +00:00
|
|
|
private $connection;
|
2017-06-25 20:31:50 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-09 17:29:18 +00:00
|
|
|
* @var \Pterodactyl\Services\Api\PermissionService
|
2017-06-25 20:31:50 +00:00
|
|
|
*/
|
2017-11-19 19:32:17 +00:00
|
|
|
private $permissionService;
|
2017-06-25 20:31:50 +00:00
|
|
|
|
2017-07-08 19:07:51 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
|
|
|
|
*/
|
2017-11-19 19:32:17 +00:00
|
|
|
private $repository;
|
2017-07-08 19:07:51 +00:00
|
|
|
|
2017-06-25 20:31:50 +00:00
|
|
|
/**
|
|
|
|
* ApiKeyService constructor.
|
|
|
|
*
|
2017-07-08 19:07:51 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
|
2017-08-31 02:11:14 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2017-07-09 17:29:18 +00:00
|
|
|
* @param \Pterodactyl\Services\Api\PermissionService $permissionService
|
2017-06-25 20:31:50 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2017-07-08 19:07:51 +00:00
|
|
|
ApiKeyRepositoryInterface $repository,
|
2017-08-31 02:11:14 +00:00
|
|
|
ConnectionInterface $connection,
|
2017-07-09 17:29:18 +00:00
|
|
|
PermissionService $permissionService
|
2017-06-25 20:31:50 +00:00
|
|
|
) {
|
2017-07-08 19:07:51 +00:00
|
|
|
$this->repository = $repository;
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->connection = $connection;
|
2017-06-25 20:31:50 +00:00
|
|
|
$this->permissionService = $permissionService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new API Key on the system with the given permissions.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param array $data
|
|
|
|
* @param array $permissions
|
|
|
|
* @param array $administrative
|
2017-11-19 19:32:17 +00:00
|
|
|
* @return \Pterodactyl\Models\APIKey
|
2017-06-25 20:31:50 +00:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
*/
|
2017-11-19 19:32:17 +00:00
|
|
|
public function handle(array $data, array $permissions, array $administrative = []): APIKey
|
2017-06-25 20:31:50 +00:00
|
|
|
{
|
2017-11-19 19:32:17 +00:00
|
|
|
$token = str_random(APIKey::KEY_LENGTH);
|
|
|
|
$data = array_merge($data, ['token' => $token]);
|
2017-06-25 20:31:50 +00:00
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->connection->beginTransaction();
|
2017-07-08 19:07:51 +00:00
|
|
|
$instance = $this->repository->create($data, true, true);
|
2017-06-25 20:31:50 +00:00
|
|
|
$nodes = $this->permissionService->getPermissions();
|
|
|
|
|
|
|
|
foreach ($permissions as $permission) {
|
2017-11-05 19:42:57 +00:00
|
|
|
@list($block, $search) = explode('-', $permission, 2);
|
2017-06-25 20:31:50 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
(empty($block) || empty($search)) ||
|
|
|
|
! array_key_exists($block, $nodes['_user']) ||
|
|
|
|
! in_array($search, $nodes['_user'][$block])
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-08 19:07:51 +00:00
|
|
|
$this->permissionService->create($instance->id, sprintf('user.%s', $permission));
|
2017-06-25 20:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($administrative as $permission) {
|
2017-11-05 19:42:57 +00:00
|
|
|
@list($block, $search) = explode('-', $permission, 2);
|
2017-06-25 20:31:50 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
(empty($block) || empty($search)) ||
|
|
|
|
! array_key_exists($block, $nodes) ||
|
|
|
|
! in_array($search, $nodes[$block])
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-08 19:07:51 +00:00
|
|
|
$this->permissionService->create($instance->id, $permission);
|
2017-06-25 20:31:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->connection->commit();
|
2017-06-25 20:31:50 +00:00
|
|
|
|
2017-11-19 19:32:17 +00:00
|
|
|
return $instance;
|
2017-06-25 20:31:50 +00:00
|
|
|
}
|
|
|
|
}
|