]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/WebhookController.php
c72dcc51066d4af80bef78c6b2cb9340d1960962
[bookstack] / app / Http / Controllers / WebhookController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\Queries\WebhooksAllPaginatedAndSorted;
7 use BookStack\Actions\Webhook;
8 use BookStack\Util\SimpleListOptions;
9 use Illuminate\Http\Request;
10
11 class WebhookController extends Controller
12 {
13     public function __construct()
14     {
15         $this->middleware([
16             'can:settings-manage',
17         ]);
18     }
19
20     /**
21      * Show all webhooks configured in the system.
22      */
23     public function index(Request $request)
24     {
25         $listOptions = SimpleListOptions::fromRequest($request, 'webhooks')->withSortOptions([
26             'name' => trans('common.sort_name'),
27             'endpoint'  => trans('settings.webhooks_endpoint'),
28             'created_at' => trans('common.sort_created_at'),
29             'updated_at' => trans('common.sort_updated_at'),
30             'active'     => trans('common.status'),
31         ]);
32
33         $webhooks = (new WebhooksAllPaginatedAndSorted())->run(20, $listOptions);
34         $webhooks->appends($listOptions->getPaginationAppends());
35
36         $this->setPageTitle(trans('settings.webhooks'));
37
38         return view('settings.webhooks.index', [
39             'webhooks'    => $webhooks,
40             'listOptions' => $listOptions,
41         ]);
42     }
43
44     /**
45      * Show the view for creating a new webhook in the system.
46      */
47     public function create()
48     {
49         $this->setPageTitle(trans('settings.webhooks_create'));
50
51         return view('settings.webhooks.create');
52     }
53
54     /**
55      * Store a new webhook in the system.
56      */
57     public function store(Request $request)
58     {
59         $validated = $this->validate($request, [
60             'name'     => ['required', 'max:150'],
61             'endpoint' => ['required', 'url', 'max:500'],
62             'events'   => ['required', 'array'],
63             'active'   => ['required'],
64             'timeout'  => ['required', 'integer', 'min:1', 'max:600'],
65         ]);
66
67         $webhook = new Webhook($validated);
68         $webhook->active = $validated['active'] === 'true';
69         $webhook->save();
70         $webhook->updateTrackedEvents(array_values($validated['events']));
71
72         $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
73
74         return redirect('/settings/webhooks');
75     }
76
77     /**
78      * Show the view to edit an existing webhook.
79      */
80     public function edit(string $id)
81     {
82         /** @var Webhook $webhook */
83         $webhook = Webhook::query()
84             ->with('trackedEvents')
85             ->findOrFail($id);
86
87         $this->setPageTitle(trans('settings.webhooks_edit'));
88
89         return view('settings.webhooks.edit', ['webhook' => $webhook]);
90     }
91
92     /**
93      * Update an existing webhook with the provided request data.
94      */
95     public function update(Request $request, string $id)
96     {
97         $validated = $this->validate($request, [
98             'name'     => ['required', 'max:150'],
99             'endpoint' => ['required', 'url', 'max:500'],
100             'events'   => ['required', 'array'],
101             'active'   => ['required'],
102             'timeout'  => ['required', 'integer', 'min:1', 'max:600'],
103         ]);
104
105         /** @var Webhook $webhook */
106         $webhook = Webhook::query()->findOrFail($id);
107
108         $webhook->active = $validated['active'] === 'true';
109         $webhook->fill($validated)->save();
110         $webhook->updateTrackedEvents($validated['events']);
111
112         $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
113
114         return redirect('/settings/webhooks');
115     }
116
117     /**
118      * Show the view to delete a webhook.
119      */
120     public function delete(string $id)
121     {
122         /** @var Webhook $webhook */
123         $webhook = Webhook::query()->findOrFail($id);
124
125         $this->setPageTitle(trans('settings.webhooks_delete'));
126
127         return view('settings.webhooks.delete', ['webhook' => $webhook]);
128     }
129
130     /**
131      * Destroy a webhook from the system.
132      */
133     public function destroy(string $id)
134     {
135         /** @var Webhook $webhook */
136         $webhook = Webhook::query()->findOrFail($id);
137
138         $webhook->trackedEvents()->delete();
139         $webhook->delete();
140
141         $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
142
143         return redirect('/settings/webhooks');
144     }
145 }