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