|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Http\Controllers\Base; |
| 4 | + |
| 5 | +use Illuminate\View\View; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Illuminate\Http\Response; |
| 8 | +use Pterodactyl\Models\ApiKey; |
| 9 | +use Illuminate\Http\RedirectResponse; |
| 10 | +use Prologue\Alerts\AlertsMessageBag; |
| 11 | +use Pterodactyl\Http\Controllers\Controller; |
| 12 | +use Pterodactyl\Services\Api\KeyCreationService; |
| 13 | +use Pterodactyl\Http\Requests\Base\CreateClientApiKeyRequest; |
| 14 | +use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; |
| 15 | + |
| 16 | +class ClientApiController extends Controller |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var \Prologue\Alerts\AlertsMessageBag |
| 20 | + */ |
| 21 | + private $alert; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Pterodactyl\Services\Api\KeyCreationService |
| 25 | + */ |
| 26 | + private $creationService; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface |
| 30 | + */ |
| 31 | + private $repository; |
| 32 | + |
| 33 | + /** |
| 34 | + * ClientApiController constructor. |
| 35 | + * |
| 36 | + * @param \Prologue\Alerts\AlertsMessageBag $alert |
| 37 | + * @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository |
| 38 | + * @param \Pterodactyl\Services\Api\KeyCreationService $creationService |
| 39 | + */ |
| 40 | + public function __construct(AlertsMessageBag $alert, ApiKeyRepositoryInterface $repository, KeyCreationService $creationService) |
| 41 | + { |
| 42 | + $this->alert = $alert; |
| 43 | + $this->creationService = $creationService; |
| 44 | + $this->repository = $repository; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Return all of the API keys available to this user. |
| 49 | + * |
| 50 | + * @param \Illuminate\Http\Request $request |
| 51 | + * @return \Illuminate\View\View |
| 52 | + */ |
| 53 | + public function index(Request $request): View |
| 54 | + { |
| 55 | + return view('base.api.index', [ |
| 56 | + 'keys' => $this->repository->getAccountKeys($request->user()), |
| 57 | + ]); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Render UI to allow creation of an API key. |
| 62 | + * |
| 63 | + * @return \Illuminate\View\View |
| 64 | + */ |
| 65 | + public function create(): View |
| 66 | + { |
| 67 | + return view('base.api.new'); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Create the API key and return the user to the key listing page. |
| 72 | + * |
| 73 | + * @param \Pterodactyl\Http\Requests\Base\CreateClientApiKeyRequest $request |
| 74 | + * @return \Illuminate\Http\RedirectResponse |
| 75 | + * |
| 76 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 77 | + */ |
| 78 | + public function store(CreateClientApiKeyRequest $request): RedirectResponse |
| 79 | + { |
| 80 | + $allowedIps = null; |
| 81 | + if (! is_null($request->input('allowed_ips'))) { |
| 82 | + $allowedIps = json_encode(explode(PHP_EOL, $request->input('allowed_ips'))); |
| 83 | + } |
| 84 | + |
| 85 | + $this->creationService->setKeyType(ApiKey::TYPE_ACCOUNT)->handle([ |
| 86 | + 'memo' => $request->input('memo'), |
| 87 | + 'allowed_ips' => $allowedIps, |
| 88 | + 'user_id' => $request->user()->id, |
| 89 | + ]); |
| 90 | + |
| 91 | + $this->alert->success('A new client API key has been generated for your account.')->flash(); |
| 92 | + |
| 93 | + return redirect()->route('account.api'); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Delete a client's API key from the panel. |
| 98 | + * |
| 99 | + * @param \Illuminate\Http\Request $request |
| 100 | + * @param $identifier |
| 101 | + * @return \Illuminate\Http\Response |
| 102 | + */ |
| 103 | + public function delete(Request $request, $identifier): Response |
| 104 | + { |
| 105 | + $this->repository->deleteAccountKey($request->user(), $identifier); |
| 106 | + |
| 107 | + return response('', 204); |
| 108 | + } |
| 109 | +} |
0 commit comments