1 | <?php |
||
29 | class Principal |
||
30 | { |
||
31 | /** |
||
32 | * @var string The principal username |
||
33 | */ |
||
34 | protected $username; |
||
35 | /** |
||
36 | * @var bool Whether the principal is anonymous |
||
37 | */ |
||
38 | protected $anonymous; |
||
39 | /** |
||
40 | * @var array Associative array of extra details |
||
41 | */ |
||
42 | protected $details; |
||
43 | |||
44 | /** |
||
45 | * @var Principal singleton instance of the anonymous Principal |
||
46 | */ |
||
47 | private static $anon; |
||
48 | |||
49 | /** |
||
50 | * Creates a new security principal. |
||
51 | * |
||
52 | * @param string|null $username |
||
53 | * @param array $details |
||
54 | * @param bool $anonymous |
||
55 | */ |
||
56 | 2 | protected function __construct(?string $username = null, array $details = [], bool $anonymous = false) |
|
62 | |||
63 | /** |
||
64 | * Gets a key-value array containing any authentication details. |
||
65 | * |
||
66 | * @return array The auth details |
||
67 | */ |
||
68 | 2 | public function getDetails(): array |
|
72 | |||
73 | /** |
||
74 | * Gets the authenticated principal username. |
||
75 | * |
||
76 | * An anonymous user has a `null` username. |
||
77 | * |
||
78 | * @return string|null The username, or `null` |
||
79 | */ |
||
80 | 2 | public function getUsername(): ?string |
|
84 | |||
85 | /** |
||
86 | * Gets whether this authentication is anonymous. |
||
87 | * |
||
88 | * @return bool Whether this principal is anonymous |
||
89 | */ |
||
90 | 2 | public function isAnonymous(): bool |
|
94 | |||
95 | /** |
||
96 | * Gets a string representation. |
||
97 | * |
||
98 | * @return string The string representation |
||
99 | */ |
||
100 | 2 | public function __toString(): string |
|
104 | |||
105 | /** |
||
106 | * Gets a non-anonymous Principal. |
||
107 | * |
||
108 | * @param string $username The principal username |
||
109 | * @param array $details Any authentication details |
||
110 | * @return Principal The principal |
||
111 | */ |
||
112 | 1 | public static function get(string $username, array $details): Principal |
|
116 | |||
117 | /** |
||
118 | * Gets a token representing an anonymous authentication. |
||
119 | * |
||
120 | * @return Principal The anonymous principal |
||
121 | */ |
||
122 | 1 | public static function getAnonymous(): Principal |
|
129 | } |
||
130 |