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']
49 $webhook = new Webhook($validated);
51 $webhook->updateTrackedEvents(array_values($validated['events']));
53 $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
54 return redirect('/settings/webhooks');
58 * Show the view to edit an existing webhook.
60 public function edit(string $id)
62 /** @var Webhook $webhook */
63 $webhook = Webhook::query()
64 ->with('trackedEvents')
67 return view('settings.webhooks.edit', ['webhook' => $webhook]);
71 * Update an existing webhook with the provided request data.
73 public function update(Request $request, string $id)
75 $validated = $this->validate($request, [
76 'name' => ['required', 'max:150'],
77 'endpoint' => ['required', 'url', 'max:500'],
78 'events' => ['required', 'array']
81 /** @var Webhook $webhook */
82 $webhook = Webhook::query()->findOrFail($id);
84 $webhook->fill($validated)->save();
85 $webhook->updateTrackedEvents($validated['events']);
87 $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
88 return redirect('/settings/webhooks');
92 * Show the view to delete a webhook.
94 public function delete(string $id)
96 /** @var Webhook $webhook */
97 $webhook = Webhook::query()->findOrFail($id);
98 return view('settings.webhooks.delete', ['webhook' => $webhook]);
102 * Destroy a webhook from the system.
104 public function destroy(string $id)
106 /** @var Webhook $webhook */
107 $webhook = Webhook::query()->findOrFail($id);
109 $webhook->trackedEvents()->delete();
112 $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
113 return redirect('/settings/webhooks');