Skip to content

Commit 050075b

Browse files
committed
Cleanup code in MountController.php
1 parent f7520b7 commit 050075b

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

app/Http/Controllers/Admin/MountController.php

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Http\Controllers\Admin;
44

55
use Ramsey\Uuid\Uuid;
6+
use Illuminate\Support\Str;
67
use Illuminate\Http\Request;
78
use Pterodactyl\Models\Nest;
89
use Pterodactyl\Models\Mount;
@@ -101,28 +102,21 @@ public function view($id)
101102
*/
102103
public function create(MountFormRequest $request)
103104
{
104-
/** @var \Pterodactyl\Models\Mount $mount */
105105
$model = (new Mount())->fill($request->validated());
106106
$model->forceFill(['uuid' => Uuid::uuid4()->toString()]);
107107

108-
if (str_starts_with($model->source, '/etc/pterodactyl')) {
109-
$this->alert->danger('Invalid source path: "/etc/pterodactyl" cannot be used as a source path.')->flash();
110-
return redirect()->route('admin.mounts');
108+
foreach (Mount::$invalidSourcePaths as $path) {
109+
if (Str::startsWith($model->source, $path)) {
110+
$this->alert->danger('"' . $path . '" cannot be used as a source path.')->flash();
111+
return redirect()->route('admin.mounts');
112+
}
111113
}
112114

113-
if (str_starts_with($model->source, '/var/lib/pterodactyl/volumes')) {
114-
$this->alert->danger('Invalid source path: "/var/lib/pterodactyl/volumes" cannot be used as a source path.')->flash();
115-
return redirect()->route('admin.mounts');
116-
}
117-
118-
if (str_starts_with($model->source, '/srv/daemon-data')) {
119-
$this->alert->danger('Invalid source path: "/srv/daemon-data" cannot be used as a source path.')->flash();
120-
return redirect()->route('admin.mounts');
121-
}
122-
123-
if (str_starts_with($model->target, '/home/container')) {
124-
$this->alert->danger('Invalid target path: "/home/container" cannot be used as a target path.')->flash();
125-
return redirect()->route('admin.mounts');
115+
foreach (Mount::$invalidTargetPaths as $path) {
116+
if (Str::startsWith($model->target, $path)) {
117+
$this->alert->danger('"' . $path . '" cannot be used as a target path.')->flash();
118+
return redirect()->route('admin.mounts');
119+
}
126120
}
127121

128122
$model->saveOrFail();
@@ -150,24 +144,18 @@ public function update(MountFormRequest $request, Mount $mount)
150144

151145
$mount->forceFill($request->validated());
152146

153-
if (str_starts_with($mount->source, '/etc/pterodactyl')) {
154-
$this->alert->danger('Invalid source path: "/etc/pterodactyl" cannot be used as a source path.')->flash();
155-
return redirect()->route('admin.mounts.view', $mount->id);
156-
}
157-
158-
if (str_starts_with($mount->source, '/var/lib/pterodactyl/volumes')) {
159-
$this->alert->danger('Invalid source path: "/var/lib/pterodactyl/volumes" cannot be used as a source path.')->flash();
160-
return redirect()->route('admin.mounts.view', $mount->id);
161-
}
162-
163-
if (str_starts_with($mount->source, '/srv/daemon-data')) {
164-
$this->alert->danger('Invalid source path: "/srv/daemon-data" cannot be used as a source path.')->flash();
165-
return redirect()->route('admin.mounts.view', $mount->id);
147+
foreach (Mount::$invalidSourcePaths as $path) {
148+
if (Str::startsWith($mount->source, $path)) {
149+
$this->alert->danger('"' . $path . '" cannot be used as a source path.')->flash();
150+
return redirect()->route('admin.mounts.view', $mount->id);
151+
}
166152
}
167153

168-
if (str_starts_with($mount->target, '/home/container')) {
169-
$this->alert->danger('Invalid target path: "/home/container" cannot be used as a target path.')->flash();
170-
return redirect()->route('admin.mounts.view', $mount->id);
154+
foreach (Mount::$invalidTargetPaths as $path) {
155+
if (Str::startsWith($mount->target, $path)) {
156+
$this->alert->danger('"' . $path . '" cannot be used as a target path.')->flash();
157+
return redirect()->route('admin.mounts.view', $mount->id);
158+
}
171159
}
172160

173161
$mount->save();

app/Models/Mount.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ class Mount extends Model
7070
*/
7171
public $timestamps = false;
7272

73+
/**
74+
* Blacklisted source paths
75+
*
76+
* @var string[]
77+
*/
78+
public static $invalidSourcePaths = [
79+
'/etc/pterodactyl',
80+
'/var/lib/pterodactyl/volumes',
81+
'/srv/daemon-data',
82+
];
83+
84+
/**
85+
* Blacklisted target paths
86+
*
87+
* @var string[]
88+
*/
89+
public static $invalidTargetPaths = [
90+
'/home/container',
91+
];
92+
7393
/**
7494
* Returns all eggs that have this mount assigned.
7595
*

0 commit comments

Comments
 (0)