Skip to content

Commit

Permalink
Merge pull request #1 from alfikiafan/afif
Browse files Browse the repository at this point in the history
Middleware and Authorization
  • Loading branch information
alscheift authored Jun 9, 2023
2 parents 0b36655 + 91f9b85 commit 0db97ff
Show file tree
Hide file tree
Showing 5 changed files with 1,081 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Kernel extends HttpKernel

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\ThrottleRequests::class . ':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
Expand All @@ -63,5 +63,10 @@ class Kernel extends HttpKernel
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,


// Custom Middleware
'administrator' => \App\Http\Middleware\OnlyAdministrator::class,
'cashier' => \App\Http\Middleware\OnlyCashier::class,
];
}
23 changes: 23 additions & 0 deletions app/Http/Middleware/OnlyAdministrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class OnlyAdministrator
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if ($request->user() && $request->user()->role !== 'administrator') {
abort(403);
}
return $next($request);
}
}
23 changes: 23 additions & 0 deletions app/Http/Middleware/OnlyCashier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class OnlyCashier
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if ($request->user() && $request->user()->role !== 'cashier') {
abort(403);
}
return $next($request);
}
}
9 changes: 8 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -19,6 +20,12 @@ public function register(): void
*/
public function boot(): void
{
//
Gate::define('admin', function ($user) {
return $user->role === 'administrator';
});

Gate::define('cashier', function ($user) {
return $user->role === 'cashier';
});
}
}
Loading

0 comments on commit 0db97ff

Please sign in to comment.