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