forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppServiceProvider.php
More file actions
113 lines (99 loc) · 2.89 KB
/
AppServiceProvider.php
File metadata and controls
113 lines (99 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace App\Providers;
use App\Enums\UserRole;
use App\Models\User;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* The path to your application's "home" route.
*
* Typically, users are redirected here after authentication.
*
* @var string
*/
public const HOME = '/';
/**
* Register any application services.
*/
public function register(): void
{
if ($this->app->environment('local')) {
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
$this->app->register(TelescopeServiceProvider::class);
}
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
$this->defineCustomIfStatements();
$this->defineGates();
$this->forceHttps();
$this->setApiRateLimit();
AboutCommand::add('Speedtest Tracker', fn () => [
'Version' => config('speedtest.build_version'),
]);
}
/**
* Define custom if statements, these were added to make the blade templates more readable.
*
* Ref: https://github.com/laravel/framework/pull/51561
*/
protected function defineCustomIfStatements(): void
{
/**
* Adds blank() custom if statement.
*/
Blade::if('blank', function (mixed $value) {
return blank($value);
});
/**
* Adds filled() custom if statement.
*/
Blade::if('filled', function (mixed $value) {
return filled($value);
});
}
/**
* Define any application gates.
*/
protected function defineGates(): void
{
Gate::define('access-admin-panel', function (User $user) {
return in_array($user->role, [UserRole::Admin, UserRole::User]);
});
Gate::define('view-dashboard', function (?User $user) {
if (config('speedtest.public_dashboard')) {
return true;
}
if ($user === null) {
return false;
}
return in_array($user->role, [UserRole::Admin, UserRole::User]);
});
}
/**
* Force https scheme in non-local environments.
*/
protected function forceHttps(): void
{
if (! app()->environment('local') && config('app.force_https')) {
URL::forceScheme('https');
}
}
protected function setApiRateLimit(): void
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(config('api.rate_limit'));
});
}
}