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\Permissions\Permission;
10 use BookStack\Util\SimpleListOptions;
11 use Illuminate\Http\Request;
13 class WebhookController extends Controller
15 public function __construct()
18 Permission::SettingsManage->middleware()
23 * Show all webhooks configured in the system.
25 public function index(Request $request)
27 $listOptions = SimpleListOptions::fromRequest($request, 'webhooks')->withSortOptions([
28 'name' => trans('common.sort_name'),
29 'endpoint' => trans('settings.webhooks_endpoint'),
30 'created_at' => trans('common.sort_created_at'),
31 'updated_at' => trans('common.sort_updated_at'),
32 'active' => trans('common.status'),
35 $webhooks = (new WebhooksAllPaginatedAndSorted())->run(20, $listOptions);
36 $webhooks->appends($listOptions->getPaginationAppends());
38 $this->setPageTitle(trans('settings.webhooks'));
40 return view('settings.webhooks.index', [
41 'webhooks' => $webhooks,
42 'listOptions' => $listOptions,
47 * Show the view for creating a new webhook in the system.
49 public function create()
51 $this->setPageTitle(trans('settings.webhooks_create'));
53 return view('settings.webhooks.create');
57 * Store a new webhook in the system.
59 public function store(Request $request)
61 $validated = $this->validate($request, [
62 'name' => ['required', 'max:150'],
63 'endpoint' => ['required', 'url', 'max:500'],
64 'events' => ['required', 'array'],
65 'active' => ['required'],
66 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
69 $webhook = new Webhook($validated);
70 $webhook->active = $validated['active'] === 'true';
72 $webhook->updateTrackedEvents(array_values($validated['events']));
74 $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
76 return redirect('/settings/webhooks');
80 * Show the view to edit an existing webhook.
82 public function edit(string $id)
84 /** @var Webhook $webhook */
85 $webhook = Webhook::query()
86 ->with('trackedEvents')
89 $this->setPageTitle(trans('settings.webhooks_edit'));
91 return view('settings.webhooks.edit', ['webhook' => $webhook]);
95 * Update an existing webhook with the provided request data.
97 public function update(Request $request, string $id)
99 $validated = $this->validate($request, [
100 'name' => ['required', 'max:150'],
101 'endpoint' => ['required', 'url', 'max:500'],
102 'events' => ['required', 'array'],
103 'active' => ['required'],
104 'timeout' => ['required', 'integer', 'min:1', 'max:600'],
107 /** @var Webhook $webhook */
108 $webhook = Webhook::query()->findOrFail($id);
110 $webhook->active = $validated['active'] === 'true';
111 $webhook->fill($validated)->save();
112 $webhook->updateTrackedEvents($validated['events']);
114 $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
116 return redirect('/settings/webhooks');
120 * Show the view to delete a webhook.
122 public function delete(string $id)
124 /** @var Webhook $webhook */
125 $webhook = Webhook::query()->findOrFail($id);
127 $this->setPageTitle(trans('settings.webhooks_delete'));
129 return view('settings.webhooks.delete', ['webhook' => $webhook]);
133 * Destroy a webhook from the system.
135 public function destroy(string $id)
137 /** @var Webhook $webhook */
138 $webhook = Webhook::query()->findOrFail($id);
140 $webhook->trackedEvents()->delete();
143 $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
145 return redirect('/settings/webhooks');