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 1 commit
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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ DOCKER_HOST_USER_ID=1000
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GITHUB_REDIRECT_URL=
GITHUB_APP_ID=918356
GITHUB_APP_URL=https://github.com/apps/gha-analyzer
22 changes: 22 additions & 0 deletions app/Http/Middleware/EnsurePermissionsAreGiven.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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
{
if ((new PermissionService())->checkPermissions() === false) {
return redirect(env("GITHUB_APP_URL"));
}

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 checkPermissions(): bool
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
public function checkPermissions(): bool
public function checkGitHubAppInstallation(): bool

More described what that function do.

{
$permissions_given = false;
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
$permissions_given = false;
$permissionsGiven = false;


$response = Http::withHeaders([
"Authorization" => "Bearer " . auth()->user()->github_token,
])->get("https://api.github.com/user/installations");

if ($response->json("installations") !== null) {
foreach ($response->json("installations") as $installation) {
if ($installation["app_id"] === intval(env("GITHUB_APP_ID"))) {
Copy link
Member

Choose a reason for hiding this comment

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

Lets move env value into config, config can be cached.

$permissions_given = true;

break;
}
}
}

return $permissions_given;
}
}
Loading