Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Requests/Api/Client/Account/StoreApiKeyRequest.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Http\Requests\Api\Client\Account;
4
5
use IPTools\Range;
6
use Pterodactyl\Models\ApiKey;
7
use Illuminate\Validation\Validator;
8
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
9
10
class StoreApiKeyRequest extends ClientApiRequest
11
{
12
public function rules(): array
13
{
14
$rules = ApiKey::getRules();
15
16
return [
17
'description' => $rules['memo'],
18
'allowed_ips' => [...$rules['allowed_ips'], 'max:50'],
19
'allowed_ips.*' => 'string',
20
];
21
}
22
23
/**
24
* Check that each of the values entered is actually valid.
25
*/
26
public function withValidator(Validator $validator): void
27
{
28
$validator->after(function (Validator $validator) {
29
if (!is_array($ips = $this->input('allowed_ips'))) {
30
return;
31
}
32
33
foreach ($ips as $index => $ip) {
34
$valid = false;
35
try {
36
$valid = Range::parse($ip)->valid();
37
} catch (\Exception $exception) {
38
if ($exception->getMessage() !== 'Invalid IP address format') {
39
throw $exception;
40
}
41
} finally {
42
$validator->errors()->addIf(!$valid, "allowed_ips.{$index}", '"' . $ip . '" is not a valid IP address or CIDR range.');
43
}
44
}
45
});
46
}
47
}
48
49