Skip to content

Commit d6d3313

Browse files
author
Your Name
committed
moved check to composer
1 parent 4f754d4 commit d6d3313

4 files changed

Lines changed: 88 additions & 11 deletions

File tree

DOCKER_COMPOSE_SETUP.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ This setup uses `serversideup/php:8.4-fpm-nginx` as the base image and is design
6363
docker compose exec loops php artisan passport:keys
6464
```
6565

66-
8. **Create admin user:**
66+
8. **Ensure boot-time environment:**
67+
```bash
68+
docker compose exec loops php artisan app:ensure-boottime
69+
```
70+
71+
9. **Create admin user:**
6772
```bash
6873
docker compose exec loops php artisan create-admin-account
6974
```

app/Console/Commands/ComposerPostInstallCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Console\Commands;
44

5+
use App\Services\BootstrapService;
56
use App\Services\SettingsFileService;
67
use Illuminate\Console\Command;
78
use Illuminate\Support\Facades\Cache;
@@ -33,6 +34,7 @@ public function handle()
3334

3435
Cache::forget('version_check_result');
3536
app(SettingsFileService::class)->flush();
37+
BootstrapService::ensureBoottimeEnvironment();
3638

3739
$this->info('Post-install tasks completed successfully.');
3840
} catch (\Exception $e) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Services\BootstrapService;
6+
use Illuminate\Console\Command;
7+
8+
class EnsureBoottimeEnvCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'app:ensure-boottime';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Ensure boot-time environment checks pass (directories, permissions, etc.)';
23+
24+
/**
25+
* Execute the console command.
26+
*/
27+
public function handle(): int
28+
{
29+
try {
30+
BootstrapService::ensureBoottimeEnvironment();
31+
$this->info('All boot-time environment checks passed.');
32+
33+
return Command::SUCCESS;
34+
} catch (\RuntimeException $e) {
35+
$this->error($e->getMessage());
36+
37+
return Command::FAILURE;
38+
}
39+
}
40+
}

app/Services/BootstrapService.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
11
<?php
2+
23
namespace App\Services;
34

5+
use Illuminate\Support\Facades\File;
46
use RuntimeException;
57

68
class BootstrapService
79
{
810
public static function ensureBoottimeEnvironment(): void
911
{
12+
self::checkOAuthKeyPermissions();
13+
self::ensureAvatarTempDirectory();
14+
}
15+
16+
protected static function ensureAvatarTempDirectory(): void
17+
{
18+
$path = storage_path('app/avatar-temp');
19+
20+
if (! File::isDirectory($path)) {
21+
File::makeDirectory($path, 0755, true);
1022

11-
if (app()->runningInConsole()) {
1223
return;
13-
}
24+
}
1425

15-
self::checkOAuthKeyPermissions();
26+
$perms = fileperms($path) & 0777;
27+
28+
if ($perms === 0755) {
29+
return;
30+
}
31+
32+
if (@chmod($path, 0755)) {
33+
return;
34+
}
35+
36+
throw new RuntimeException(
37+
"Avatar temp directory \"{$path}\" has incorrect permissions (" . self::formatPerms($perms) . "). " .
38+
"Expected 0755. Please run: chmod 755 {$path}"
39+
);
1640
}
1741

1842
protected static function checkOAuthKeyPermissions(): void
1943
{
2044
$privateKeyPath = storage_path('oauth-private.key');
2145
$publicKeyPath = storage_path('oauth-public.key');
22-
46+
2347
self::checkOAuthFile($privateKeyPath);
2448
self::checkOAuthFile($publicKeyPath);
2549
}
@@ -33,15 +57,15 @@ protected static function checkOAuthFile(string $filePath): void
3357
}
3458

3559
$permissions = self::getPermissions($filePath);
36-
60+
3761
$isSafe = ($permissions === '600' || $permissions === '660');
38-
62+
3963
if ($isSafe) {
4064
return;
4165
}
4266

43-
$fixed = @chmod($filePath, 0660); // Try to change the chmod to 660
44-
67+
$fixed = @chmod($filePath, 0660);
68+
4569
if ($fixed) {
4670
return;
4771
}
@@ -53,7 +77,13 @@ protected static function checkOAuthFile(string $filePath): void
5377

5478
protected static function getPermissions(string $filePath): string
5579
{
56-
$permissionNumber = fileperms($filePath) & 0777;// fileperms() returns a number with extra info we don't need & 0777 removes that extra info, leaving just the permissions
57-
return decoct($permissionNumber); // Convert to a readable string like "644" or "600" (decoct converts decimal to octal)
80+
$permissionNumber = fileperms($filePath) & 0777;
81+
82+
return decoct($permissionNumber);
83+
}
84+
85+
protected static function formatPerms(int $perms): string
86+
{
87+
return sprintf('%04o', $perms);
5888
}
5989
}

0 commit comments

Comments
 (0)