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 return view('settings.webhooks.index', ['webhooks' => $webhooks]);
32 * Show the view for creating a new webhook in the system.
34 public function create()
36 return view('settings.webhooks.create');
40 * Store a new webhook in the system.
42 public function store(Request $request)
44 $validated = $this->validate($request, [
45 'name' => ['required', 'max:150'],
46 'endpoint' => ['required', 'url', 'max:500'],
47 'events' => ['required', 'array'],
48 'active' => ['required'],
49 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
52 $webhook = new Webhook($validated);
53 $webhook->active = $validated['active'] === 'true';
55 $webhook->updateTrackedEvents(array_values($validated['events']));
57 $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
59 return redirect('/settings/webhooks');
63 * Show the view to edit an existing webhook.
65 public function edit(string $id)
67 /** @var Webhook $webhook */
68 $webhook = Webhook::query()
69 ->with('trackedEvents')
72 return view('settings.webhooks.edit', ['webhook' => $webhook]);
76 * Update an existing webhook with the provided request data.
78 public function update(Request $request, string $id)
80 $validated = $this->validate($request, [
81 'name' => ['required', 'max:150'],
82 'endpoint' => ['required', 'url', 'max:500'],
83 'events' => ['required', 'array'],
84 'active' => ['required'],
85 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
88 /** @var Webhook $webhook */
89 $webhook = Webhook::query()->findOrFail($id);
91 $webhook->active = $validated['active'] === 'true';
92 $webhook->fill($validated)->save();
93 $webhook->updateTrackedEvents($validated['events']);
95 $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
97 return redirect('/settings/webhooks');
101 * Show the view to delete a webhook.
103 public function delete(string $id)
105 /** @var Webhook $webhook */
106 $webhook = Webhook::query()->findOrFail($id);
108 return view('settings.webhooks.delete', ['webhook' => $webhook]);
112 * Destroy a webhook from the system.
114 public function destroy(string $id)
116 /** @var Webhook $webhook */
117 $webhook = Webhook::query()->findOrFail($id);
119 $webhook->trackedEvents()->delete();
122 $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
124 return redirect('/settings/webhooks');