-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubject.php
More file actions
129 lines (109 loc) · 2.64 KB
/
Subject.php
File metadata and controls
129 lines (109 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2021 David Young
* @license https://github.com/opulencephp/Opulence/blob/1.2/LICENSE.md
*/
namespace Opulence\Authentication;
use Opulence\Authentication\Credentials\ICredential;
/**
* Defines an authentication subject
*/
class Subject implements ISubject
{
/** @var IPrincipal[] The list of principals */
protected $principals = [];
/** @var ICredential[] The list of credentials */
protected $credentials = [];
/**
* @param array $principals The list of principals
* @param array $credentials The list of credentials
*/
public function __construct(array $principals = [], array $credentials = [])
{
foreach ($principals as $principal) {
$this->addPrincipal($principal);
}
foreach ($credentials as $credential) {
$this->addCredential($credential);
}
}
/**
* @inheritdoc
*/
public function addCredential(ICredential $credential)
{
$this->credentials[$credential->getType()] = $credential;
}
/**
* @inheritdoc
*/
public function addPrincipal(IPrincipal $principal)
{
$this->principals[$principal->getType()] = $principal;
}
/**
* @inheritdoc
*/
public function getCredential(string $type)
{
if (!isset($this->credentials[$type])) {
return null;
}
return $this->credentials[$type];
}
/**
* @inheritdoc
*/
public function getCredentials() : array
{
return array_values($this->credentials);
}
/**
* @inheritdoc
*/
public function getPrimaryPrincipal()
{
if (!isset($this->principals[PrincipalTypes::PRIMARY])) {
return null;
}
return $this->principals[PrincipalTypes::PRIMARY];
}
/**
* @inheritdoc
*/
public function getPrincipal(string $type)
{
if (!isset($this->principals[$type])) {
return null;
}
return $this->principals[$type];
}
/**
* @inheritdoc
*/
public function getPrincipals() : array
{
return array_values($this->principals);
}
/**
* @inheritdoc
*/
public function getRoles() : array
{
$roles = [];
foreach ($this->principals as $type => $principal) {
$roles = array_merge($roles, $principal->getRoles());
}
return $roles;
}
/**
* @inheritdoc
*/
public function hasRole(string $roleName) : bool
{
return in_array($roleName, $this->getRoles());
}
}