Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#22 - Add permission handling #24

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/Http/Middleware/EnsurePermissionsAreGiven.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use App\Services\PermissionService;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsurePermissionsAreGiven
{
public function handle(Request $request, Closure $next): Response
{
$organizationId = intval($request->organizationId);

if ((new PermissionService())->checkGitHubAppInstallation($organizationId) === false) {
return redirect("/");
}

return $next($request);
}
}
31 changes: 31 additions & 0 deletions app/Services/PermissionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Services;

use Illuminate\Support\Facades\Http;

class PermissionService
{
public function checkGitHubAppInstallation(int $organizationId): bool
{
$permissionsGiven = false;

$response = Http::withHeaders([
"Authorization" => "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;
}
}
4 changes: 4 additions & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +14,9 @@
health: "/up",
)
->withMiddleware(function (Middleware $middleware): void {
$middleware->alias([
"permissions" => EnsurePermissionsAreGiven::class,
]);
})
->withExceptions(function (Exceptions $exceptions): void {
})->create();
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"client_id" => env("GITHUB_CLIENT_ID"),
"client_secret" => env("GITHUB_CLIENT_SECRET"),
"redirect" => env("GITHUB_REDIRECT_URL"),
"app_id" => 918356,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"app_id" => 918356,
"app_id" => env("GITHUB_APP_ID"),

],
];
Loading