67 lines
1.8 KiB
PHP
67 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Slowlyo\OwlAdmin\Admin;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class AdminPermission
|
|
{
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
|
*/
|
|
public function handle(Request $request, Closure $next, ...$args): Response
|
|
{
|
|
$name = Route::currentRouteName();
|
|
if (!$name) {
|
|
return $next($request);
|
|
}
|
|
|
|
if (Admin::config('admin.auth.permission') === false) {
|
|
return $next($request);
|
|
}
|
|
|
|
if ($request->path() == Admin::config('admin.route.prefix')) {
|
|
return $next($request);
|
|
}
|
|
|
|
$excepted = collect(Admin::config('admin.auth.except', []))
|
|
->merge(Admin::config('admin.show_development_tools') ? ['/dev_tools*'] : [])
|
|
->map(fn($path) => $this->pathFormatting($path))
|
|
->contains(fn($except) => $request->is($except == '/' ? $except : trim($except, '/')));
|
|
|
|
if ($excepted) {
|
|
return $next($request);
|
|
}
|
|
|
|
$user = Admin::user();
|
|
if (!$user) {
|
|
return $next($request);
|
|
}
|
|
if ($user->isAdministrator() || $user->can($name)) {
|
|
return $next($request);
|
|
}
|
|
|
|
return Admin::response()->fail(admin_trans('admin.unauthorized'));
|
|
}
|
|
|
|
private function pathFormatting($path)
|
|
{
|
|
$prefix = '/' . trim(Admin::config('admin.route.prefix'), '/');
|
|
|
|
$prefix = ($prefix === '/') ? '' : $prefix;
|
|
|
|
$path = trim($path, '/');
|
|
|
|
if (is_null($path) || $path === '') {
|
|
return $prefix ?: '/';
|
|
}
|
|
return $prefix . '/' . $path;
|
|
}
|
|
}
|