Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

FileAcl::assigned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php /** MicroFileACL */
2
3
namespace Micro\Auth;
4
5
use Micro\Mvc\Models\Query;
6
7
/**
8
 * File ACL class file.
9
 *
10
 * ACL security with files.
11
 *
12
 * @author Oleg Lunegov <[email protected]>
13
 * @link https://github.com/lugnsk/micro
14
 * @copyright Copyright &copy; 2013 Oleg Lunegov
15
 * @license /LICENSE
16
 * @package Micro
17
 * @subpackage Auth
18
 * @version 1.0
19
 * @since 1.0
20
 */
21
class FileAcl extends Acl
22
{
23
    /** @var array $roles configured roles */
24
    protected $roles;
25
    /** @var array $perms configured perms */
26
    protected $perms;
27
    /** @var array $rolePermsCompare compare of permissions in roles */
28
    protected $rolePermsCompare;
29
30
31
    /**
32
     * Configured ACL with files
33
     *
34
     * @access public
35
     *
36
     * @param array $params configuration array
37
     *
38
     * @result void
39
     */
40
    public function __construct(array $params = [])
41
    {
42
        parent::__construct($params);
43
44
        $roles = !empty($params['roles']) ? $params['roles'] : [];
45
        $this->roles = !empty($roles['roles']) ? $roles['roles'] : [];
46
        $this->perms = !empty($roles['perms']) ? $roles['perms'] : [];
47
        $this->rolePermsCompare = !empty($roles['role_perms']) ? $roles['role_perms'] : [];
48
    }
49
50
    /**
51
     * Check user access to permission
52
     *
53
     * @access public
54
     *
55
     * @param integer $userId user id
56
     * @param string $permission checked permission
57
     * @param array $data not used, added for compatible!
58
     *
59
     * @return bool
60
     * @throws \Micro\Base\Exception
61
     */
62
    public function check($userId, $permission, array $data = [])
63
    {
64
        $permissionId = in_array($permission, $this->perms, true);
65
        /** @var array $assigned */
66
        $assigned = $this->assigned($userId);
67
        if (!$assigned) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $assigned of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
68
            return false;
69
        }
70
71
        foreach ($assigned AS $assign) {
72
            if ($assign['perm'] && $assign['perm'] === $permissionId) {
73
                return true;
74
            } elseif ($assign['role'] && in_array($permissionId, $this->rolePerms($assign['role']), true)) {
75
                return true;
76
            }
77
        }
78
79
        return false;
80
    }
81
82
    /**
83
     * Get assigned elements
84
     *
85
     * @access public
86
     *
87
     * @param integer $userId user ID
88
     *
89
     * @return mixed
90
     * @throws \Micro\Base\Exception
91
     */
92
    public function assigned($userId)
93
    {
94
        $query = new Query($this->container->db);
0 ignored issues
show
Bug introduced by
Accessing db on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
95
        $query->select = '*';
96
        $query->table = 'acl_user';
97
        $query->addWhere('`user`=' . $userId);
98
        $query->single = false;
99
100
        return $query->run();
101
    }
102
103
    /**
104
     * Get permissions in role
105
     *
106
     * @access private
107
     *
108
     * @param integer $role role name
109
     *
110
     * @return array
111
     */
112
    protected function rolePerms($role)
113
    {
114
        return $this->rolePermsCompare[$role];
115
    }
116
}
117