|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Component\Auth\Adapters; |
|
4
|
|
|
|
|
5
|
|
|
use Nymfonya\Component\Config; |
|
|
|
|
|
|
6
|
|
|
use Nymfonya\Component\Container; |
|
7
|
|
|
use App\Component\Auth\AdapterInterface; |
|
8
|
|
|
use App\Component\Crypt; |
|
9
|
|
|
use App\Model\Repository\Users; |
|
10
|
|
|
use App\Component\Db\Core; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Adapter Repository let auth from config accounts entries |
|
14
|
|
|
* Decryption required on password. |
|
15
|
|
|
*/ |
|
16
|
|
|
class Repository implements AdapterInterface |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
const _PASSWORD = 'password'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* container |
|
23
|
|
|
* |
|
24
|
|
|
* @var Container |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $container; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* app config |
|
30
|
|
|
* |
|
31
|
|
|
* @var Config |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $config; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* user repository |
|
37
|
|
|
* |
|
38
|
|
|
* @var Users |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $userRepo; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* db core instance |
|
44
|
|
|
* |
|
45
|
|
|
* @var Core |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $dbCore; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* instanciate |
|
51
|
|
|
* |
|
52
|
|
|
* @param Container $container |
|
53
|
|
|
*/ |
|
54
|
5 |
|
public function __construct(Container $container) |
|
55
|
|
|
{ |
|
56
|
5 |
|
$this->container = $container; |
|
57
|
5 |
|
$this->userRepo = new Users($this->container); |
|
58
|
5 |
|
$this->dbCore = new Core($this->container); |
|
59
|
5 |
|
$this->dbCore->fromOrm($this->userRepo); |
|
60
|
5 |
|
$this->config = $this->container->getService(Config::class); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* auth process |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function auth(string $login, string $password): array |
|
69
|
|
|
{ |
|
70
|
2 |
|
$this->userRepo->getByEmail($login); |
|
71
|
2 |
|
$this->dbCore |
|
72
|
2 |
|
->run( |
|
73
|
2 |
|
$this->userRepo->getSql(), |
|
74
|
2 |
|
$this->userRepo->getBuilderValues() |
|
75
|
|
|
) |
|
76
|
2 |
|
->hydrate(); |
|
77
|
2 |
|
$result = $this->dbCore->getRowset(); |
|
78
|
2 |
|
if (empty($result)) { |
|
79
|
1 |
|
return []; |
|
80
|
|
|
} |
|
81
|
1 |
|
$user = $result[0]; |
|
82
|
1 |
|
$clearPassword = $this->decrypt($user[self::_PASSWORD]); |
|
83
|
1 |
|
return ($password === $clearPassword) ? $user : []; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* return user by id |
|
88
|
|
|
* |
|
89
|
|
|
* @param integer $id |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getById(int $id): array |
|
93
|
|
|
{ |
|
94
|
1 |
|
$this->userRepo->getById($id); |
|
95
|
1 |
|
$this->dbCore |
|
96
|
1 |
|
->run( |
|
97
|
1 |
|
$this->userRepo->getSql(), |
|
98
|
1 |
|
$this->userRepo->getBuilderValues() |
|
99
|
|
|
) |
|
100
|
1 |
|
->hydrate(); |
|
101
|
1 |
|
$result = $this->dbCore->getRowset(); |
|
102
|
1 |
|
if (empty($result)) { |
|
103
|
1 |
|
return []; |
|
104
|
|
|
} |
|
105
|
1 |
|
$user = $result[0]; |
|
106
|
1 |
|
$user[self::_PASSWORD] = $this->decrypt($user[self::_PASSWORD]); |
|
107
|
1 |
|
return $user; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* decrypt content |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $content |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
1 |
|
protected function decrypt(string $content): string |
|
117
|
|
|
{ |
|
118
|
1 |
|
return (new Crypt($this->config))->decrypt($content, true); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: