mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
first commit
This commit is contained in:
33
app/Http/Middleware/AdminMiddleware.php
Normal file
33
app/Http/Middleware/AdminMiddleware.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Services\AuthService;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class AdminMiddleware implements MiddlewareInterface
|
||||
{
|
||||
private AuthService $auth;
|
||||
|
||||
public function __construct(AuthService $auth)
|
||||
{
|
||||
$this->auth = $auth;
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
// Check if user is logged in and is admin
|
||||
if (!$this->auth->isLoggedIn() || !$this->auth->isAdmin()) {
|
||||
$response = new \Slim\Psr7\Response();
|
||||
return $response->withStatus(403)->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
// Add user to request attributes
|
||||
$request = $request->withAttribute('user', $this->auth->getCurrentUser());
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
33
app/Http/Middleware/AuthMiddleware.php
Normal file
33
app/Http/Middleware/AuthMiddleware.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Services\AuthService;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class AuthMiddleware implements MiddlewareInterface
|
||||
{
|
||||
private AuthService $auth;
|
||||
|
||||
public function __construct(AuthService $auth)
|
||||
{
|
||||
$this->auth = $auth;
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
// Check if user is logged in
|
||||
if (!$this->auth->isLoggedIn()) {
|
||||
$response = new \Slim\Psr7\Response();
|
||||
return $response->withStatus(302)->withHeader('Location', '/login');
|
||||
}
|
||||
|
||||
// Add user to request attributes
|
||||
$request = $request->withAttribute('user', $this->auth->getCurrentUser());
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user