Skip to content

Commit

Permalink
made sure that all middleware must extend to PulseFrame/Middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingmaj0r committed Aug 9, 2024
1 parent 9b2e8b1 commit 7a66a86
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
11 changes: 5 additions & 6 deletions src/Http/Middleware/AdminMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

namespace PulseFrame\Http\Middleware;

use PulseFrame\Facades\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Closure;
use PulseFrame\Middleware;
use PulseFrame\Exceptions\AccessForbiddenException;

class AdminMiddleware
class AdminMiddleware extends Middleware
{
public function handle(Request $request, Closure $next)
public function handle($request, $next)
{
if ($_SESSION['role'] === 'admin') {
return $next($request);
} else {
throw new AccessDeniedHttpException('Forbidden');
throw new AccessForbiddenException("You are not authorized to access this resource.");
}
}
}
11 changes: 6 additions & 5 deletions src/Http/Middleware/ApiMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace PulseFrame\Http\Middleware;

use PulseFrame\Facades\Request;
use PulseFrame\Middleware;
use PulseFrame\Facades\Response;
use Closure;

class ApiMiddleware
class ApiMiddleware extends Middleware
{
public function handle(Request $request, Closure $next)
public function handle($request, $next)
{
$maxRequestsPerHour = 100;

Expand All @@ -33,7 +32,9 @@ public function handle(Request $request, Closure $next)
$_SESSION[$key] = $rateLimitData;

if ($rateLimitData['requests'] > $maxRequestsPerHour) {
return Response::JSON('error', 'Rate limit exceeded.');
http_response_code(429);
echo Response::JSON('error', 'Rate limit exceeded.');
exit;
}

return $next($request);
Expand Down
7 changes: 3 additions & 4 deletions src/Http/Middleware/AuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace PulseFrame\Http\Middleware;

use PulseFrame\Middleware;
use PulseFrame\Facades\Response;
use PulseFrame\Facades\Database;
use PulseFrame\Facades\Request;
use Closure;

class AuthMiddleware
class AuthMiddleware extends Middleware
{
public function handle(Request $request, Closure $next)
public function handle($request, $next)
{
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
$user = Database::find("UsersModel", $_SESSION['email']);
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Middleware/WebMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace PulseFrame\Http\Middleware;

use PulseFrame\Middleware;
use PulseFrame\Facades\View;
use PulseFrame\Facades\Response;
use PulseFrame\Facades\Request;
use Closure;

class WebMiddleware
class WebMiddleware extends Middleware
{
public function handle(Request $request, Closure $next)
public function handle($request, $next)
{
$maintenanceFile = $_ENV['storage_path'] . '/framework/maintenance.flag';

Expand Down
11 changes: 11 additions & 0 deletions src/Middleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace PulseFrame;

use PulseFrame\Facades\Request;
use Closure;

abstract class Middleware
{
abstract public function handle(Request $request, Closure $next);
}

0 comments on commit 7a66a86

Please sign in to comment.