Passed
Push — main ( 25353d...351785 )
by Sammy
01:44
created

Kadro::authorize()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 8
nc 4
nop 1
dl 0
loc 16
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace HexMakina\kadro\Controllers;
4
5
use HexMakina\kadro\Auth\{AccessRefusedException, AuthControllerInterface, OperatorInterface};
6
use HexMakina\kadro\Controllers\Interfaces\IntlControllerInterface;
7
8
class Kadro extends Display implements AuthControllerInterface, IntlControllerInterface
9
{
10
    private $translation_function_name = 'L';
11
12
    public function __toString()
13
    {
14
        return get_called_class();
15
    }
16
17
    public function requires_operator(): bool
18
    {
19
        return true; // security by default
20
    }
21
22
    public function operator(): OperatorInterface
23
    {
24
        return $this->get('OperatorInterface');
25
    }
26
27
    // returns true or throws AccessRefusedException
28
    public function authorize($permission = null): bool
29
    {
30
        if (!$this->requires_operator()) {
31
            return true;
32
        }
33
34
        $operator = $this->operator();
35
        if (is_null($operator) || $operator->is_new() || !$operator->is_active()) {
36
            throw new AccessRefusedException();
37
        }
38
39
        if (!is_null($permission) && !$operator->has_permission($permission)) {
40
            throw new AccessRefusedException();
41
        }
42
43
        return true;
44
    }
45
46
    public function execute()
47
    {
48
      // kadro controller is a display controller with authentification and intl
49
        $this->authorize();
50
        return parent::execute();
51
    }
52
53
    public function prepare()
54
    {
55
        parent::prepare();
56
        $this->trim_request_data();
57
    }
58
59
    // intl function, calls to lezer
60
    public function l($message, $context = []): string
61
    {
62
        return call_user_func($this->translation_function_name, $message, $context);
63
    }
64
65
    private function trim_request_data()
66
    {
67
        array_walk_recursive($_GET, function (&$value) {
68
            $value = trim($value);
69
        });
70
        array_walk_recursive($_REQUEST, function (&$value) {
71
            $value = trim($value);
72
        });
73
74
        if ($this->router()->submits()) {
75
            array_walk_recursive($_POST, function (&$value) {
76
                $value = trim($value);
77
            });
78
        }
79
    }
80
}
81