added pargar
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Pargar;
|
||||
use App\Jobs\LogRouteCustomerJob;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
|
||||
class LogRequests
|
||||
{
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
// زمان شروع بر حسب میلیثانیه
|
||||
$start = microtime(true);
|
||||
|
||||
try {
|
||||
// لاگ قبل از اجرای کنترلر
|
||||
$Pargar = new Pargar();
|
||||
$Pargar->ip = $request->ip();
|
||||
$Pargar->m = $request->route()?->getName() ?? 'not_found';
|
||||
//get action name from controller
|
||||
if ($route = $request->route()) {
|
||||
// تلاش برای گرفتن نام اکشن (کنترلر و متد)
|
||||
$Pargar->function_name = $route->getActionName();
|
||||
} else {
|
||||
$Pargar->function_name = 'not_found';
|
||||
}
|
||||
|
||||
|
||||
$Pargar->finance_data = json_encode($request->all(), JSON_UNESCAPED_UNICODE);
|
||||
$Pargar->finance_head = json_encode($request->headers->all(), JSON_UNESCAPED_UNICODE);
|
||||
$Pargar->url = $request->fullUrl();
|
||||
$Pargar->method = $request->method();
|
||||
$Pargar->save();
|
||||
} catch (\Throwable $e) {
|
||||
$Pargar = null; // برای اطمینان از عدم خطا در ادامه
|
||||
}
|
||||
|
||||
// اجرای درخواست اصلی
|
||||
$response = $next($request);
|
||||
|
||||
try {
|
||||
|
||||
if ($Pargar) {
|
||||
// if ($route = $request->route()) {
|
||||
// try {
|
||||
// $Pargar->m = $route
|
||||
// ? $route->getActionName() . ' (' . (new \ReflectionClass($route->getController()))->getFileName() . ')'
|
||||
// : 'unknown';
|
||||
// } catch (\Throwable $e) {
|
||||
// $Pargar->m = 'unknown';
|
||||
// }
|
||||
//
|
||||
// }
|
||||
$Pargar->response = $response->getContent();
|
||||
$Pargar->ms = round((microtime(true) - $start) * 1000, 2); // زمان در میلیثانیه
|
||||
$Pargar->save();
|
||||
}
|
||||
} catch (\Throwable $e) {}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Services\ConfigService;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class PargarAuth
|
||||
{
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
if (! app(ConfigService::class)->isPargarAccounting()) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Pargar integration is disabled',
|
||||
'data' => null,
|
||||
], 503);
|
||||
}
|
||||
|
||||
$auth = $request->header('Authorization') ?? $request->header('pargar');
|
||||
$token = is_string($auth) ? trim(str_replace('Bearer ', '', $auth)) : '';
|
||||
|
||||
if ($token === '' || ! $this->tokenValid($token)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Invalid credentials!',
|
||||
'data' => null,
|
||||
], 401);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
public static function issueToken(string $username): string
|
||||
{
|
||||
$token = bin2hex(random_bytes(32));
|
||||
Cache::put(self::cacheKey($token), $username, now()->addDay());
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
public static function tokenValid(string $token): bool
|
||||
{
|
||||
$config = app(ConfigService::class);
|
||||
$static = trim($config->getString('pargar_api_token', ''));
|
||||
if ($static !== '' && hash_equals($static, $token)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return Cache::has(self::cacheKey($token));
|
||||
}
|
||||
|
||||
private static function cacheKey(string $token): string
|
||||
{
|
||||
return 'pargar_api_token:' . $token;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user