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