|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Component\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use Nymfonya\Component\Config; |
|
6
|
|
|
use Nymfonya\Component\Container; |
|
7
|
|
|
use App\Component\Auth\AdapterInterface; |
|
8
|
|
|
use App\Component\Auth\Adapters\File as FileAdapter; |
|
9
|
|
|
use App\Component\Auth\Adapters\Config as ConfigAdapter; |
|
10
|
|
|
use App\Component\Auth\Adapters\Repository as RepositoryAdapter; |
|
11
|
|
|
|
|
12
|
|
|
class Factory implements AdapterInterface |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
const CONFIG_AUTH_ADAPTER = 'auth'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* container |
|
19
|
|
|
* |
|
20
|
|
|
* @var Container |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $container; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* adapter |
|
26
|
|
|
* |
|
27
|
|
|
* @var AdapterInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $adapter; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* instanciate |
|
33
|
|
|
* |
|
34
|
|
|
* @param Container $container |
|
35
|
|
|
*/ |
|
36
|
8 |
|
public function __construct(Container $container) |
|
37
|
|
|
{ |
|
38
|
8 |
|
$this->container = $container; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* set auth adapter |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $classname |
|
45
|
|
|
* @return Factory |
|
46
|
|
|
*/ |
|
47
|
7 |
|
public function setAdapter(string $classname = ''): Factory |
|
48
|
|
|
{ |
|
49
|
7 |
|
if (empty($classname)) { |
|
50
|
3 |
|
$config = $this->container->getService(Config::class); |
|
51
|
3 |
|
if (false === $config->hasEntry(self::CONFIG_AUTH_ADAPTER)) { |
|
52
|
1 |
|
throw new \Exception('Missing auth config entry'); |
|
53
|
|
|
} |
|
54
|
2 |
|
$adapterConfig = $config->getSettings(self::CONFIG_AUTH_ADAPTER); |
|
55
|
2 |
|
if (false === isset($adapterConfig['adapter'])) { |
|
56
|
1 |
|
throw new \Exception('Missing auth config adapter'); |
|
57
|
|
|
} |
|
58
|
1 |
|
$adapterClassname = $adapterConfig['adapter']; |
|
59
|
|
|
} else { |
|
60
|
4 |
|
$adapterClassname = $classname; |
|
61
|
|
|
} |
|
62
|
|
|
$allowedAdapters = [ |
|
63
|
5 |
|
FileAdapter::class, |
|
64
|
|
|
ConfigAdapter::class, |
|
65
|
|
|
RepositoryAdapter::class |
|
66
|
|
|
]; |
|
67
|
5 |
|
if (false === in_array($adapterClassname, $allowedAdapters)) { |
|
68
|
1 |
|
throw new \Exception('Bad auth adapter classname'); |
|
69
|
|
|
} |
|
70
|
4 |
|
$this->adapter = new $adapterClassname($this->container); |
|
71
|
4 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* return adapter instance |
|
76
|
|
|
* |
|
77
|
|
|
* @return AdapterInterface |
|
78
|
|
|
*/ |
|
79
|
4 |
|
public function getAdapter(): AdapterInterface |
|
80
|
|
|
{ |
|
81
|
4 |
|
return $this->adapter; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* return auth user |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
4 |
|
public function auth(string $login, string $password): array |
|
90
|
|
|
{ |
|
91
|
4 |
|
return $this->getAdapter()->auth($login, $password); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|