Completed
Push — master ( 1bbe3a...a1970e )
by Oleg
08:32
created

FileRbac::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php /** MicroFileRBAC */
2
3
namespace Micro\Auth\Drivers;
4
5
use Micro\Db\IConnection;
6
7
/**
8
 * File RBAC class file.
9
 *
10
 * RBAC security with files.
11
 *
12
 * @author Oleg Lunegov <[email protected]>
13
 * @link https://github.com/linpax/microphp-framework
14
 * @copyright Copyright (c) 2013 Oleg Lunegov
15
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
16
 * @package Micro
17
 * @subpackage Auth\Drivers
18
 * @version 1.0
19
 * @since 1.0
20
 */
21
class FileRbac extends Rbac
22
{
23
    /** @var array $roles RBAC role tree */
24
    private $roles = [];
25
26
27
    /**
28
     * Redefine constructor for RBAC
29
     *
30
     * @access public
31
     *
32
     * @param IConnection $connection
33
     *
34
     * @result void
35
     */
36
    public function __construct(IConnection $connection)
37
    {
38
        parent::__construct($connection);
39
40
        if (!empty($params['roles'])) {
0 ignored issues
show
Bug introduced by
The variable $params seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
41
            $this->roles = $this->tree($params['roles']);
42
        }
43
    }
44
45
    /**
46
     * Assign RBAC element into user
47
     *
48
     * @access public
49
     *
50
     * @param integer $userId user id
51
     * @param string $name element name
52
     *
53
     * @return bool
54
     */
55
    public function assign($userId, $name)
56
    {
57
        if ($this->searchRoleRecursive($this->roles, $name)) {
58
            $exists = $this->db->exists('rbac_user', ['user' => $userId, 'role' => $name]);
59
            if (!$exists) {
60
                return $this->db->insert('rbac_user', ['role' => $name, 'user' => $userId]);
61
            }
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * Get raw roles
69
     *
70
     * @access public
71
     * @return mixed
72
     */
73
    public function rawRoles()
74
    {
75
        return $this->roles;
76
    }
77
}
78