]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/WebhookController.php
Added timeout and debugging statuses to webhooks
[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         return view('settings.webhooks.index', ['webhooks' => $webhooks]);
29     }
30
31     /**
32      * Show the view for creating a new webhook in the system.
33      */
34     public function create()
35     {
36         return view('settings.webhooks.create');
37     }
38
39     /**
40      * Store a new webhook in the system.
41      */
42     public function store(Request $request)
43     {
44         $validated = $this->validate($request, [
45             'name'     => ['required', 'max:150'],
46             'endpoint' => ['required', 'url', 'max:500'],
47             'events'   => ['required', 'array'],
48             'active'   => ['required'],
49             'timeout'  => ['required', 'integer', 'min:1', 'max:600'],
50         ]);
51
52         $webhook = new Webhook($validated);
53         $webhook->active = $validated['active'] === 'true';
54         $webhook->save();
55         $webhook->updateTrackedEvents(array_values($validated['events']));
56
57         $this->logActivity(ActivityType::WEBHOOK_CREATE, $webhook);
58
59         return redirect('/settings/webhooks');
60     }
61
62     /**
63      * Show the view to edit an existing webhook.
64      */
65     public function edit(string $id)
66     {
67         /** @var Webhook $webhook */
68         $webhook = Webhook::query()
69             ->with('trackedEvents')
70             ->findOrFail($id);
71
72         return view('settings.webhooks.edit', ['webhook' => $webhook]);
73     }
74
75     /**
76      * Update an existing webhook with the provided request data.
77      */
78     public function update(Request $request, string $id)
79     {
80         $validated = $this->validate($request, [
81             'name'     => ['required', 'max:150'],
82             'endpoint' => ['required', 'url', 'max:500'],
83             'events'   => ['required', 'array'],
84             'active'   => ['required'],
85             'timeout'  => ['required', 'integer', 'min:1', 'max:600'],
86         ]);
87
88         /** @var Webhook $webhook */
89         $webhook = Webhook::query()->findOrFail($id);
90
91         $webhook->active = $validated['active'] === 'true';
92         $webhook->fill($validated)->save();
93         $webhook->updateTrackedEvents($validated['events']);
94
95         $this->logActivity(ActivityType::WEBHOOK_UPDATE, $webhook);
96
97         return redirect('/settings/webhooks');
98     }
99
100     /**
101      * Show the view to delete a webhook.
102      */
103     public function delete(string $id)
104     {
105         /** @var Webhook $webhook */
106         $webhook = Webhook::query()->findOrFail($id);
107
108         return view('settings.webhooks.delete', ['webhook' => $webhook]);
109     }
110
111     /**
112      * Destroy a webhook from the system.
113      */
114     public function destroy(string $id)
115     {
116         /** @var Webhook $webhook */
117         $webhook = Webhook::query()->findOrFail($id);
118
119         $webhook->trackedEvents()->delete();
120         $webhook->delete();
121
122         $this->logActivity(ActivityType::WEBHOOK_DELETE, $webhook);
123
124         return redirect('/settings/webhooks');
125     }
126 }