Skip to content

Commit

Permalink
chore(gate): update in Auth Gate implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
MathewEm committed Nov 6, 2024
1 parent 7ddf687 commit 772cef3
Show file tree
Hide file tree
Showing 6 changed files with 641 additions and 296 deletions.
17 changes: 15 additions & 2 deletions app/Filament/Requests/Auth/LoginRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;

class LoginRequest extends BaseAuth
{
use WithRateLimiting;

/* public function authorize()
* {
return true;
* } */

public function authenticate(): ?LoginResponse
{

Expand All @@ -41,7 +47,7 @@ public function authenticate(): ?LoginResponse
$maxAttempts = Config::get('auth.rate_limit.max_attempts');
$decaySeconds = Config::get('auth.rate_limit.decay_seconds');
try {
$this->rateLimit($maxAttempts, $decaySeconds);
$this->rateLimit($maxAttempts, 10);
} catch (TooManyRequestsException $exception) {
$this->logLockout();
$this->getRateLimitedNotification($exception)?->send();
Expand All @@ -62,6 +68,13 @@ public function authenticate(): ?LoginResponse

$user = Filament::auth()->user();

/**
* Authorizes user.
*
* @thows 403 AuthorizationException
*/
Gate::authorize('viewLibrarium', $user);

/**
* Check if user's email is verified
*
Expand Down
8 changes: 8 additions & 0 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use App\Filament\Resources\UserResource\Pages;
use App\Filament\Resources\UserResource\RelationManagers;
use App\Models\User;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Gate;

class UserResource extends Resource
{
Expand Down Expand Up @@ -54,6 +56,9 @@ public static function table(Table $table): Table
Tables\Columns\TextColumn::make('last_name')
->sortable(),
Tables\Columns\TextColumn::make('email'),
Tables\Columns\IconColumn::make('email_verified_at')
->boolean()
->sortable(),
Tables\Columns\IconColumn::make('active')
->boolean()
->sortable(),
Expand Down Expand Up @@ -89,6 +94,9 @@ public static function getRelations(): array

public static function getPages(): array
{
// $user = Filament::auth()->user();
// Gate::authorize('updateAnyUser', $user);

return [
'index' => Pages\ListUsers::route('/'),
'create' => Pages\CreateUser::route('/create'),
Expand Down
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function markEmailAsVerified(): bool
*/
public function canAccessPanel(Panel $panel): bool
{
return $this->hasRole('admin');
return $this->can('view_librarium');
}

/**
Expand Down
19 changes: 19 additions & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -30,5 +31,23 @@ public function boot()
VerifyEmail::toMailUsing(function (User $notifiable, string $url) {
return (new MailMessage)->subject(__('email.auth.verify.title'))->markdown('authentication.verify_email', ['url' => $url, 'user' => $notifiable->first_name]);
});

/**
* Register the Librarium View Panel gate.
*
* This gate determines who can view Librarium in non-local environments.
*/
Gate::define('viewLibrarium', function($user) {
return $user->can('view_librarium');
});

/**
* Register the User Update gate.
*
* This gate determines who can update Users in Librarium in non-local environments.
*/
Gate::define('updateAnyUser', function($user) {
return $user->can('update_any_user');
});
}
}
Loading

0 comments on commit 772cef3

Please sign in to comment.