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