forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreApiKeyRequest.php
More file actions
47 lines (41 loc) · 1.38 KB
/
StoreApiKeyRequest.php
File metadata and controls
47 lines (41 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Pterodactyl\Http\Requests\Api\Client\Account;
use IPTools\Range;
use Pterodactyl\Models\ApiKey;
use Illuminate\Validation\Validator;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
class StoreApiKeyRequest extends ClientApiRequest
{
public function rules(): array
{
$rules = ApiKey::getRules();
return [
'description' => $rules['memo'],
'allowed_ips' => [...$rules['allowed_ips'], 'max:50'],
'allowed_ips.*' => 'string',
];
}
/**
* Check that each of the values entered is actually valid.
*/
public function withValidator(Validator $validator): void
{
$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.');
}
}
});
}
}