Skip to content

Commit 069d7c2

Browse files
authored
Cleaned up panel acl (alexjustesen#2375)
Co-authored-by: Alex Justesen <[email protected]>
1 parent 3bc99c2 commit 069d7c2

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

app/Http/Middleware/PublicDashboard.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Http\Request;
7+
use Illuminate\Support\Facades\Gate;
78
use Symfony\Component\HttpFoundation\Response;
89

910
class PublicDashboard
@@ -15,7 +16,7 @@ class PublicDashboard
1516
*/
1617
public function handle(Request $request, Closure $next): Response
1718
{
18-
if (! config('speedtest.public_dashboard')) {
19+
if (Gate::denies('view-dashboard')) {
1920
return redirect()->route('filament.admin.auth.login');
2021
}
2122

app/Livewire/Topbar/RunSpeedtestAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public function dashboardAction(): Action
2727
->icon('heroicon-o-chart-bar')
2828
->iconPosition(IconPosition::Before)
2929
->color('gray')
30-
->hidden(fn (): bool => ! config('speedtest.public_dashboard'))
31-
->url(shouldOpenInNewTab: true, url: '/')
30+
->url(shouldOpenInNewTab: true, url: route('home'))
3231
->extraAttributes([
3332
'id' => 'dashboardAction',
3433
]);

app/Models/User.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class User extends Authenticatable implements FilamentUser
3636
protected $hidden = [
3737
'password',
3838
'remember_token',
39+
'role',
3940
];
4041

4142
/**

app/Providers/AppServiceProvider.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace App\Providers;
44

5+
use App\Enums\UserRole;
6+
use App\Models\User;
57
use Illuminate\Cache\RateLimiting\Limit;
68
use Illuminate\Foundation\Console\AboutCommand;
79
use Illuminate\Http\Request;
810
use Illuminate\Support\Facades\Blade;
11+
use Illuminate\Support\Facades\Gate;
912
use Illuminate\Support\Facades\RateLimiter;
1013
use Illuminate\Support\Facades\URL;
1114
use Illuminate\Support\ServiceProvider;
@@ -38,12 +41,10 @@ public function register(): void
3841
public function boot(): void
3942
{
4043
$this->defineCustomIfStatements();
44+
$this->defineGates();
45+
$this->forceHttps();
4146
$this->setApiRateLimit();
4247

43-
if (config('app.force_https')) {
44-
URL::forceScheme('https');
45-
}
46-
4748
AboutCommand::add('Speedtest Tracker', fn () => [
4849
'Version' => config('speedtest.build_version'),
4950
]);
@@ -71,6 +72,38 @@ protected function defineCustomIfStatements(): void
7172
});
7273
}
7374

75+
/**
76+
* Define any application gates.
77+
*/
78+
protected function defineGates(): void
79+
{
80+
Gate::define('access-admin-panel', function (User $user) {
81+
return in_array($user->role, [UserRole::Admin, UserRole::User]);
82+
});
83+
84+
Gate::define('view-dashboard', function (?User $user) {
85+
if (config('speedtest.public_dashboard')) {
86+
return true;
87+
}
88+
89+
if ($user === null) {
90+
return false;
91+
}
92+
93+
return in_array($user->role, [UserRole::Admin, UserRole::User]);
94+
});
95+
}
96+
97+
/**
98+
* Force https scheme in non-local environments.
99+
*/
100+
protected function forceHttps(): void
101+
{
102+
if (! app()->environment('local') && config('app.force_https')) {
103+
URL::forceScheme('https');
104+
}
105+
}
106+
74107
protected function setApiRateLimit(): void
75108
{
76109
RateLimiter::for('api', function (Request $request) {

0 commit comments

Comments
 (0)