55 lines
1.7 KiB
PHP
55 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Services\AdminPageAccess;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
|
|
class EnsureAdminPageAccess
|
|
{
|
|
public function handle(Request $request, Closure $next)
|
|
{
|
|
$user = auth('user')->user();
|
|
if (!$user || AdminPageAccess::hasFullAccess($user)) {
|
|
return $next($request);
|
|
}
|
|
|
|
$path = strtolower(trim($request->path(), '/'));
|
|
if (str_starts_with($path, 'admin/')) {
|
|
$path = substr($path, strlen('admin/'));
|
|
}
|
|
if ($path === 'me' || $path === 'me/update') {
|
|
return $next($request);
|
|
}
|
|
|
|
$method = strtoupper($request->method());
|
|
$action = null;
|
|
if (in_array($method, ['GET', 'HEAD', 'OPTIONS'], true)) {
|
|
if (preg_match('#/active(/|$)#', $path)) {
|
|
$action = 'edit';
|
|
}
|
|
} elseif ($method === 'DELETE') {
|
|
$action = 'delete';
|
|
} elseif (in_array($method, ['PUT', 'PATCH'], true)) {
|
|
$action = 'edit';
|
|
} elseif ($method === 'POST' && !AdminPageAccess::isReadPost($path)) {
|
|
$action = 'edit';
|
|
}
|
|
|
|
if ($action === null) {
|
|
return $next($request);
|
|
}
|
|
|
|
$page = (string) $request->header('X-Admin-Page', '');
|
|
if ($page === '' || !AdminPageAccess::allows($user, $page, $action)) {
|
|
$message = $action === 'delete'
|
|
? 'شما مجوز حذف در این صفحه را ندارید'
|
|
: 'شما مجوز ویرایش در این صفحه را ندارید';
|
|
return response()->json(['message' => $message], 403);
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
}
|