diff --git a/app/Http/Middleware/EnsurePermissionsAreGiven.php b/app/Http/Middleware/EnsurePermissionsAreGiven.php new file mode 100644 index 00000000..df264a76 --- /dev/null +++ b/app/Http/Middleware/EnsurePermissionsAreGiven.php @@ -0,0 +1,24 @@ +organizationId); + + if ((new PermissionService())->checkGitHubAppInstallation($organizationId) === false) { + return redirect("/"); + } + + return $next($request); + } +} diff --git a/app/Services/PermissionService.php b/app/Services/PermissionService.php new file mode 100644 index 00000000..51c7394b --- /dev/null +++ b/app/Services/PermissionService.php @@ -0,0 +1,31 @@ + "Bearer " . auth()->user()->github_token, + ])->get("https://api.github.com/user/orgs"); + + if ($response->json() !== null) { + foreach ($response->json() as $organization) { + if ($organization["id"] === $organizationId) { + $permissionsGiven = true; + + break; + } + } + } + + return $permissionsGiven; + } +} diff --git a/bootstrap/app.php b/bootstrap/app.php index 91570f99..3ab6f2fd 100755 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use App\Http\Middleware\EnsurePermissionsAreGiven; use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; @@ -13,6 +14,9 @@ health: "/up", ) ->withMiddleware(function (Middleware $middleware): void { + $middleware->alias([ + "permissions" => EnsurePermissionsAreGiven::class, + ]); }) ->withExceptions(function (Exceptions $exceptions): void { })->create(); diff --git a/config/services.php b/config/services.php index 4c9f2d64..830d09e1 100755 --- a/config/services.php +++ b/config/services.php @@ -21,5 +21,6 @@ "client_id" => env("GITHUB_CLIENT_ID"), "client_secret" => env("GITHUB_CLIENT_SECRET"), "redirect" => env("GITHUB_REDIRECT_URL"), + "app_id" => 918356, ], ];