Completed
Push — b0.25.0 ( 25876b...875a21 )
by Sebastian
05:28
created

Role::isUserInRoleByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Authorization;
13
14
use Linna\Authentication\User;
15
use Linna\DataMapper\DomainObjectAbstract;
16
17
/**
18
 * Role.
19
 */
20
class Role extends DomainObjectAbstract
21
{
22
    use PermissionTrait;
23
24
    /**
25
     * @var string Group name
26
     */
27
    public $name = '';
28
29
    /**
30
     * @var string Group description
31
     */
32
    public $description = '';
33
34
    /**
35
     * @var int It say if group is active or not
36
     */
37
    public $active = 0;
38
39
    /**
40
     * @var array Contain users in group
41
     */
42
    private $users = [];
43
44
    /**
45
     * @var string Last update
46
     */
47
    public $lastUpdate = '';
48
49
    /**
50
     * Constructor.
51
     */
52 1
    public function __construct(array $users = [], array $permissions = [])
53
    {
54 1
        $this->users = $users;
55 1
        $this->permission = $permissions;
56
57
        //set required type
58 1
        settype($this->rId, 'integer');
59 1
        settype($this->objectId, 'integer');
60 1
        settype($this->active, 'integer');
61 1
    }
62
63
    /**
64
     * Check if an user is in role, use User instance.
65
     *
66
     * @param User $user
67
     *
68
     * @return bool
69
     */
70
    public function isUserInRole(User $user): bool
71
    {
72
        return $this->isUserInRoleById($user->getId());
73
    }
74
75
    /**
76
     * Check if an user is in role, use the user Id.
77
     *
78
     * @param int $userId
79
     *
80
     * @return bool
81
     */
82
    public function isUserInRoleById(int $userId): bool
83
    {
84
        if (isset($this->users[$userId])) {
85
            return true;
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * Check if an user is in role, use the user name.
93
     *
94
     * @param string $userName
95
     *
96
     * @return bool
97
     */
98
    public function isUserInRoleByName(string $userName): bool
99
    {
100
        if (in_array($userName, array_column($this->users, 'name'), true)) {
101
            return true;
102
        }
103
104
        return false;
105
    }
106
}
107