Skip to content

Commit

Permalink
feat: add a viewScalar gate to add custom authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Sep 12, 2024
1 parent 860e81c commit 869e8c6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ return [
]
```

## Authorization

The Scalar API reference may be accessed via the /scalar route. By default, everyone will be able to access this route. However, within your App\Providers\AppServiceProvider.php file, you can overwrite the gate definition. This authorization gate controls access to Scalar in non-local environments. You are free to modify this gate as needed to restrict access to your Horizon installation:

```php
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
Gate::define('viewScalar', function ($user) {
return in_array($user->email, [
//
]);
});
}
}
```

## Testing

```bash
Expand Down
5 changes: 5 additions & 0 deletions src/Controllers/ScalarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
namespace Scalar\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Gate;

class ScalarController extends Controller
{
public function __invoke()
{
if (! Gate::check('viewScalar') && ! app()->environment('local')) {
return abort(403);
}

return view('scalar::reference');
}
}
10 changes: 10 additions & 0 deletions src/ScalarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

namespace Scalar;

use Illuminate\Support\Facades\Gate;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class ScalarServiceProvider extends PackageServiceProvider
{
public function boot()
{
parent::boot();

Gate::define('viewScalar', function ($user = null) {
return true;
});
}

public function configurePackage(Package $package): void
{
$package
Expand Down

0 comments on commit 869e8c6

Please sign in to comment.