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'],
51 $webhook = new Webhook($validated);
52 $webhook->active = $validated['active'] === 'true';
54 $webhook->updateTrackedEvents(array_values($validated['events']));
56 $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
58 return redirect('/settings/webhooks');
62 * Show the view to edit an existing webhook.
64 public function edit(string $id)
66 /** @var Webhook $webhook */
67 $webhook = Webhook::query()
68 ->with('trackedEvents')
71 return view('settings.webhooks.edit', ['webhook' => $webhook]);
75 * Update an existing webhook with the provided request data.
77 public function update(Request $request, string $id)
79 $validated = $this->validate($request, [
80 'name' => ['required', 'max:150'],
81 'endpoint' => ['required', 'url', 'max:500'],
82 'events' => ['required', 'array'],
83 'active' => ['required'],
86 /** @var Webhook $webhook */
87 $webhook = Webhook::query()->findOrFail($id);
89 $webhook->active = $validated['active'] === 'true';
90 $webhook->fill($validated)->save();
91 $webhook->updateTrackedEvents($validated['events']);
93 $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
95 return redirect('/settings/webhooks');
99 * Show the view to delete a webhook.
101 public function delete(string $id)
103 /** @var Webhook $webhook */
104 $webhook = Webhook::query()->findOrFail($id);
106 return view('settings.webhooks.delete', ['webhook' => $webhook]);
110 * Destroy a webhook from the system.
112 public function destroy(string $id)
114 /** @var Webhook $webhook */
115 $webhook = Webhook::query()->findOrFail($id);
117 $webhook->trackedEvents()->delete();
120 $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
122 return redirect('/settings/webhooks');