Failed Conditions
Pull Request — master (#10)
by Maximo
03:05
created

AclMiddleware::call()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 39.042

Importance

Changes 0
Metric Value
cc 8
eloc 30
nc 12
nop 1
dl 0
loc 46
ccs 6
cts 28
cp 0.2143
crap 39.042
rs 8.1954
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style introduced by
End of line character is invalid; expected "\n" but found "\r\n"
Loading history...
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Middleware;
6
7
use Phalcon\Mvc\Micro;
8
use Phalcon\Mvc\Micro\MiddlewareInterface;
9
use Gewaer\Exception\ServerErrorHttpException;
10
use Gewaer\Exception\PermissionException;
11
12
/**
13
 * Class AclMiddleware
14
 *
15
 * @package Gewaer\Middleware
16
 */
17
class AclMiddleware implements MiddlewareInterface
18
{
19
    /**
20
     * Call me
21
     *
22
     * @param Micro $api
23
     * @todo need to check section for auth here
24
     * @return bool
25
     */
26 2
    public function call(Micro $api)
27
    {
28 2
        $auth = $api->getService('auth');
29 2
        $router = $api->getService('router');
30 2
        $request = $api->getService('request');
31
32 2
        if (!$auth->isIgnoreUri()) {
33
            // explode() by / , postiion #1 is always the controller , so its the resource ^.^
34
            $matchRouter = explode('/', $router->getMatchedRoute()->getCompiledPattern());
35
            $resource = ucfirst($matchRouter[2]); //2 is alwasy the controller of the router
36
            $userData = $api->getService('userData');
37
38
            $action = null;
39
            // GET -> read
40
            // PUT -> update
41
            // DELETE -> delete
42
            // POST -> create
43
44
            switch (strtolower($request->getMethod())) {
45
                case 'get':
46
                    $action = 'read';
47
                break;
48
                case 'post':
49
                    $action = 'create';
50
                break;
51
                case 'delete':
52
                    $action = 'delete';
53
                break;
54
                case 'put':
55
                case 'patch':
56
                    $action = 'update';
57
                break;
58
                default:
59
                    throw new ServerErrorHttpException('No Permission define for this action');
60
                break;
61
            }
62
63
            //do you have permision
64
            if (!$userData->can($resource . '.' . $action)) {
65
                throw new PermissionException('You dont have permission to run this action ' . $action . ' at ' . $resource);
66
                $api->stop();
0 ignored issues
show
Unused Code introduced by
$api->stop() is not reachable.

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.

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.

Loading history...
67
                return false;
68
            }
69
        }
70
71 2
        return true;
72
    }
73
}
74