for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Gewaer\Middleware;
use Phalcon\Mvc\Micro;
use Phalcon\Mvc\Micro\MiddlewareInterface;
use Gewaer\Exception\ServerErrorHttpException;
use Gewaer\Exception\PermissionException;
/**
* Class AclMiddleware
*
* @package Gewaer\Middleware
*/
class AclMiddleware implements MiddlewareInterface
{
* Call me
* @param Micro $api
* @todo need to check section for auth here
* @return bool
public function call(Micro $api)
$auth = $api->getService('auth');
$router = $api->getService('router');
$request = $api->getService('request');
if (!$auth->isIgnoreUri()) {
// explode() by / , postiion #1 is always the controller , so its the resource ^.^
$matchRouter = explode('/', $router->getMatchedRoute()->getCompiledPattern());
$resource = ucfirst($matchRouter[2]); //2 is alwasy the controller of the router
$userData = $api->getService('userData');
$action = null;
// GET -> read
// PUT -> update
// DELETE -> delete
// POST -> create
switch (strtolower($request->getMethod())) {
case 'get':
$action = 'read';
break;
case 'post':
$action = 'create';
case 'delete':
$action = 'delete';
case 'put':
case 'patch':
$action = 'update';
default:
throw new ServerErrorHttpException('No Permission define for this action');
}
//do you have permision
if (!$userData->can($resource . '.' . $action)) {
throw new PermissionException('You dont have permission to run this action ' . $action . ' at ' . $resource);
$api->stop();
$api->stop()
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.
return
die
exit
function fx() { try { doSomething(); return true; } catch (\Exception $e) { return false; } return false; }
In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.
return false
return false;
return true;