Init
This commit is contained in:
83
app/Providers/AppServiceProvider.php
Normal file
83
app/Providers/AppServiceProvider.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Helpers\Image;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Auth\Notifications\ResetPassword;
|
||||
use Illuminate\Auth\Notifications\VerifyEmail;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
// Register Telescope only in local environment
|
||||
if ($this->app->environment('local') && class_exists(\Laravel\Telescope\TelescopeServiceProvider::class)) {
|
||||
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
|
||||
$this->app->register(TelescopeServiceProvider::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
RateLimiter::for('api', function (Request $request) {
|
||||
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
|
||||
});
|
||||
|
||||
RateLimiter::for('verification-notification', function (Request $request) {
|
||||
return Limit::perMinute(1)->by($request->user()?->email ?: $request->ip());
|
||||
});
|
||||
|
||||
RateLimiter::for('uploads', function (Request $request) {
|
||||
return $request->user()?->hasRole('admin')
|
||||
? Limit::none()
|
||||
: Limit::perMinute(10)->by($request->ip());
|
||||
});
|
||||
|
||||
ResetPassword::createUrlUsing(function (object $notifiable, string $token) {
|
||||
return config('app.frontend_url') . "/password-reset/{$token}?email={$notifiable->getEmailForPasswordReset()}";
|
||||
});
|
||||
|
||||
VerifyEmail::createUrlUsing(function (object $notifiable) {
|
||||
$url = url()->temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(config('auth.verification.expire', 60)),
|
||||
[
|
||||
'ulid' => $notifiable->ulid,
|
||||
'hash' => sha1($notifiable->getEmailForVerification()),
|
||||
]
|
||||
);
|
||||
|
||||
return config('app.frontend_url') . '/auth/verify?verify_url=' . urlencode($url);
|
||||
});
|
||||
|
||||
/**
|
||||
* Convert uploaded image to webp, jpeg or png format and resize it
|
||||
*/
|
||||
UploadedFile::macro('convert', function (int $width = null, int $height = null, string $extension = 'webp', int $quality = 90): UploadedFile {
|
||||
return tap($this, function (UploadedFile $file) use ($width, $height, $extension, $quality) {
|
||||
Image::convert($file->path(), $file->path(), $width, $height, $extension, $quality);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Remove all special characters from a string
|
||||
*/
|
||||
Str::macro('onlyWords', function (string $text): string {
|
||||
// \p{L} matches any kind of letter from any language
|
||||
// \d matches a digit in any script
|
||||
return Str::replaceMatches('/[^\p{L}\d ]/u', '', $text);
|
||||
});
|
||||
}
|
||||
}
|
||||
62
app/Providers/TelescopeServiceProvider.php
Normal file
62
app/Providers/TelescopeServiceProvider.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Laravel\Telescope\IncomingEntry;
|
||||
use Laravel\Telescope\Telescope;
|
||||
use Laravel\Telescope\TelescopeApplicationServiceProvider;
|
||||
|
||||
class TelescopeServiceProvider extends TelescopeApplicationServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
Telescope::night();
|
||||
|
||||
$this->hideSensitiveRequestDetails();
|
||||
|
||||
$isLocal = $this->app->environment('local');
|
||||
|
||||
Telescope::filter(function (IncomingEntry $entry) use ($isLocal) {
|
||||
return $isLocal ||
|
||||
$entry->isReportableException() ||
|
||||
$entry->isFailedRequest() ||
|
||||
$entry->isFailedJob() ||
|
||||
$entry->isScheduledTask() ||
|
||||
$entry->hasMonitoredTag();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevent sensitive request details from being logged by Telescope.
|
||||
*/
|
||||
protected function hideSensitiveRequestDetails(): void
|
||||
{
|
||||
if ($this->app->environment('local')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Telescope::hideRequestParameters(['_token']);
|
||||
|
||||
Telescope::hideRequestHeaders([
|
||||
'cookie',
|
||||
'x-csrf-token',
|
||||
'x-xsrf-token',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the Telescope gate.
|
||||
*
|
||||
* This gate determines who can access Telescope in non-local environments.
|
||||
*/
|
||||
protected function gate(): void
|
||||
{
|
||||
Gate::define('viewTelescope', function ($user) {
|
||||
return $user->hasRole('admin');
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user