3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\Queries\WebhooksAllPaginatedAndSorted;
7 use BookStack\Actions\Webhook;
8 use Illuminate\Http\Request;
10 class WebhookController extends Controller
12 public function __construct()
15 'can:settings-manage',
20 * Show all webhooks configured in the system.
22 public function index(Request $request)
25 'search' => $request->get('search', ''),
26 'sort' => setting()->getForCurrentUser('webhooks_sort', 'name'),
27 'order' => setting()->getForCurrentUser('webhooks_sort_order', 'asc'),
30 $webhooks = (new WebhooksAllPaginatedAndSorted())->run(20, $listDetails);
31 $webhooks->appends(['search' => $listDetails['search']]);
33 $this->setPageTitle(trans('settings.webhooks'));
35 return view('settings.webhooks.index', [
36 'webhooks' => $webhooks,
37 'listDetails' => $listDetails,
42 * Show the view for creating a new webhook in the system.
44 public function create()
46 $this->setPageTitle(trans('settings.webhooks_create'));
48 return view('settings.webhooks.create');
52 * Store a new webhook in the system.
54 public function store(Request $request)
56 $validated = $this->validate($request, [
57 'name' => ['required', 'max:150'],
58 'endpoint' => ['required', 'url', 'max:500'],
59 'events' => ['required', 'array'],
60 'active' => ['required'],
61 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
64 $webhook = new Webhook($validated);
65 $webhook->active = $validated['active'] === 'true';
67 $webhook->updateTrackedEvents(array_values($validated['events']));
69 $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
71 return redirect('/settings/webhooks');
75 * Show the view to edit an existing webhook.
77 public function edit(string $id)
79 /** @var Webhook $webhook */
80 $webhook = Webhook::query()
81 ->with('trackedEvents')
84 $this->setPageTitle(trans('settings.webhooks_edit'));
86 return view('settings.webhooks.edit', ['webhook' => $webhook]);
90 * Update an existing webhook with the provided request data.
92 public function update(Request $request, string $id)
94 $validated = $this->validate($request, [
95 'name' => ['required', 'max:150'],
96 'endpoint' => ['required', 'url', 'max:500'],
97 'events' => ['required', 'array'],
98 'active' => ['required'],
99 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
102 /** @var Webhook $webhook */
103 $webhook = Webhook::query()->findOrFail($id);
105 $webhook->active = $validated['active'] === 'true';
106 $webhook->fill($validated)->save();
107 $webhook->updateTrackedEvents($validated['events']);
109 $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
111 return redirect('/settings/webhooks');
115 * Show the view to delete a webhook.
117 public function delete(string $id)
119 /** @var Webhook $webhook */
120 $webhook = Webhook::query()->findOrFail($id);
122 $this->setPageTitle(trans('settings.webhooks_delete'));
124 return view('settings.webhooks.delete', ['webhook' => $webhook]);
128 * Destroy a webhook from the system.
130 public function destroy(string $id)
132 /** @var Webhook $webhook */
133 $webhook = Webhook::query()->findOrFail($id);
135 $webhook->trackedEvents()->delete();
138 $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
140 return redirect('/settings/webhooks');