Failed Conditions
Push — develop ( da4f01...29d72f )
by Anton
15:25
created

Table::getUserPrivileges()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
c 0
b 0
f 0
cc 2
crap 2
rs 10
1
<?php
2
3
/**
4
 * @copyright Bluz PHP Team
5
 * @link      https://github.com/bluzphp/skeleton
6
 */
7
8
declare(strict_types=1);
9
10
namespace Application\Privileges;
11
12
use Application\Roles;
13
use Bluz\Proxy\Cache;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Cache 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...
14
use Bluz\Proxy\Db;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Db 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...
15
16
/**
17
 * Table of Privileges
18
 *
19
 * @package  Application\Privileges
20
 *
21
 * @method   static Row|null findRow($primaryKey)
22
 * @see      \Bluz\Db\Table::findRow()
23
 * @method   static Row|null findRowWhere($whereList)
24
 * @see      \Bluz\Db\Table::findRowWhere()
25
 */
26
class Table extends \Bluz\Db\Table
0 ignored issues
show
Bug introduced by
The type Bluz\Db\Table 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...
27
{
28
    /**
29
     * Table
30
     *
31
     * @var string
32
     */
33
    protected $name = 'acl_privileges';
34
35
    /**
36
     * Primary key(s)
37
     *
38
     * @var array
39
     */
40
    protected $primary = ['roleId', 'module', 'privilege'];
41
42
    /**
43
     * Get all privileges
44
     *
45
     * @return Row[]
46 1
     */
47
    public function getPrivileges(): array
48 1
    {
49 1
        return self::fetch(
50
            'SELECT DISTINCT p.roleId, p.module, p.privilege
51
            FROM acl_privileges AS p
52
            ORDER BY module, privilege'
53
        );
54
    }
55
56
    /**
57
     * Get user privileges
58
     *
59
     * @param integer $userId
60
     *
61
     * @return array
62 6
     */
63
    public function getUserPrivileges($userId): array
64 6
    {
65
        $roles = Roles\Table::getInstance()->getUserRolesIdentity($userId);
66 6
67 6
        $stack = [[]];
68 1
        foreach ($roles as $roleId) {
69
            $stack[] = $this->getRolePrivileges($roleId);
70 6
        }
71
        $stack = array_merge(...$stack);
72
73 6
        // magic array_unique for multi array
74
        return array_unique($stack);
75
76
        // follow code is faster, but required record for every user in memcache
77
        // in other words, need more memory for decrease CPU load
78
        // for update
79
        /*
80
        $cacheKey = '|privileges|'.$userId;
81
        if (!$data = Cache::get($cacheKey)) {
82
            $data = Db::getDefaultAdapter()->fetchColumn(
83
                "SELECT DISTINCT r.id, CONCAT(p.module, ':', p.privilege)
84
                FROM acl_privileges AS p, acl_roles AS r, acl_users_roles AS u2r
85
                WHERE p.roleId = r.id AND r.id = u2r.roleId AND u2r.userId = ?
86
                ORDER BY module, privilege",
87
                [(int) $userId]
88
            );
89
90
            Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY);
91
        }
92
        return $data;
93
        */
94
    }
95
96
    /**
97
     * Get user privileges
98
     *
99
     * @param integer $roleId
100
     *
101
     * @return array
102 1
     */
103
    public function getRolePrivileges($roleId): array
104 1
    {
105
        $cacheKey = 'roles.privileges.' . $roleId;
106 1
107 1
        if (!$data = Cache::get($cacheKey)) {
108 1
            $data = Db::fetchColumn(
109
                "SELECT DISTINCT CONCAT(p.module, ':', p.privilege)
110
                FROM acl_privileges AS p, acl_roles AS r
111
                WHERE p.roleId = r.id AND r.id = ?
112 1
                ORDER BY CONCAT(p.module, ':', p.privilege)",
113
                [(int)$roleId]
114
            );
115 1
116
            Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY, ['system', 'roles', 'privileges']);
117 1
        }
118
        return $data;
119
    }
120
}
121