Pembatasan Browser Aplikasi
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Batasi akses web hanya untuk Google Chrome.
|
||||
*
|
||||
* Deteksi berdasarkan header User-Agent, jadi ini sekadar pembatas kenyamanan
|
||||
* (bukan pengaman): User-Agent bisa dipalsukan. Browser berbasis Chromium yang
|
||||
* menyamar sebagai Chrome (mis. Brave) tidak bisa dibedakan dan tetap lolos.
|
||||
*/
|
||||
class RequireChrome
|
||||
{
|
||||
/** Browser lain yang UA-nya juga memuat "Chrome/" atau "CriOS/". */
|
||||
private const BUKAN_CHROME = [
|
||||
'Edg/', 'EdgA/', 'EdgiOS/', 'OPR/', 'Opera', 'OPT/', 'SamsungBrowser',
|
||||
'Vivaldi', 'YaBrowser', 'UCBrowser', 'Firefox', 'FxiOS', 'DuckDuckGo',
|
||||
];
|
||||
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (! config('app.chrome_only') || $this->isChrome((string) $request->userAgent())) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Aplikasi ini hanya dapat diakses menggunakan browser Google Chrome.',
|
||||
], 403);
|
||||
}
|
||||
|
||||
return response()->view('errors.browser-tidak-didukung', [], 403);
|
||||
}
|
||||
|
||||
private function isChrome(string $ua): bool
|
||||
{
|
||||
if (! str_contains($ua, 'Chrome/') && ! str_contains($ua, 'CriOS/')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (self::BUKAN_CHROME as $penanda) {
|
||||
if (str_contains($ua, $penanda)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user