3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\Webhook;
7 use Illuminate\Http\Request;
9 class WebhookController extends Controller
11 public function __construct()
14 'can:settings-manage',
19 * Show all webhooks configured in the system.
21 public function index()
23 $webhooks = Webhook::query()
24 ->orderBy('name', 'desc')
25 ->with('trackedEvents')
27 return view('settings.webhooks.index', ['webhooks' => $webhooks]);
31 * Show the view for creating a new webhook in the system.
33 public function create()
35 return view('settings.webhooks.create');
39 * Store a new webhook in the system.
41 public function store(Request $request)
43 $validated = $this->validate($request, [
44 'name' => ['required', 'max:150'],
45 'endpoint' => ['required', 'url', 'max:500'],
46 'events' => ['required', 'array'],
47 'active' => ['required'],
50 $webhook = new Webhook($validated);
51 $webhook->active = $validated['active'] === 'true';
53 $webhook->updateTrackedEvents(array_values($validated['events']));
55 $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
56 return redirect('/settings/webhooks');
60 * Show the view to edit an existing webhook.
62 public function edit(string $id)
64 /** @var Webhook $webhook */
65 $webhook = Webhook::query()
66 ->with('trackedEvents')
69 return view('settings.webhooks.edit', ['webhook' => $webhook]);
73 * Update an existing webhook with the provided request data.
75 public function update(Request $request, string $id)
77 $validated = $this->validate($request, [
78 'name' => ['required', 'max:150'],
79 'endpoint' => ['required', 'url', 'max:500'],
80 'events' => ['required', 'array'],
81 'active' => ['required'],
84 /** @var Webhook $webhook */
85 $webhook = Webhook::query()->findOrFail($id);
87 $webhook->active = $validated['active'] === 'true';
88 $webhook->fill($validated)->save();
89 $webhook->updateTrackedEvents($validated['events']);
91 $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
92 return redirect('/settings/webhooks');
96 * Show the view to delete a webhook.
98 public function delete(string $id)
100 /** @var Webhook $webhook */
101 $webhook = Webhook::query()->findOrFail($id);
102 return view('settings.webhooks.delete', ['webhook' => $webhook]);
106 * Destroy a webhook from the system.
108 public function destroy(string $id)
110 /** @var Webhook $webhook */
111 $webhook = Webhook::query()->findOrFail($id);
113 $webhook->trackedEvents()->delete();
116 $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
117 return redirect('/settings/webhooks');