| 1 |
<?php |
| 2 |
namespace Depicter\Middleware; |
| 3 |
|
| 4 |
|
| 5 |
use WPEmerge\Requests\RequestInterface; |
| 6 |
use WPEmerge\Responses\ResponseService; |
| 7 |
|
| 8 |
class CapabilityMiddleware { |
| 9 |
|
| 10 |
/** |
| 11 |
* Response service. |
| 12 |
* |
| 13 |
* @var ResponseService |
| 14 |
*/ |
| 15 |
protected $responseService = null; |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor. |
| 19 |
* |
| 20 |
* @codeCoverageIgnore |
| 21 |
* @param ResponseService $responseService |
| 22 |
*/ |
| 23 |
public function __construct( ResponseService $responseService ) { |
| 24 |
$this->responseService = $responseService; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @param RequestInterface $request |
| 29 |
* @param $next |
| 30 |
* @param string $capability |
| 31 |
* |
| 32 |
* @return mixed|ResponseService |
| 33 |
*/ |
| 34 |
public function handle( RequestInterface $request, $next, string $capability = 'manage_options' ){ |
| 35 |
// ignore the request if the current user doesn't have sufficient permissions |
| 36 |
if ( ! current_user_can( $capability ) ) { |
| 37 |
return $this->responseService->json([ |
| 38 |
'errors' => [ __( "Sorry, insufficient permission!", 'depicter' ) ] |
| 39 |
])->withStatus(403); |
| 40 |
} |
| 41 |
|
| 42 |
return $next( $request ); |
| 43 |
} |
| 44 |
} |
| 45 |
|