Раздел «Вебхуки» в панели, команды webhook:list и webhook:test
Подписки на события видны и правятся из панели: события галочками, секрет с заменой, пробная посылка, журнал доставок со страницами. Состояние показывает число подписок и недоставленных посылок.
This commit is contained in:
1 parent
fce276bb13
commit
054c1f2897
11 files changed
+825
-2
No files matched your search
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers\Admin;
|
||||
|
||||
use Rsgrinko\Proton\Http\Controller;
|
||||
use Rsgrinko\Proton\Http\Request;
|
||||
use Rsgrinko\Proton\Http\Response;
|
||||
use Rsgrinko\Proton\Models\Webhook;
|
||||
use Rsgrinko\Proton\Models\WebhookDelivery;
|
||||
use Rsgrinko\Proton\Support\Audit;
|
||||
use Rsgrinko\Proton\View\View;
|
||||
use Rsgrinko\Proton\Webhooks\Webhooks;
|
||||
|
||||
/**
|
||||
* Подписки на события: куда стучаться, о чём и с каким секретом.
|
||||
*
|
||||
* Секрет виден владельцу подписки всегда — им подписчик проверяет посылки,
|
||||
* и подсмотреть его больше негде. Заменить секрет можно, и старый перестаёт
|
||||
* подходить сразу.
|
||||
*/
|
||||
final class WebhooksController extends Controller
|
||||
{
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
return $this->view('admin/webhooks', [
|
||||
'active' => 'webhooks',
|
||||
'page' => Webhook::query()->orderBy('id', 'desc')->paginate($this->page($request), $this->perPage()),
|
||||
'groups' => Webhooks::groups(),
|
||||
'stats' => WebhookDelivery::stats(),
|
||||
], 'Вебхуки');
|
||||
}
|
||||
|
||||
public function create(): Response
|
||||
{
|
||||
return $this->view('admin/webhook', [
|
||||
'active' => 'webhooks',
|
||||
'webhook' => new Webhook(),
|
||||
'groups' => Webhooks::groups(),
|
||||
'deliveries' => ['items' => [], 'total' => 0, 'page' => 1, 'pages' => 1, 'per_page' => 0],
|
||||
'fresh' => '',
|
||||
], 'Новая подписка');
|
||||
}
|
||||
|
||||
public function store(Request $request): Response
|
||||
{
|
||||
$data = $this->validate($request, [
|
||||
'name' => 'nullable|max:191',
|
||||
'url' => 'required|url|max:500',
|
||||
'secret' => 'nullable|max:191',
|
||||
], ['name' => 'Название', 'url' => 'Адрес', 'secret' => 'Секрет']);
|
||||
|
||||
$events = Webhooks::filter((array) $request->input('events', []));
|
||||
|
||||
if ($events === []) {
|
||||
$this->flash('Выберите хотя бы одно событие', 'error');
|
||||
|
||||
return $this->redirect('admin.webhooks.create');
|
||||
}
|
||||
|
||||
$webhook = Webhook::add(
|
||||
(string) ($data['name'] ?? ''),
|
||||
(string) $data['url'],
|
||||
$events,
|
||||
(string) ($data['secret'] ?? '')
|
||||
);
|
||||
|
||||
Audit::created('webhook', $webhook->id(), 'подписка «' . (string) $webhook->name . '» на ' . (string) $webhook->url);
|
||||
|
||||
$this->flash('Подписка создана. Секрет пропишите у подписчика');
|
||||
|
||||
return $this->redirect('admin.webhooks.show', ['id' => $webhook->id()]);
|
||||
}
|
||||
|
||||
public function show(Request $request, int $id): Response
|
||||
{
|
||||
$webhook = $this->webhook($id);
|
||||
|
||||
return $this->view('admin/webhook', [
|
||||
'active' => 'webhooks',
|
||||
'webhook' => $webhook,
|
||||
'groups' => Webhooks::groups(),
|
||||
'deliveries' => WebhookDelivery::query()
|
||||
->where('webhook_id', $webhook->id())
|
||||
->orderBy('id', 'desc')
|
||||
->paginate($this->page($request), $this->perPage()),
|
||||
// Новый секрет показываем один раз, сразу после замены
|
||||
'fresh' => (string) View::takeStash('webhook_secret', ''),
|
||||
], (string) $webhook->name);
|
||||
}
|
||||
|
||||
public function update(Request $request, int $id): Response
|
||||
{
|
||||
$webhook = $this->webhook($id);
|
||||
|
||||
$data = $this->validate($request, [
|
||||
'name' => 'nullable|max:191',
|
||||
'url' => 'required|url|max:500',
|
||||
], ['name' => 'Название', 'url' => 'Адрес']);
|
||||
|
||||
$events = Webhooks::filter((array) $request->input('events', []));
|
||||
|
||||
if ($events === []) {
|
||||
$this->flash('Выберите хотя бы одно событие', 'error');
|
||||
|
||||
return $this->redirect('admin.webhooks.show', ['id' => $webhook->id()]);
|
||||
}
|
||||
|
||||
$before = [
|
||||
'name' => (string) $webhook->name,
|
||||
'url' => (string) $webhook->url,
|
||||
'events' => implode(', ', $webhook->events()),
|
||||
];
|
||||
|
||||
$webhook->forceFill([
|
||||
'name' => (string) ($data['name'] ?? '') !== '' ? (string) $data['name'] : (string) $webhook->name,
|
||||
'url' => (string) $data['url'],
|
||||
'events' => json_encode($events, JSON_UNESCAPED_UNICODE),
|
||||
])->save();
|
||||
|
||||
Audit::updated('webhook', $webhook->id(), 'подписка «' . (string) $webhook->name . '»', Audit::between($before, [
|
||||
'name' => (string) $webhook->name,
|
||||
'url' => (string) $webhook->url,
|
||||
'events' => implode(', ', $webhook->events()),
|
||||
]));
|
||||
|
||||
$this->flash('Подписка сохранена');
|
||||
|
||||
return $this->redirect('admin.webhooks.show', ['id' => $webhook->id()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Включить или отключить подписку.
|
||||
*/
|
||||
public function toggle(int $id): Response
|
||||
{
|
||||
$webhook = $this->webhook($id);
|
||||
|
||||
$active = !(bool) $webhook->active;
|
||||
|
||||
// Включили заново — счётчик неудач больше не повод отключать
|
||||
$webhook->forceFill(['active' => $active ? 1 : 0, 'failures' => 0])->save();
|
||||
|
||||
Audit::action('webhook', $webhook->id(), ($active ? 'включена' : 'отключена') . ' подписка «' . (string) $webhook->name . '»');
|
||||
|
||||
$this->flash($active ? 'Подписка включена' : 'Подписка отключена');
|
||||
|
||||
return $this->redirect('admin.webhooks.show', ['id' => $webhook->id()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Заменить секрет: старый перестаёт подходить сразу.
|
||||
*/
|
||||
public function rotate(int $id): Response
|
||||
{
|
||||
$webhook = $this->webhook($id);
|
||||
|
||||
View::stash('webhook_secret', $webhook->rotateSecret());
|
||||
|
||||
Audit::action('webhook', $webhook->id(), 'заменён секрет подписки «' . (string) $webhook->name . '»');
|
||||
|
||||
$this->flash('Секрет заменён. Пропишите новый у подписчика — старый больше не подойдёт');
|
||||
|
||||
return $this->redirect('admin.webhooks.show', ['id' => $webhook->id()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Пробная посылка: уходит очередью, как и настоящие.
|
||||
*/
|
||||
public function test(int $id): Response
|
||||
{
|
||||
$webhook = $this->webhook($id);
|
||||
|
||||
$delivery = Webhooks::test($webhook);
|
||||
|
||||
Audit::action('webhook', $webhook->id(), 'отправлена пробная посылка подписке «' . (string) $webhook->name . '»');
|
||||
|
||||
$this->flash('Пробная посылка поставлена в очередь (№' . $delivery->id() . '). Её разберёт воркер');
|
||||
|
||||
return $this->redirect('admin.webhooks.show', ['id' => $webhook->id()]);
|
||||
}
|
||||
|
||||
public function delete(int $id): Response
|
||||
{
|
||||
$webhook = $this->webhook($id);
|
||||
|
||||
$name = (string) $webhook->name;
|
||||
|
||||
// Журнал доставок без подписки не нужен
|
||||
WebhookDelivery::query()->where('webhook_id', $webhook->id())->delete();
|
||||
|
||||
$webhook->delete();
|
||||
|
||||
Audit::deleted('webhook', $id, 'удалена подписка «' . $name . '»');
|
||||
|
||||
$this->flash('Подписка удалена');
|
||||
|
||||
return $this->redirect('admin.webhooks');
|
||||
}
|
||||
|
||||
private function webhook(int $id): Webhook
|
||||
{
|
||||
/** @var Webhook $webhook */
|
||||
$webhook = $this->require(Webhook::find($id), 'admin.webhooks', 'Подписка не найдена');
|
||||
|
||||
return $webhook;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user