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')
28 $this->setPageTitle(trans('settings.webhooks'));
30 return view('settings.webhooks.index', ['webhooks' => $webhooks]);
34 * Show the view for creating a new webhook in the system.
36 public function create()
38 $this->setPageTitle(trans('settings.webhooks_create'));
39 return view('settings.webhooks.create');
43 * Store a new webhook in the system.
45 public function store(Request $request)
47 $validated = $this->validate($request, [
48 'name' => ['required', 'max:150'],
49 'endpoint' => ['required', 'url', 'max:500'],
50 'events' => ['required', 'array'],
51 'active' => ['required'],
52 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
55 $webhook = new Webhook($validated);
56 $webhook->active = $validated['active'] === 'true';
58 $webhook->updateTrackedEvents(array_values($validated['events']));
60 $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
62 return redirect('/settings/webhooks');
66 * Show the view to edit an existing webhook.
68 public function edit(string $id)
70 /** @var Webhook $webhook */
71 $webhook = Webhook::query()
72 ->with('trackedEvents')
75 $this->setPageTitle(trans('settings.webhooks_edit'));
77 return view('settings.webhooks.edit', ['webhook' => $webhook]);
81 * Update an existing webhook with the provided request data.
83 public function update(Request $request, string $id)
85 $validated = $this->validate($request, [
86 'name' => ['required', 'max:150'],
87 'endpoint' => ['required', 'url', 'max:500'],
88 'events' => ['required', 'array'],
89 'active' => ['required'],
90 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
93 /** @var Webhook $webhook */
94 $webhook = Webhook::query()->findOrFail($id);
96 $webhook->active = $validated['active'] === 'true';
97 $webhook->fill($validated)->save();
98 $webhook->updateTrackedEvents($validated['events']);
100 $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
102 return redirect('/settings/webhooks');
106 * Show the view to delete a webhook.
108 public function delete(string $id)
110 /** @var Webhook $webhook */
111 $webhook = Webhook::query()->findOrFail($id);
113 $this->setPageTitle(trans('settings.webhooks_delete'));
115 return view('settings.webhooks.delete', ['webhook' => $webhook]);
119 * Destroy a webhook from the system.
121 public function destroy(string $id)
123 /** @var Webhook $webhook */
124 $webhook = Webhook::query()->findOrFail($id);
126 $webhook->trackedEvents()->delete();
129 $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
131 return redirect('/settings/webhooks');