misc_pterodactyl-panel/app/Http/Controllers/Base/AccountKeyController.php

108 lines
3 KiB
PHP
Raw Normal View History

<?php
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Http\Controllers\Base;
use Illuminate\View\View;
2016-12-07 22:46:38 +00:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Pterodactyl\Models\ApiKey;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Api\KeyCreationService;
use Pterodactyl\Http\Requests\Base\StoreAccountKeyRequest;
2017-08-31 02:14:20 +00:00
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
class AccountKeyController extends Controller
{
/**
* @var \Prologue\Alerts\AlertsMessageBag
*/
protected $alert;
/**
* @var \Pterodactyl\Services\Api\KeyCreationService
*/
protected $keyService;
/**
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
*/
protected $repository;
/**
* APIController constructor.
*
2019-09-06 04:32:57 +00:00
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Services\Api\KeyCreationService $keyService
*/
public function __construct(
AlertsMessageBag $alert,
ApiKeyRepositoryInterface $repository,
KeyCreationService $keyService
) {
$this->alert = $alert;
$this->keyService = $keyService;
$this->repository = $repository;
}
2017-03-19 23:36:50 +00:00
/**
* Display a listing of all account API keys.
2017-03-19 23:36:50 +00:00
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
2017-03-19 23:36:50 +00:00
* @return \Illuminate\View\View
*/
public function index(Request $request): View
{
return view('base.api.index', [
'keys' => $this->repository->getAccountKeys($request->user()),
]);
}
2017-03-19 23:36:50 +00:00
/**
* Display account API key creation page.
2017-03-19 23:36:50 +00:00
*
* @param \Illuminate\Http\Request $request
2017-03-19 23:36:50 +00:00
* @return \Illuminate\View\View
*/
public function create(Request $request): View
{
return view('base.api.new');
}
2017-03-19 23:36:50 +00:00
/**
* Handle saving new account API key.
2017-03-19 23:36:50 +00:00
*
* @param \Pterodactyl\Http\Requests\Base\StoreAccountKeyRequest $request
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
2017-09-02 05:21:15 +00:00
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
2017-03-19 23:36:50 +00:00
*/
public function store(StoreAccountKeyRequest $request)
{
$this->keyService->setKeyType(ApiKey::TYPE_ACCOUNT)->handle([
'user_id' => $request->user()->id,
'allowed_ips' => $request->input('allowed_ips'),
'memo' => $request->input('memo'),
]);
$this->alert->success(trans('base.api.index.keypair_created'))->flash();
return redirect()->route('account.api');
}
2017-03-19 23:36:50 +00:00
/**
* Delete an account API key from the Panel via an AJAX request.
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
2019-09-06 04:32:57 +00:00
* @param string $identifier
* @return \Illuminate\Http\Response
2017-03-19 23:36:50 +00:00
*/
public function revoke(Request $request, string $identifier): Response
{
$this->repository->deleteAccountKey($request->user(), $identifier);
return response('', 204);
}
}