3 namespace BookStack\Activity\Controllers;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\Webhook;
7 use BookStack\Activity\Queries\WebhooksAllPaginatedAndSorted;
8 use BookStack\Http\Controller;
9 use BookStack\Util\SimpleListOptions;
10 use Illuminate\Http\Request;
12 class WebhookController extends Controller
14 public function __construct()
17 'can:settings-manage',
22 * Show all webhooks configured in the system.
24 public function index(Request $request)
26 $listOptions = SimpleListOptions::fromRequest($request, 'webhooks')->withSortOptions([
27 'name' => trans('common.sort_name'),
28 'endpoint' => trans('settings.webhooks_endpoint'),
29 'created_at' => trans('common.sort_created_at'),
30 'updated_at' => trans('common.sort_updated_at'),
31 'active' => trans('common.status'),
34 $webhooks = (new WebhooksAllPaginatedAndSorted())->run(20, $listOptions);
35 $webhooks->appends($listOptions->getPaginationAppends());
37 $this->setPageTitle(trans('settings.webhooks'));
39 return view('settings.webhooks.index', [
40 'webhooks' => $webhooks,
41 'listOptions' => $listOptions,
46 * Show the view for creating a new webhook in the system.
48 public function create()
50 $this->setPageTitle(trans('settings.webhooks_create'));
52 return view('settings.webhooks.create');
56 * Store a new webhook in the system.
58 public function store(Request $request)
60 $validated = $this->validate($request, [
61 'name' => ['required', 'max:150'],
62 'endpoint' => ['required', 'url', 'max:500'],
63 'events' => ['required', 'array'],
64 'active' => ['required'],
65 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
68 $webhook = new Webhook($validated);
69 $webhook->active = $validated['active'] === 'true';
71 $webhook->updateTrackedEvents(array_values($validated['events']));
73 $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
75 return redirect('/settings/webhooks');
79 * Show the view to edit an existing webhook.
81 public function edit(string $id)
83 /** @var Webhook $webhook */
84 $webhook = Webhook::query()
85 ->with('trackedEvents')
88 $this->setPageTitle(trans('settings.webhooks_edit'));
90 return view('settings.webhooks.edit', ['webhook' => $webhook]);
94 * Update an existing webhook with the provided request data.
96 public function update(Request $request, string $id)
98 $validated = $this->validate($request, [
99 'name' => ['required', 'max:150'],
100 'endpoint' => ['required', 'url', 'max:500'],
101 'events' => ['required', 'array'],
102 'active' => ['required'],
103 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
106 /** @var Webhook $webhook */
107 $webhook = Webhook::query()->findOrFail($id);
109 $webhook->active = $validated['active'] === 'true';
110 $webhook->fill($validated)->save();
111 $webhook->updateTrackedEvents($validated['events']);
113 $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
115 return redirect('/settings/webhooks');
119 * Show the view to delete a webhook.
121 public function delete(string $id)
123 /** @var Webhook $webhook */
124 $webhook = Webhook::query()->findOrFail($id);
126 $this->setPageTitle(trans('settings.webhooks_delete'));
128 return view('settings.webhooks.delete', ['webhook' => $webhook]);
132 * Destroy a webhook from the system.
134 public function destroy(string $id)
136 /** @var Webhook $webhook */
137 $webhook = Webhook::query()->findOrFail($id);
139 $webhook->trackedEvents()->delete();
142 $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
144 return redirect('/settings/webhooks');