ACL::allow_in()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace HexMakina\kadro\Auth;
4
5
use HexMakina\BlackBox\Auth\OperatorInterface;
6
use HexMakina\BlackBox\Database\SelectInterface;
7
use HexMakina\Crudites\Queries\AutoJoin;
8
9
class ACL extends \HexMakina\TightORM\TightModel
10
{
11
    public const TABLE_NAME = 'kadro_acl';
12
    public const TABLE_ALIAS = 'acl';
13
14
    public function traceable(): bool
15
    {
16
        return false;
17
    }
18
19
    public static function match(OperatorInterface $op, $permission_name)
20
    {
21
        return in_array($permission_name, self::permissions_names_for($op));
22
    }
23
24
    public static function query_retrieve($filters = [], $options = []): SelectInterface
25
    {
26
        $options['eager'] = false;
27
        $ret = parent::query_retrieve($filters, $options);
28
        $eager_params = [];
29
        $eager_params[Permission::relationalMappingName()] = Permission::tableAlias();
30
        $eager_params[Operator::relationalMappingName()] = Operator::tableAlias();
31
        $eager_params[ACL::relationalMappingName()] = ACL::tableAlias();
32
33
        // why ? why dont you comment.. is the real question
34
        AutoJoin::eager($ret, $eager_params);
35
36
        return $ret;
37
    }
38
39
    public static function permissions_for(OperatorInterface $op)
40
    {
41
        $res = self::any(['operator_id' => $op->getId()]);
42
43
        $permission_ids = [];
44
        foreach ($res as $r) {
45
            $permission_ids[] = $r->get('permission_id');
46
        }
47
48
        $ret = Permission::filter(['ids' => $permission_ids]);
49
        return $ret;
50
    }
51
52
    public static function permissions_names_for(OperatorInterface $op)
53
    {
54
        $operator_with_perms = get_class($op)::exists($op->getId());
55
        // $operator_with_perms = get_class($op)::retrieve($operator_with_perms);
56
        if (is_null($operator_with_perms)) {
57
            return [];
58
        }
59
60
        return explode(',', $operator_with_perms->get('permission_names'));
61
    }
62
63
    public static function allow_in(OperatorInterface $op, Permission $p)
64
    {
65
        $ret = new ACL();
66
        $ret->set('operator_id', $op->getId());
67
        $ret->set('permission_id', $p->getId());
68
        $ret->save($op->getId());
69
        return $ret;
70
    }
71
}
72