jeremykenedy /
laravel-blocker
| 1 | <?php |
||||||||
| 2 | |||||||||
| 3 | namespace jeremykenedy\LaravelBlocker\App\Traits; |
||||||||
| 4 | |||||||||
| 5 | use Illuminate\Support\Facades\Redirect; |
||||||||
| 6 | use Illuminate\Support\Facades\Request; |
||||||||
| 7 | use jeremykenedy\LaravelBlocker\App\Models\BlockedItem; |
||||||||
| 8 | |||||||||
| 9 | trait LaravelCheckBlockedTrait |
||||||||
| 10 | { |
||||||||
| 11 | use IpAddressDetails; |
||||||||
| 12 | |||||||||
| 13 | /** |
||||||||
| 14 | * Check if on laravel blocer list and respond accordingly. |
||||||||
| 15 | */ |
||||||||
| 16 | public static function checkBlocked() |
||||||||
| 17 | { |
||||||||
| 18 | $requestIp = Request::ip(); |
||||||||
| 19 | $all = Request::all(); |
||||||||
| 20 | $method = Request::method(); |
||||||||
| 21 | $route = Request::route(); |
||||||||
| 22 | $ipAddressDetails = IpAddressDetails::checkIP($requestIp); |
||||||||
| 23 | $blocked = false; |
||||||||
| 24 | $type = null; |
||||||||
| 25 | |||||||||
| 26 | // Check IP |
||||||||
| 27 | $blocked = self::checkedBlockedList($requestIp, $blocked); |
||||||||
| 28 | |||||||||
| 29 | // Check Ip Address Details |
||||||||
| 30 | if ($ipAddressDetails) { |
||||||||
| 31 | // Check City |
||||||||
| 32 | $blocked = self::checkedBlockedList($ipAddressDetails['city'], $blocked); |
||||||||
| 33 | |||||||||
| 34 | // Check State |
||||||||
| 35 | $blocked = self::checkedBlockedList($ipAddressDetails['state'], $blocked); |
||||||||
| 36 | |||||||||
| 37 | // Check Country |
||||||||
| 38 | $blocked = self::checkedBlockedList($ipAddressDetails['country'], $blocked); |
||||||||
| 39 | |||||||||
| 40 | // Check Country Code |
||||||||
| 41 | $blocked = self::checkedBlockedList($ipAddressDetails['countryCode'], $blocked); |
||||||||
| 42 | |||||||||
| 43 | // Check Continent |
||||||||
| 44 | $blocked = self::checkedBlockedList($ipAddressDetails['continent'], $blocked); |
||||||||
| 45 | |||||||||
| 46 | // Check Continent |
||||||||
| 47 | $blocked = self::checkedBlockedList($ipAddressDetails['continent'], $blocked); |
||||||||
| 48 | |||||||||
| 49 | // Check Region |
||||||||
| 50 | $blocked = self::checkedBlockedList($ipAddressDetails['region'], $blocked); |
||||||||
| 51 | |||||||||
| 52 | $type = 'ip'; |
||||||||
| 53 | } |
||||||||
| 54 | |||||||||
| 55 | // Registering |
||||||||
| 56 | if ($method === 'POST' && $route->uri === 'register') { |
||||||||
| 57 | $domain_name = self::getEmailDomain($all['email']); |
||||||||
| 58 | $blocked = self::checkedBlockedList($domain_name, $blocked); |
||||||||
| 59 | $blocked = self::checkedBlockedList($all['email'], $blocked); |
||||||||
| 60 | $type = 'register'; |
||||||||
| 61 | } |
||||||||
| 62 | |||||||||
| 63 | // Logged IN |
||||||||
| 64 | if (\Auth::check()) { |
||||||||
| 65 | $userId = Request::user()->id; |
||||||||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||||||||
| 66 | $userEmail = Request::user()->email; |
||||||||
| 67 | $domain_name = self::getEmailDomain($userEmail); |
||||||||
| 68 | $blocked = self::checkedBlockedList($domain_name, $blocked); |
||||||||
| 69 | $blocked = self::checkedBlockedList($userEmail, $blocked); |
||||||||
| 70 | $type = 'auth'; |
||||||||
| 71 | } |
||||||||
| 72 | |||||||||
| 73 | self::checkBlockedActions($blocked, $type); |
||||||||
| 74 | } |
||||||||
| 75 | |||||||||
| 76 | /** |
||||||||
| 77 | * How to responde to a blocked item. |
||||||||
| 78 | * |
||||||||
| 79 | * @param string $blocked The blocked item |
||||||||
| 80 | * @param string $type The type of blocked item |
||||||||
| 81 | */ |
||||||||
| 82 | private static function checkBlockedActions($blocked, $type = null) |
||||||||
| 83 | { |
||||||||
| 84 | if ($blocked) { |
||||||||
| 85 | switch ($type) { |
||||||||
| 86 | case 'register': |
||||||||
| 87 | return Redirect::back()->withError('Not allowed'); |
||||||||
| 88 | break; |
||||||||
|
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other Loading history...
|
|||||||||
| 89 | |||||||||
| 90 | case 'auth': |
||||||||
| 91 | case 'ip': |
||||||||
| 92 | default: |
||||||||
| 93 | switch (config('laravelblocker.blockerDefaultAction')) { |
||||||||
|
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 94 | case 'view': |
||||||||
| 95 | abort(response()->view(config('laravelblocker.blockerDefaultActionView'))); |
||||||||
|
0 ignored issues
–
show
The function
abort was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The function
response was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 96 | break; |
||||||||
| 97 | |||||||||
| 98 | case 'redirect': |
||||||||
| 99 | $currentRoute = Request::route()->getName(); |
||||||||
| 100 | $redirectRoute = config('laravelblocker.blockerDefaultActionRedirect'); |
||||||||
| 101 | |||||||||
| 102 | if ($currentRoute != $redirectRoute) { |
||||||||
| 103 | abort(redirect($redirectRoute)); |
||||||||
|
0 ignored issues
–
show
The function
redirect was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 104 | } |
||||||||
| 105 | break; |
||||||||
| 106 | |||||||||
| 107 | case 'abort': |
||||||||
| 108 | default: |
||||||||
| 109 | abort(config('laravelblocker.blockerDefaultActionAbortType')); |
||||||||
| 110 | break; |
||||||||
| 111 | } |
||||||||
| 112 | break; |
||||||||
| 113 | } |
||||||||
| 114 | } |
||||||||
| 115 | } |
||||||||
| 116 | |||||||||
| 117 | /** |
||||||||
| 118 | * Gets the email domain. |
||||||||
| 119 | * |
||||||||
| 120 | * @param string $email The email |
||||||||
| 121 | * |
||||||||
| 122 | * @return string The email domain. |
||||||||
| 123 | */ |
||||||||
| 124 | private static function getEmailDomain($email) |
||||||||
| 125 | { |
||||||||
| 126 | return substr(strrchr($email, '@'), 1); |
||||||||
| 127 | } |
||||||||
| 128 | |||||||||
| 129 | /** |
||||||||
| 130 | * { function_description }. |
||||||||
| 131 | * |
||||||||
| 132 | * @param string $checkAgainst The check against |
||||||||
| 133 | * @param bool $blocked The blocked |
||||||||
| 134 | * |
||||||||
| 135 | * @return bool ( description_of_the_return_value ) |
||||||||
| 136 | */ |
||||||||
| 137 | private static function checkedBlockedList($checkAgainst, $blocked) |
||||||||
| 138 | { |
||||||||
| 139 | $blockedItems = BlockedItem::all(); |
||||||||
| 140 | |||||||||
| 141 | foreach ($blockedItems as $blockedItems) { |
||||||||
| 142 | if ($blockedItems->value == $checkAgainst) { |
||||||||
| 143 | $blocked = true; |
||||||||
| 144 | } |
||||||||
| 145 | } |
||||||||
| 146 | |||||||||
| 147 | return $blocked; |
||||||||
| 148 | } |
||||||||
| 149 | } |
||||||||
| 150 |