AclMiddleware::call()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 29
nc 7
nop 1
dl 0
loc 46
ccs 0
cts 35
cp 0
crap 90
rs 8.0555
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Middleware;
6
7
use Phalcon\Mvc\Micro;
0 ignored issues
show
Bug introduced by
The type Phalcon\Mvc\Micro was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Canvas\Http\Exception\InternalServerErrorException;
9
use Canvas\Http\Exception\UnauthorizedException;
10
use Canvas\Models\Subscription;
11
12
/**
13
 * Class AclMiddleware.
14
 *
15
 * @package Canvas\Middleware
16
 */
17
class AclMiddleware extends TokenBase
18
{
19
    /**
20
     * Call me.
21
     *
22
     * @param Micro $api
23
     * @todo need to check section for auth here
24
     * @return bool
25
     */
26
    public function call(Micro $api)
27
    {
28
        $router = $api->getService('router');
29
        $request = $api->getService('request');
30
31
        // explode() by / , postiion #1 is always the controller , so its the resource ^.^
32
        $matchRouter = explode('/', $router->getMatchedRoute()->getCompiledPattern());
33
34
        $resource = ucfirst(isset($matchRouter[2]) ? $matchRouter[2] : $matchRouter[1]); //2 is alwasy the controller of the router
35
        $userData = $api->getService('userData');
36
37
        $action = null;
38
        $method = strtolower($request->getMethod());
39
40
        // GET -> read
41
        // PUT -> update
42
        // DELETE -> delete
43
        // POST -> create
44
        switch ($method) {
45
            case 'get':
46
                $action = 'list';
47
                if (preg_match("/\/([0-9]+)(?=[^\/]*$)/", $request->getURI())) {
48
                    $action = 'read';
49
                }
50
                break;
51
            case 'post':
52
                $action = 'create';
53
                break;
54
            case 'delete':
55
                $action = 'delete';
56
                break;
57
            case 'put':
58
            case 'patch':
59
                $action = 'update';
60
                break;
61
            default:
62
                throw new InternalServerErrorException('No Permission define for this action ' . $method);
63
            break;
64
        }
65
66
        //do you have permision
67
        if (!$userData->can($resource . '.' . $action)) {
68
            throw new UnauthorizedException('You dont have permission to run this action ' . $action . ' at ' . $resource);
69
        }
70
71
        return true;
72
    }
73
}
74