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