Правила доступа на запись и подписанные ссылки
This commit is contained in:
1 parent
9508c29964
commit
3a04ee5bb3
14 files changed
+638
-10
No files matched your search
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Rsgrinko\Proton\Http;
|
||||
|
||||
use Rsgrinko\Proton\Access\AccessDenied;
|
||||
use Rsgrinko\Proton\Access\Policy;
|
||||
use Rsgrinko\Proton\Database\Model\Model;
|
||||
use Rsgrinko\Proton\Database\Model\RecordNotFound;
|
||||
use Rsgrinko\Proton\Database\Query\Builder;
|
||||
@@ -93,6 +95,20 @@ abstract class Controller
|
||||
return Validator::make($request->all(), $rules, $labels)->validate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Правило на запись: можно ли этому человеку трогать именно её.
|
||||
* Нельзя — 403, дальше код не идёт.
|
||||
*
|
||||
* Право у группы маршрутов решает, пускать ли в раздел; здесь решается
|
||||
* судьба конкретной строки, поэтому вызов стоит рядом с ней.
|
||||
*/
|
||||
protected function authorize(string $ability, mixed $subject = null): void
|
||||
{
|
||||
if (Policy::denies($ability, $subject)) {
|
||||
throw new AccessDenied('С этой записью так нельзя');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Запись обязана быть, иначе — «не найдено» с возвратом в список.
|
||||
*
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Rsgrinko\Proton\Http;
|
||||
|
||||
use Rsgrinko\Proton\Access\AccessDenied;
|
||||
use Rsgrinko\Proton\Auth\Auth;
|
||||
use Rsgrinko\Proton\Auth\Csrf;
|
||||
use Rsgrinko\Proton\Database\Model\RecordNotFound;
|
||||
@@ -12,6 +13,7 @@ use Rsgrinko\Proton\Http\Middleware\Authenticate;
|
||||
use Rsgrinko\Proton\Http\Middleware\Can;
|
||||
use Rsgrinko\Proton\Http\Middleware\Guest;
|
||||
use Rsgrinko\Proton\Http\Middleware\Install;
|
||||
use Rsgrinko\Proton\Http\Middleware\Signed;
|
||||
use Rsgrinko\Proton\Http\Middleware\Throttle;
|
||||
use Rsgrinko\Proton\Http\Middleware\VerifyCsrf;
|
||||
use Rsgrinko\Proton\Support\Config;
|
||||
@@ -54,6 +56,8 @@ final class Kernel
|
||||
|
||||
try {
|
||||
$response = $this->finish($this->router()->dispatch($request));
|
||||
} catch (AccessDenied $e) {
|
||||
$response = $this->finish($this->denied($request, $e));
|
||||
} catch (RecordNotFound $e) {
|
||||
$response = $this->finish($this->notFound($request, $e));
|
||||
} catch (ValidationException $e) {
|
||||
@@ -91,7 +95,8 @@ final class Kernel
|
||||
->middleware('can', new Can())
|
||||
->middleware('api', new ApiKey())
|
||||
->middleware('throttle', new Throttle())
|
||||
->middleware('install', new Install());
|
||||
->middleware('install', new Install())
|
||||
->middleware('signed', new Signed());
|
||||
|
||||
foreach ((array) Config::get('routes', ['routes/web.php']) as $file) {
|
||||
$router->load(APP_ROOT . '/' . ltrim((string) $file, '/'));
|
||||
@@ -111,6 +116,18 @@ final class Kernel
|
||||
->withHeader(RequestId::HEADER, RequestId::current());
|
||||
}
|
||||
|
||||
/**
|
||||
* Правило на запись не пустило.
|
||||
*/
|
||||
private function denied(Request $request, AccessDenied $e): Response
|
||||
{
|
||||
if ($request->wantsJson()) {
|
||||
return Response::error($e->getMessage(), 403);
|
||||
}
|
||||
|
||||
return Response::html(View::render('errors/403', ['message' => $e->getMessage()], 'Нет доступа'), 403);
|
||||
}
|
||||
|
||||
private function notFound(Request $request, RecordNotFound $e): Response
|
||||
{
|
||||
if ($request->wantsJson()) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rsgrinko\Proton\Http\Middleware;
|
||||
|
||||
use Rsgrinko\Proton\Http\Request;
|
||||
use Rsgrinko\Proton\Http\Response;
|
||||
use Rsgrinko\Proton\Support\SignedUrl;
|
||||
use Rsgrinko\Proton\View\View;
|
||||
|
||||
/**
|
||||
* Пускает только по целой и не протухшей подписи: signed.
|
||||
*
|
||||
* Ставится там, где адрес открывают без входа — файл из письма, готовая
|
||||
* выгрузка. Вместе с `auth` смысла не имеет: если человек и так вошёл,
|
||||
* подпись ничего не добавляет.
|
||||
*/
|
||||
final class Signed
|
||||
{
|
||||
public function __invoke(Request $request, callable $next): Response
|
||||
{
|
||||
if (SignedUrl::valid($request)) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
if ($request->wantsJson()) {
|
||||
return Response::error('Ссылка недействительна или устарела', 403);
|
||||
}
|
||||
|
||||
return Response::html(View::render('errors/403', [
|
||||
'message' => 'Ссылка недействительна или устарела. Попросите её заново.',
|
||||
], 'Ссылка не работает'), 403);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user