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