Skip to content

Commit 869e8c6

Browse files
committed
feat: add a viewScalar gate to add custom authorization
1 parent 860e81c commit 869e8c6

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,31 @@ return [
4747
]
4848
```
4949

50+
## Authorization
51+
52+
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:
53+
54+
```php
55+
<?php
56+
57+
namespace App\Providers;
58+
59+
use Illuminate\Support\Facades\Gate;
60+
use Illuminate\Support\ServiceProvider;
61+
62+
class AppServiceProvider extends ServiceProvider
63+
{
64+
public function boot(): void
65+
{
66+
Gate::define('viewScalar', function ($user) {
67+
return in_array($user->email, [
68+
//
69+
]);
70+
});
71+
}
72+
}
73+
```
74+
5075
## Testing
5176

5277
```bash

src/Controllers/ScalarController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
namespace Scalar\Controllers;
44

55
use Illuminate\Routing\Controller;
6+
use Illuminate\Support\Facades\Gate;
67

78
class ScalarController extends Controller
89
{
910
public function __invoke()
1011
{
12+
if (! Gate::check('viewScalar') && ! app()->environment('local')) {
13+
return abort(403);
14+
}
15+
1116
return view('scalar::reference');
1217
}
1318
}

src/ScalarServiceProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22

33
namespace Scalar;
44

5+
use Illuminate\Support\Facades\Gate;
56
use Spatie\LaravelPackageTools\Package;
67
use Spatie\LaravelPackageTools\PackageServiceProvider;
78

89
class ScalarServiceProvider extends PackageServiceProvider
910
{
11+
public function boot()
12+
{
13+
parent::boot();
14+
15+
Gate::define('viewScalar', function ($user = null) {
16+
return true;
17+
});
18+
}
19+
1020
public function configurePackage(Package $package): void
1121
{
1222
$package

0 commit comments

Comments
 (0)