Skip to content

Commit 3bf3f7e

Browse files
authored
Fix filemanager permissions (#508)
* Fix filemanager permissions * fix filemanager permissions * fix tests warning
1 parent e17fdbb commit 3bf3f7e

File tree

12 files changed

+56
-55
lines changed

12 files changed

+56
-55
lines changed

.github/workflows/tests.yml

+3
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,8 @@ jobs:
4040
- name: Create sqlite database
4141
run: touch storage/database-test.sqlite
4242

43+
- name: Set up the .env file
44+
run: touch .env
45+
4346
- name: Run test suite
4447
run: php artisan test

app/Facades/SSH.php

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* @method static setLog(?ServerLog $log)
1515
* @method static connect()
1616
* @method static string exec(string $command, string $log = '', int $siteId = null, ?bool $stream = false, callable $streamCallback = null)
17+
* @method static string upload(string $local, string $remote, ?string $owner = null)
18+
* @method static string download(string $local, string $remote)
19+
* @method static string write(string $path, string $content, string $owner = null)
1720
* @method static string assertExecuted(array|string $commands)
1821
* @method static string assertExecutedContains(string $command)
1922
* @method static string assertFileUploaded(string $toPath, ?string $content = null)

app/Helpers/SSH.php

+14-11
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,25 @@ public function exec(string $command, string $log = '', ?int $siteId = null, ?bo
157157
/**
158158
* @throws Throwable
159159
*/
160-
public function upload(string $local, string $remote): void
160+
public function upload(string $local, string $remote, ?string $owner = null): void
161161
{
162162
$this->log = null;
163163

164164
if (! $this->connection instanceof SFTP) {
165165
$this->connect(true);
166166
}
167167

168-
$this->connection->put($remote, $local, SFTP::SOURCE_LOCAL_FILE);
168+
$tmpName = Str::random(10).strtotime('now');
169+
$tempPath = home_path($this->user).'/'.$tmpName;
170+
171+
$this->connection->put($tempPath, $local, SFTP::SOURCE_LOCAL_FILE);
172+
173+
$this->exec(sprintf('sudo mv %s %s', $tempPath, $remote));
174+
if (! $owner) {
175+
$owner = $this->user;
176+
}
177+
$this->exec(sprintf('sudo chown %s:%s %s', $owner, $owner, $remote));
178+
$this->exec(sprintf('sudo chmod 644 %s', $remote));
169179
}
170180

171181
/**
@@ -185,22 +195,15 @@ public function download(string $local, string $remote): void
185195
/**
186196
* @throws SSHError
187197
*/
188-
public function write(string $remotePath, string $content, bool $sudo = false): void
198+
public function write(string $remotePath, string $content, ?string $owner = null): void
189199
{
190200
$tmpName = Str::random(10).strtotime('now');
191201

192202
try {
193203
/** @var FilesystemAdapter $storageDisk */
194204
$storageDisk = Storage::disk('local');
195-
196205
$storageDisk->put($tmpName, $content);
197-
198-
if ($sudo) {
199-
$this->upload($storageDisk->path($tmpName), sprintf('/home/%s/%s', $this->server->ssh_user, $tmpName));
200-
$this->exec(sprintf('sudo mv /home/%s/%s %s', $this->server->ssh_user, $tmpName, $remotePath));
201-
} else {
202-
$this->upload($storageDisk->path($tmpName), $remotePath);
203-
}
206+
$this->upload($storageDisk->path($tmpName), $remotePath, $owner);
204207
} catch (Throwable $e) {
205208
throw new SSHCommandError(
206209
message: $e->getMessage()

app/SSH/OS/OS.php

+5-35
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
namespace App\SSH\OS;
44

55
use App\Exceptions\SSHError;
6-
use App\Exceptions\SSHUploadFailed;
76
use App\Models\Server;
87
use App\Models\ServerLog;
98
use App\Models\Site;
10-
use Illuminate\Filesystem\FilesystemAdapter;
11-
use Illuminate\Support\Facades\Storage;
12-
use Illuminate\Support\Str;
13-
use Throwable;
149

1510
class OS
1611
{
@@ -178,27 +173,8 @@ public function reboot(): void
178173
}
179174

180175
/**
181-
* @throws SSHUploadFailed
182-
*/
183-
public function editFile(string $path, ?string $content = null): void
184-
{
185-
$tmpName = Str::random(10).strtotime('now');
186-
try {
187-
/** @var FilesystemAdapter $storageDisk */
188-
$storageDisk = Storage::disk('local');
189-
$storageDisk->put($tmpName, $content);
190-
$this->server->ssh()->upload(
191-
$storageDisk->path($tmpName),
192-
$path
193-
);
194-
} catch (Throwable) {
195-
throw new SSHUploadFailed;
196-
} finally {
197-
$this->deleteTempFile($tmpName);
198-
}
199-
}
200-
201-
/**
176+
* @deprecated use write() instead
177+
*
202178
* @throws SSHError
203179
*/
204180
public function editFileAs(string $path, string $user, ?string $content = null): void
@@ -349,9 +325,10 @@ public function ls(string $path, ?string $user = null): string
349325
*/
350326
public function write(string $path, string $content, ?string $user = null): void
351327
{
352-
$this->server->ssh($user)->write(
328+
$this->server->ssh()->write(
353329
$path,
354-
$content
330+
$content,
331+
$user
355332
);
356333
}
357334

@@ -362,11 +339,4 @@ public function mkdir(string $path, ?string $user = null): string
362339
{
363340
return $this->server->ssh($user)->exec('mkdir -p '.$path);
364341
}
365-
366-
private function deleteTempFile(string $name): void
367-
{
368-
if (Storage::disk('local')->exists($name)) {
369-
Storage::disk('local')->delete($name);
370-
}
371-
}
372342
}

app/SSH/Services/PHP/PHP.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function createFpmPool(string $user, string $version, $site_id): void
135135
'user' => $user,
136136
'version' => $version,
137137
]),
138-
true
138+
'root'
139139
);
140140

141141
$this->service->server->systemd()->restart($this->service->unit);

app/SSH/Services/ProcessManager/Supervisor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function create(
5555
'numprocs' => (string) $numprocs,
5656
'logFile' => $logFile,
5757
]),
58-
true
58+
'root'
5959
);
6060

6161
$this->service->server->ssh()->exec(

app/SSH/Services/Webserver/Nginx.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function install(): void
2626
view('ssh.services.webserver.nginx.nginx', [
2727
'user' => $this->service->server->getSshUser(),
2828
]),
29-
true
29+
'root'
3030
);
3131

3232
$this->service->server->systemd()->restart('nginx');
@@ -83,7 +83,7 @@ public function createVHost(Site $site): void
8383
view('ssh.services.webserver.nginx.vhost', [
8484
'site' => $site,
8585
]),
86-
true
86+
'root'
8787
);
8888

8989
$this->service->server->ssh()->exec(
@@ -108,7 +108,7 @@ public function updateVHost(Site $site, ?string $vhost = null): void
108108
$vhost ?? view('ssh.services.webserver.nginx.vhost', [
109109
'site' => $site,
110110
]),
111-
true
111+
'root'
112112
);
113113

114114
$this->service->server->systemd()->restart('nginx');

app/Support/Testing/SSHFake.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function exec(string $command, string $log = '', ?int $siteId = null, ?bo
8282
return $output;
8383
}
8484

85-
public function upload(string $local, string $remote): void
85+
public function upload(string $local, string $remote, ?string $owner = null): void
8686
{
8787
$this->uploadedLocalPath = $local;
8888
$this->uploadedRemotePath = $remote;

app/Web/Pages/Servers/FileManager/Index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Index extends Page
1414

1515
public function mount(): void
1616
{
17-
$this->authorize('update', $this->server);
17+
$this->authorize('manage', $this->server);
1818
}
1919

2020
public function getWidgets(): array

app/Web/Pages/Servers/FileManager/Widgets/FilesList.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,10 @@ protected function uploadAction(): Action
269269
->after(function (array $data) {
270270
run_action($this, function () use ($data) {
271271
foreach ($data['file'] as $file) {
272-
$this->server->ssh($this->serverUser)->upload(
272+
$this->server->ssh()->upload(
273273
Storage::disk('tmp')->path($file),
274274
$this->path.'/'.$file,
275+
$this->serverUser
275276
);
276277
}
277278
$this->refresh();

app/Web/Pages/Servers/Page.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getSubNavigation(): array
6060
->url(DatabasesIndex::getUrl(parameters: ['server' => $this->server]));
6161
}
6262

63-
if (auth()->user()->can('update', $this->server)) {
63+
if (auth()->user()->can('manage', $this->server)) {
6464
$items[] = NavigationItem::make(FileManagerIndex::getNavigationLabel())
6565
->icon('heroicon-o-folder')
6666
->isActiveWhen(fn () => request()->routeIs(FileManagerIndex::getRouteName().'*'))

docker/publish.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
TAG=$1
4+
5+
if [ -z "$TAG" ]; then
6+
echo "No tag provided"
7+
exit 1
8+
fi
9+
10+
rm -rf /tmp/vito
11+
12+
git clone git@github.com:vitodeploy/vito.git /tmp/vito
13+
14+
cd /tmp/vito || exit
15+
16+
docker buildx build . \
17+
-f docker/Dockerfile \
18+
-t vitodeploy/vito:"$TAG" \
19+
--platform linux/amd64,linux/arm64 \
20+
--no-cache \
21+
--push

0 commit comments

Comments
 (0)