2020-03-23 01:15:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Requests\Api\Client\Account;
|
|
|
|
|
2022-06-18 18:21:20 +00:00
|
|
|
use IPTools\Range;
|
2020-10-11 00:15:30 +00:00
|
|
|
use Pterodactyl\Models\ApiKey;
|
2022-06-18 18:21:20 +00:00
|
|
|
use Illuminate\Validation\Validator;
|
2020-03-23 01:15:38 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
|
|
|
|
|
|
|
class StoreApiKeyRequest extends ClientApiRequest
|
|
|
|
{
|
|
|
|
public function rules(): array
|
|
|
|
{
|
2020-10-11 00:15:30 +00:00
|
|
|
$rules = ApiKey::getRules();
|
|
|
|
|
2020-03-23 01:15:38 +00:00
|
|
|
return [
|
2020-10-11 00:15:30 +00:00
|
|
|
'description' => $rules['memo'],
|
2022-06-18 18:21:20 +00:00
|
|
|
'allowed_ips' => [...$rules['allowed_ips'], 'max:50'],
|
|
|
|
'allowed_ips.*' => 'string',
|
2020-03-23 01:15:38 +00:00
|
|
|
];
|
|
|
|
}
|
2020-08-18 02:48:51 +00:00
|
|
|
|
|
|
|
/**
|
2022-06-18 18:21:20 +00:00
|
|
|
* Check that each of the values entered is actually valid.
|
2020-08-18 02:48:51 +00:00
|
|
|
*/
|
2022-06-18 18:21:20 +00:00
|
|
|
public function withValidator(Validator $validator): void
|
2020-08-18 02:48:51 +00:00
|
|
|
{
|
2022-06-18 18:21:20 +00:00
|
|
|
$validator->after(function (Validator $validator) {
|
|
|
|
if (!is_array($ips = $this->input('allowed_ips'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($ips as $index => $ip) {
|
|
|
|
$valid = false;
|
|
|
|
try {
|
|
|
|
$valid = Range::parse($ip)->valid();
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
if ($exception->getMessage() !== 'Invalid IP address format') {
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
$validator->errors()->addIf(!$valid, "allowed_ips.{$index}", '"' . $ip . '" is not a valid IP address or CIDR range.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-08-18 02:48:51 +00:00
|
|
|
}
|
2020-03-23 01:15:38 +00:00
|
|
|
}
|