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'));
40 return view('settings.webhooks.create');
44 * Store a new webhook in the system.
46 public function store(Request $request)
48 $validated = $this->validate($request, [
49 'name' => ['required', 'max:150'],
50 'endpoint' => ['required', 'url', 'max:500'],
51 'events' => ['required', 'array'],
52 'active' => ['required'],
53 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
56 $webhook = new Webhook($validated);
57 $webhook->active = $validated['active'] === 'true';
59 $webhook->updateTrackedEvents(array_values($validated['events']));
61 $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
63 return redirect('/settings/webhooks');
67 * Show the view to edit an existing webhook.
69 public function edit(string $id)
71 /** @var Webhook $webhook */
72 $webhook = Webhook::query()
73 ->with('trackedEvents')
76 $this->setPageTitle(trans('settings.webhooks_edit'));
78 return view('settings.webhooks.edit', ['webhook' => $webhook]);
82 * Update an existing webhook with the provided request data.
84 public function update(Request $request, string $id)
86 $validated = $this->validate($request, [
87 'name' => ['required', 'max:150'],
88 'endpoint' => ['required', 'url', 'max:500'],
89 'events' => ['required', 'array'],
90 'active' => ['required'],
91 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
94 /** @var Webhook $webhook */
95 $webhook = Webhook::query()->findOrFail($id);
97 $webhook->active = $validated['active'] === 'true';
98 $webhook->fill($validated)->save();
99 $webhook->updateTrackedEvents($validated['events']);
101 $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
103 return redirect('/settings/webhooks');
107 * Show the view to delete a webhook.
109 public function delete(string $id)
111 /** @var Webhook $webhook */
112 $webhook = Webhook::query()->findOrFail($id);
114 $this->setPageTitle(trans('settings.webhooks_delete'));
116 return view('settings.webhooks.delete', ['webhook' => $webhook]);
120 * Destroy a webhook from the system.
122 public function destroy(string $id)
124 /** @var Webhook $webhook */
125 $webhook = Webhook::query()->findOrFail($id);
127 $webhook->trackedEvents()->delete();
130 $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
132 return redirect('/settings/webhooks');