1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_auth; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use ArrayAccess; |
7
|
|
|
use kalanis\kw_accounts\AccountsException; |
8
|
|
|
use kalanis\kw_auth\Interfaces\IAuthTree; |
9
|
|
|
use kalanis\kw_auth\Interfaces\IKauTranslations; |
10
|
|
|
use kalanis\kw_auth\Traits\TLang; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AuthTree |
15
|
|
|
* @package kalanis\kw_auth |
16
|
|
|
* Authenticate the user with predefined methods |
17
|
|
|
*/ |
18
|
|
|
class AuthTree implements IAuthTree |
19
|
|
|
{ |
20
|
|
|
use TLang; |
21
|
|
|
|
22
|
|
|
protected ?Methods\AMethods $authTree = null; |
23
|
|
|
protected ?Methods\AMethods $usedMethod = null; |
24
|
|
|
|
25
|
3 |
|
public function __construct(?IKauTranslations $lang = null) |
26
|
|
|
{ |
27
|
3 |
|
$this->setAuLang($lang); |
28
|
3 |
|
} |
29
|
|
|
|
30
|
2 |
|
public function setTree(?Methods\AMethods $authTree): void |
31
|
|
|
{ |
32
|
2 |
|
$this->usedMethod = null; |
33
|
2 |
|
$this->authTree = $authTree; |
34
|
2 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ArrayAccess<string, string|int|float> $credentials |
38
|
|
|
* @throws AccountsException |
39
|
|
|
* @throws AuthException |
40
|
|
|
*/ |
41
|
2 |
|
public function findMethod(ArrayAccess $credentials): void |
42
|
|
|
{ |
43
|
2 |
|
$currentMethod = $this->getAuthTree(); |
44
|
|
|
do { |
45
|
1 |
|
$currentMethod->process($credentials); |
46
|
1 |
|
if ($currentMethod->isAuthorized()) { |
47
|
1 |
|
$this->usedMethod = $currentMethod; |
48
|
1 |
|
return; |
49
|
|
|
} |
50
|
1 |
|
} while ($currentMethod = $currentMethod->getNextMethod()); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
1 |
|
public function getMethod(): ?Methods\AMethods |
54
|
|
|
{ |
55
|
1 |
|
return $this->usedMethod; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @throws AuthException |
60
|
|
|
* @return Methods\AMethods |
61
|
|
|
*/ |
62
|
2 |
|
protected function getAuthTree(): Methods\AMethods |
63
|
|
|
{ |
64
|
2 |
|
if (!$this->authTree) { |
65
|
1 |
|
throw new AuthException($this->getAuLang()->kauNoAuthTreeSet()); |
66
|
|
|
} |
67
|
1 |
|
return $this->authTree; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|