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