Completed
Push — master ( c18000...efec97 )
by Anton
12s
created

Table::getPrivileges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application\Privileges;
11
12
use Application\Roles;
13
use Bluz\Proxy\Cache;
14
use Bluz\Proxy\Db;
15
16
/**
17
 * Table
18
 *
19
 * @package  Application\Privileges
20
 *
21
 * @method   static Row findRow($primaryKey)
22
 * @method   static Row findRowWhere($whereList)
23
 */
24
class Table extends \Bluz\Db\Table
25
{
26
    /**
27
     * Table
28
     *
29
     * @var string
30
     */
31
    protected $name = 'acl_privileges';
32
33
    /**
34
     * Primary key(s)
35
     * @var array
36
     */
37
    protected $primary = array('roleId', 'module', 'privilege');
38
39
    /**
40
     * Get all privileges
41
     *
42
     * @return array
43
     */
44 1
    public function getPrivileges()
45
    {
46 1
        return self::fetch(
47
            "SELECT DISTINCT p.roleId, p.module, p.privilege
48
            FROM acl_privileges AS p
49 1
            ORDER BY module, privilege"
50
        );
51
    }
52
53
    /**
54
     * Get user privileges
55
     *
56
     * @param integer $userId
57
     * @return array
58
     */
59 3
    public function getUserPrivileges($userId)
60
    {
61 3
        $roles = Roles\Table::getInstance()->getUserRolesIdentity($userId);
62
63 3
        $stack = [];
64 3
        foreach ($roles as $roleId) {
65
            $stack = array_merge($stack, $this->getRolePrivileges($roleId));
66
        }
67
68
        // magic array_unique for multi array
69 3
        return array_unique($stack);
70
71
        // follow code is faster, but required record for every user in memcache
72
        // in other words, need more memory for decrease CPU load
73
        // for update
74
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
        $cacheKey = '|privileges|'.$userId;
76
        if (!$data = Cache::get($cacheKey)) {
77
            $data = Db::getDefaultAdapter()->fetchColumn(
78
                "SELECT DISTINCT r.id, CONCAT(p.module, ':', p.privilege)
79
                FROM acl_privileges AS p, acl_roles AS r, acl_users_roles AS u2r
80
                WHERE p.roleId = r.id AND r.id = u2r.roleId AND u2r.userId = ?
81
                ORDER BY module, privilege",
82
                array((int) $userId)
83
            );
84
85
            Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY);
86
        }
87
        return $data;
88
        */
89
    }
90
91
    /**
92
     * Get user privileges
93
     *
94
     * @param integer $roleId
95
     * @return array
96
     */
97 View Code Duplication
    public function getRolePrivileges($roleId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $cacheKey = 'roles.privileges.'.$roleId;
100
101
        if (!$data = Cache::get($cacheKey)) {
102
            $data = Db::fetchColumn(
103
                "SELECT DISTINCT CONCAT(p.module, ':', p.privilege)
104
                FROM acl_privileges AS p, acl_roles AS r
105
                WHERE p.roleId = r.id AND r.id = ?
106
                ORDER BY CONCAT(p.module, ':', p.privilege)",
107
                array((int) $roleId)
108
            );
109
110
            Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY, ['system', 'roles', 'privileges']);
111
        }
112
        return $data;
113
    }
114
}
115