Completed
Push — b0.25.0 ( 7dcdac...47a5e1 )
by Sebastian
03:26
created

Authorization   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 15
dl 0
loc 81
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A can() 0 19 5
A __construct() 0 4 1
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\Authentication;
15
use Linna\Authorization\Permission;
16
use Linna\DataMapper\NullDomainObject;
17
18
/**
19
 * Provide methods for check permissions for authenticated user.
20
 */
21
class Authorization
22
{
23
    use PermissionTrait {
24
        PermissionTrait::can as canByObject;
25
    }
26
27
    /**
28
     * @var int User id
29
     */
30
    protected $userId = 0;
31
32
    /**
33
     * Class Constructor.
34
     * <pre><code class="php">use Linna\Auth\Authentication;
35
     * use Linna\Auth\Authorization;
36
     * use Linna\Auth\Password;
37
     * use Linna\Session\Session;
38
     *
39
     * //your concrete permission mapper
40
     * use YourApp\Mapper\PermissionMapper;
41
     *
42
     * $password = new Password();
43
     * $session = new Session();
44
     *
45
     * $authentication = new Authentication($session, $password);
46
     * $permissionMapper = new PermissionMapper();
47
     *
48
     * $authorization = new Authorization($authentication, $permissionMapper);
49
     * </code></pre>
50
     *
51
     * @param Authentication            $authentication
52
     * @param PermissionMapperInterface $permissionMapper
53
     */
54 3
    public function __construct(Authentication $authentication, PermissionMapperInterface $permissionMapper)
55
    {
56 3
        $this->userId = $authentication->getLoginData()['user_id'] ?? 0;
57 3
        $this->permission = $permissionMapper->fetchByUserId($this->userId);
58 3
    }
59
60
    /**
61
     * Check if authenticated user has a permission.
62
     * <pre><code class="php">$authorization = new Authorization($authentication, $permissionMapper);
63
     *
64
     * //with this example, the class checks if the authenticated
65
     * //user has the permission with the permission object.
66
     *
67
     * $permission = $permissionMapper->fetchById(1);
68
     * $authorization->can($permission);
69
     *
70
     * //with this example, the class checks if the authenticated
71
     * //user has the permission with the permission id 1.
72
     * $authorization->can(1);
73
     *
74
     * //with this example, the class checks if the authenticated
75
     * //user has the permission 'see users'.
76
     * $authorization->can('see users');
77
     * </code></pre>
78
     *
79
     * @param Permission|int|string $permission
80
     *
81
     * @return bool
82
     */
83 2
    public function can($permission): bool
84
    {
85 2
        if ($permission instanceof NullDomainObject) {
0 ignored issues
show
introduced by
$permission is never a sub-type of Linna\DataMapper\NullDomainObject.
Loading history...
86 1
            return false;
87
        }
88
89 2
        if ($permission instanceof Permission) {
90 1
            return $this->canByObject($permission);
91
        }
92
93 2
        if (is_int($permission)) {
94 1
            return $this->canById($permission);
95
        }
96
97 2
        if (is_string($permission)) {
0 ignored issues
show
introduced by
The condition is_string($permission) is always true.
Loading history...
98 1
            return $this->canByName($permission);
99
        }
100
101 1
        return false;
102
    }
103
}
104