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
|
9 |
|
public function __construct(Container $container) |
37
|
|
|
{ |
38
|
9 |
|
$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
|
5 |
|
if (false === in_array($adapterClassname, $this->allowedAdapters())) { |
63
|
1 |
|
throw new \Exception('Bad auth adapter classname'); |
64
|
|
|
} |
65
|
4 |
|
$this->adapter = new $adapterClassname($this->container); |
66
|
4 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* return adapter instance |
71
|
|
|
* |
72
|
|
|
* @return AdapterInterface |
73
|
|
|
*/ |
74
|
4 |
|
public function getAdapter(): AdapterInterface |
75
|
|
|
{ |
76
|
4 |
|
return $this->adapter; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* return auth user |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
4 |
|
public function auth(string $login, string $password): array |
85
|
|
|
{ |
86
|
4 |
|
return $this->getAdapter()->auth($login, $password); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* returns alloed adapter list |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
1 |
|
protected function allowedAdapters(): array |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
1 |
|
FileAdapter::class, ConfigAdapter::class, |
98
|
|
|
RepositoryAdapter::class |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|