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