|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Domain\Security\Exception; |
|
4
|
|
|
|
|
5
|
|
|
use App\Domain\Security\Enum\SecurityType; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Security throttle exception. |
|
9
|
|
|
*/ |
|
10
|
|
|
class SecurityException extends \RuntimeException |
|
11
|
|
|
{ |
|
12
|
22 |
|
public function __construct( |
|
13
|
|
|
private readonly int|string $remainingDelay, |
|
14
|
|
|
private readonly SecurityType $securityType, |
|
15
|
|
|
string $message = 'Security check failed.' |
|
16
|
|
|
) { |
|
17
|
22 |
|
parent::__construct($message); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return int|string int or 'captcha' |
|
22
|
|
|
*/ |
|
23
|
22 |
|
public function getRemainingDelay(): int|string |
|
24
|
|
|
{ |
|
25
|
22 |
|
return $this->remainingDelay; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
22 |
|
public function getSecurityType(): SecurityType |
|
29
|
|
|
{ |
|
30
|
22 |
|
return $this->securityType; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
3 |
|
public function getPublicMessage(): string |
|
34
|
|
|
{ |
|
35
|
3 |
|
$userThrottleMessage = is_numeric($this->remainingDelay) ? |
|
36
|
2 |
|
sprintf(__('wait %s'), '<span class="throttle-time-span">' . $this->remainingDelay . '</span>s') |
|
37
|
1 |
|
: __('fill out the captcha'); |
|
38
|
|
|
|
|
39
|
3 |
|
return match ($this->getSecurityType()) { |
|
40
|
3 |
|
SecurityType::USER_LOGIN, SecurityType::USER_EMAIL => sprintf( |
|
41
|
3 |
|
__('It looks like you are doing this too much.<br> Please %s and try again.', $userThrottleMessage) |
|
42
|
3 |
|
), |
|
43
|
3 |
|
SecurityType::GLOBAL_LOGIN, SecurityType::GLOBAL_EMAIL => __( |
|
44
|
3 |
|
'The site is under a too high request load. |
|
45
|
3 |
|
<br> Therefore, a general throttling is in place. Please fill out the captcha and try again.' |
|
46
|
3 |
|
), |
|
47
|
3 |
|
default => 'Please wait or fill out the captcha and repeat the action.', |
|
48
|
3 |
|
}; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|