1 | <?php |
||
17 | class Auth implements \ArrayAccess |
||
18 | { |
||
19 | /** |
||
20 | * Provides setLogger method, and protected logger property. |
||
21 | */ |
||
22 | use LoggerAwareTrait; |
||
23 | |||
24 | /** |
||
25 | * Name of the method called when authenticating. |
||
26 | */ |
||
27 | const ACTION_LOGIN = 'login'; |
||
28 | |||
29 | /** |
||
30 | * Name of the method called when attempting to log out. |
||
31 | */ |
||
32 | const ACTION_LOGOUT = 'logout'; |
||
33 | |||
34 | /** |
||
35 | * Name of the session verification method. |
||
36 | */ |
||
37 | const ACTION_VERIFY = 'verify'; |
||
38 | |||
39 | /** |
||
40 | * A result that has no effect either positive or negative. |
||
41 | */ |
||
42 | const RESULT_NOOP = null; |
||
43 | |||
44 | /** |
||
45 | * A result that acts as a provisional success; Pass if all other tests pass. |
||
46 | */ |
||
47 | const RESULT_SUCCESS = 0; |
||
48 | |||
49 | /** |
||
50 | * A result that indicates forced immediate success, bypassing further tests. Should not be used lightly. |
||
51 | */ |
||
52 | const RESULT_FORCE = 1; |
||
53 | |||
54 | /** |
||
55 | * A result that indicates failure. All failures are immediate. |
||
56 | */ |
||
57 | const RESULT_FAILURE = -1; |
||
58 | |||
59 | /** |
||
60 | * An array of auth plugins. |
||
61 | * |
||
62 | * @var AuthPluginInterface[] |
||
63 | */ |
||
64 | private $plugins = []; |
||
65 | |||
66 | /** |
||
67 | * Shared values, accessible using the ArrayAccess interface. |
||
68 | * |
||
69 | * @var mixed[] |
||
70 | */ |
||
71 | private $shared = []; |
||
72 | |||
73 | /** |
||
74 | * Add a plugin to the Auth module. |
||
75 | * |
||
76 | * @param string|AuthPluginInterface The name of a plugin class to be registered, or a configured instance of a |
||
77 | * security plugin. |
||
78 | * @return bool True if the plugin was added successfully. |
||
79 | */ |
||
80 | 11 | public function addPlugin($plugin) |
|
95 | |||
96 | /** |
||
97 | * Perform a login based on username and password. |
||
98 | * |
||
99 | * @param string $username A user identifier. |
||
100 | * @param string $password The user's password. |
||
101 | * @return bool True if the login was successful, false otherwise. |
||
102 | * @throws AuthException |
||
103 | */ |
||
104 | 8 | public function login($username, $password) |
|
108 | |||
109 | /** |
||
110 | * Attempt to log the user out. |
||
111 | * |
||
112 | * @throws AuthException |
||
113 | */ |
||
114 | 2 | public function logout() |
|
118 | |||
119 | /** |
||
120 | * Verify a user's saved data - check if the user is logged in. |
||
121 | * |
||
122 | * @return bool True if the user's session was verified successfully. |
||
123 | * @throws AuthException |
||
124 | */ |
||
125 | 4 | public function verify() |
|
129 | |||
130 | /** |
||
131 | * Call an action on a plugin, with particular arguments. |
||
132 | * |
||
133 | * @param AuthPluginInterface $plugin The plugin on which to run the action. |
||
134 | * @param string $action The plugin action, login, logout, or verify. |
||
135 | * @param mixed[] $arguments A list of arguments to pass to the action. |
||
136 | * @return int The result returned by the Auth plugin. |
||
137 | * @throws AuthException |
||
138 | */ |
||
139 | 9 | private function callPlugin(AuthPluginInterface $plugin, $action, $arguments) |
|
162 | |||
163 | /** |
||
164 | * Perform a generalized action: login, logout, or verify: Run said function on each plugin in order verifying |
||
165 | * result along the way. |
||
166 | * |
||
167 | * @param string $action The action to be taken. One of login, logout, or verify. |
||
168 | * @param mixed[] $arguments A list of arguments to pass to the action. |
||
169 | * @return bool True if the action was successful, false otherwise. |
||
170 | * @throws AuthException |
||
171 | */ |
||
172 | 9 | private function action($action, $arguments = []) |
|
195 | |||
196 | /** |
||
197 | * Passthrough function call for plugins. |
||
198 | * |
||
199 | * @param string $method The name of the method to be called. |
||
200 | * @param array $args An array of arguments to be passed to the method. |
||
201 | * @return mixed Returns whatever the passthrough function returns, or null or error or missing function. |
||
202 | * @throws AuthException |
||
203 | * @noinspection PhpRedundantCatchClauseInspection |
||
204 | */ |
||
205 | 5 | public function __call($method, $args = []) |
|
224 | |||
225 | /** |
||
226 | * ArrayAccess offsetExists |
||
227 | * |
||
228 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
229 | * @inheritDoc |
||
230 | */ |
||
231 | 1 | public function offsetExists($offset) |
|
235 | |||
236 | /** |
||
237 | * ArrayAccess offsetGet |
||
238 | * |
||
239 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
240 | * @inheritDoc |
||
241 | */ |
||
242 | 1 | public function offsetGet($offset) |
|
246 | |||
247 | /** |
||
248 | * ArrayAccess offsetSet |
||
249 | * |
||
250 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
251 | * @inheritDoc |
||
252 | */ |
||
253 | 1 | public function offsetSet($offset, $value) |
|
257 | |||
258 | /** |
||
259 | * ArrayAccess offsetUnset |
||
260 | * |
||
261 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
262 | * @inheritDoc |
||
263 | */ |
||
264 | 1 | public function offsetUnset($offset) |
|
268 | |||
269 | /** |
||
270 | * Get the logger instance set on this object |
||
271 | * |
||
272 | * @return LoggerInterface The logger for use in this Auth object, or null if not set. |
||
273 | */ |
||
274 | 2 | public function getLogger() |
|
278 | |||
279 | /** |
||
280 | * Log a message and exception in a semi-consistent form. |
||
281 | * |
||
282 | * Logs the message, and appends exception message and location. |
||
283 | * |
||
284 | * @param Exception $exc The exception to log. |
||
285 | * @param string $message The exception message in printf style. |
||
286 | * @param string ... Any number of string parameters corresponding to %s placeholders in the message string. |
||
287 | * @noinspection PhpUnusedLocalVariableInspection |
||
288 | */ |
||
289 | 2 | private function logException(Exception $exc, $message /*, ... */) |
|
298 | } |
||
299 |