3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\Queries\WebhooksAllPaginatedAndSorted;
7 use BookStack\Actions\Webhook;
8 use BookStack\Util\SimpleListOptions;
9 use Illuminate\Http\Request;
11 class WebhookController extends Controller
13 public function __construct()
16 'can:settings-manage',
21 * Show all webhooks configured in the system.
23 public function index(Request $request)
25 $listOptions = SimpleListOptions::fromRequest($request, 'webhooks')->withSortOptions([
26 'name' => trans('common.sort_name'),
27 'endpoint' => trans('settings.webhooks_endpoint'),
28 'created_at' => trans('common.sort_created_at'),
29 'updated_at' => trans('common.sort_updated_at'),
30 'active' => trans('common.status'),
33 $webhooks = (new WebhooksAllPaginatedAndSorted())->run(20, $listOptions);
34 $webhooks->appends($listOptions->getPaginationAppends());
36 $this->setPageTitle(trans('settings.webhooks'));
38 return view('settings.webhooks.index', [
39 'webhooks' => $webhooks,
40 'listOptions' => $listOptions,
45 * Show the view for creating a new webhook in the system.
47 public function create()
49 $this->setPageTitle(trans('settings.webhooks_create'));
51 return view('settings.webhooks.create');
55 * Store a new webhook in the system.
57 public function store(Request $request)
59 $validated = $this->validate($request, [
60 'name' => ['required', 'max:150'],
61 'endpoint' => ['required', 'url', 'max:500'],
62 'events' => ['required', 'array'],
63 'active' => ['required'],
64 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
67 $webhook = new Webhook($validated);
68 $webhook->active = $validated['active'] === 'true';
70 $webhook->updateTrackedEvents(array_values($validated['events']));
72 $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
74 return redirect('/settings/webhooks');
78 * Show the view to edit an existing webhook.
80 public function edit(string $id)
82 /** @var Webhook $webhook */
83 $webhook = Webhook::query()
84 ->with('trackedEvents')
87 $this->setPageTitle(trans('settings.webhooks_edit'));
89 return view('settings.webhooks.edit', ['webhook' => $webhook]);
93 * Update an existing webhook with the provided request data.
95 public function update(Request $request, string $id)
97 $validated = $this->validate($request, [
98 'name' => ['required', 'max:150'],
99 'endpoint' => ['required', 'url', 'max:500'],
100 'events' => ['required', 'array'],
101 'active' => ['required'],
102 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
105 /** @var Webhook $webhook */
106 $webhook = Webhook::query()->findOrFail($id);
108 $webhook->active = $validated['active'] === 'true';
109 $webhook->fill($validated)->save();
110 $webhook->updateTrackedEvents($validated['events']);
112 $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
114 return redirect('/settings/webhooks');
118 * Show the view to delete a webhook.
120 public function delete(string $id)
122 /** @var Webhook $webhook */
123 $webhook = Webhook::query()->findOrFail($id);
125 $this->setPageTitle(trans('settings.webhooks_delete'));
127 return view('settings.webhooks.delete', ['webhook' => $webhook]);
131 * Destroy a webhook from the system.
133 public function destroy(string $id)
135 /** @var Webhook $webhook */
136 $webhook = Webhook::query()->findOrFail($id);
138 $webhook->trackedEvents()->delete();
141 $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
143 return redirect('/settings/webhooks');