1 | <?php |
||||
2 | |||||
3 | namespace Lagdo\DbAdmin\Config; |
||||
4 | |||||
5 | use Jaxon\Config\ConfigReader; |
||||
6 | use Jaxon\Config\ConfigSetter; |
||||
7 | |||||
8 | use function array_map; |
||||
9 | use function array_filter; |
||||
10 | use function array_values; |
||||
11 | use function env; |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
12 | use function in_array; |
||||
13 | use function is_array; |
||||
14 | use function is_file; |
||||
15 | use function is_numeric; |
||||
16 | use function is_string; |
||||
17 | use function preg_match; |
||||
18 | |||||
19 | class UserFileReader |
||||
20 | { |
||||
21 | /** |
||||
22 | * @var string |
||||
23 | */ |
||||
24 | private string $compareRegex = '/^env\(.*\)$/'; |
||||
25 | |||||
26 | /** |
||||
27 | * @var string |
||||
28 | */ |
||||
29 | private string $captureRegex = '/^env\((.*)\)$/'; |
||||
30 | |||||
31 | /** |
||||
32 | * The constructor |
||||
33 | * |
||||
34 | * @param AuthInterface $auth |
||||
35 | */ |
||||
36 | public function __construct(private AuthInterface $auth) |
||||
37 | {} |
||||
38 | |||||
39 | /** |
||||
40 | * @param string $value |
||||
41 | * |
||||
42 | * @return bool |
||||
43 | */ |
||||
44 | private function callsEnvVar(string $value): bool |
||||
45 | { |
||||
46 | return preg_match($this->compareRegex, $value) !== false; |
||||
47 | } |
||||
48 | |||||
49 | /** |
||||
50 | * @param array $server |
||||
51 | * |
||||
52 | * @return bool |
||||
53 | */ |
||||
54 | private function checkPortNumber(array $server): bool |
||||
55 | { |
||||
56 | if (!isset($server['port']) || is_numeric($server['port'])) { |
||||
57 | return true; |
||||
58 | } |
||||
59 | if (!is_string($server['port'])) { |
||||
60 | return false; |
||||
61 | } |
||||
62 | return $this->callsEnvVar($server['port']); |
||||
63 | } |
||||
64 | |||||
65 | /** |
||||
66 | * @param array $server |
||||
67 | * |
||||
68 | * @return bool |
||||
69 | */ |
||||
70 | private function checkServer(array $server): bool |
||||
71 | { |
||||
72 | if (!isset($server['name']) || !isset($server['driver']) || |
||||
73 | !is_string($server['name']) || !is_string($server['driver'])) { |
||||
74 | return false; |
||||
75 | } |
||||
76 | if ($server['driver'] === 'sqlite') { |
||||
77 | return isset($server['directory']) && is_string($server['directory']); |
||||
78 | } |
||||
79 | return isset($server['username']) && isset($server['password']) && |
||||
80 | isset($server['host']) && is_string($server['username']) && |
||||
81 | is_string($server['password']) && is_string($server['host']) && |
||||
82 | $this->checkPortNumber($server); |
||||
83 | } |
||||
84 | |||||
85 | /** |
||||
86 | * @param array $options |
||||
87 | * |
||||
88 | * @return bool |
||||
89 | */ |
||||
90 | private function checkUser(array $options): bool |
||||
91 | { |
||||
92 | $user = $options['id']['user'] ?? null; |
||||
93 | return is_string($user) && $this->auth->user() === $user; |
||||
94 | } |
||||
95 | |||||
96 | /** |
||||
97 | * @param array $options |
||||
98 | * |
||||
99 | * @return bool |
||||
100 | */ |
||||
101 | private function checkUsers(array $options): bool |
||||
102 | { |
||||
103 | $users = $options['id']['users'] ?? null; |
||||
104 | return is_array($users) && in_array($this->auth->user(), $users); |
||||
105 | } |
||||
106 | |||||
107 | /** |
||||
108 | * @param array $options |
||||
109 | * |
||||
110 | * @return bool |
||||
111 | */ |
||||
112 | private function checkRole(array $options): bool |
||||
113 | { |
||||
114 | $role = $options['id']['role'] ?? null; |
||||
115 | return is_string($role) && $this->auth->role() === $role; |
||||
116 | } |
||||
117 | |||||
118 | /** |
||||
119 | * @param array $options |
||||
120 | * |
||||
121 | * @return bool |
||||
122 | */ |
||||
123 | private function checkRoles(array $options): bool |
||||
124 | { |
||||
125 | $roles = $options['id']['roles'] ?? null; |
||||
126 | return is_array($roles) && in_array($this->auth->role(), $roles); |
||||
127 | } |
||||
128 | |||||
129 | /** |
||||
130 | * @param array $options |
||||
131 | * |
||||
132 | * @return bool |
||||
133 | */ |
||||
134 | private function userMatches(array $options): bool |
||||
135 | { |
||||
136 | return $this->checkUser($options) || $this->checkUsers($options) || |
||||
137 | $this->checkRole($options) || $this->checkRoles($options); |
||||
138 | } |
||||
139 | |||||
140 | /** |
||||
141 | * @param string $value |
||||
142 | * |
||||
143 | * @return mixed |
||||
144 | */ |
||||
145 | private function getOptionValue(string $value): mixed |
||||
146 | { |
||||
147 | // We need to capture the matching string. |
||||
148 | $match = preg_match($this->captureRegex, $value, $matches); |
||||
149 | return $match === false || !isset($matches[1]) ? $value : env($matches[1]); |
||||
0 ignored issues
–
show
The function
env was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
150 | } |
||||
151 | |||||
152 | /** |
||||
153 | * @param array $server |
||||
154 | * |
||||
155 | * @return array |
||||
156 | */ |
||||
157 | public function getServerOptions(array $server): array |
||||
158 | { |
||||
159 | if ($server['driver'] === 'sqlite') { |
||||
160 | $server['directory'] = $this->getOptionValue($server['directory']); |
||||
161 | return $server; |
||||
162 | } |
||||
163 | |||||
164 | $server['host'] = $this->getOptionValue($server['host']); |
||||
165 | $server['username'] = $this->getOptionValue($server['username']); |
||||
166 | $server['password'] = $this->getOptionValue($server['password']); |
||||
167 | if (isset($server['port']) && is_string($server['port'])) { |
||||
168 | $server['port'] = $this->getOptionValue($server['port']); |
||||
169 | } |
||||
170 | return $server; |
||||
171 | } |
||||
172 | |||||
173 | /** |
||||
174 | * Replace options with values from the .env config. |
||||
175 | * |
||||
176 | * @param array $values |
||||
177 | * |
||||
178 | * @return array |
||||
179 | */ |
||||
180 | private function getOptionValues(array $values): array |
||||
181 | { |
||||
182 | // Callback to filter the servers list on valid entries. |
||||
183 | $check = fn(array $server) => $this->checkServer($server); |
||||
184 | // Callback to get the server options final values. |
||||
185 | $convert = fn(array $server) => $this->getServerOptions($server); |
||||
186 | $values['servers'] = array_map($convert, |
||||
187 | array_filter($values['servers'] ?? [], $check)); |
||||
188 | |||||
189 | return $values; |
||||
190 | } |
||||
191 | |||||
192 | /** |
||||
193 | * Get the options for the authenticated user. |
||||
194 | * |
||||
195 | * @param string $configFile |
||||
196 | * @param array $defaultOptions |
||||
197 | * |
||||
198 | * @return array |
||||
199 | */ |
||||
200 | public function getOptions(string $configFile, array $defaultOptions = []): array |
||||
201 | { |
||||
202 | // If the config file doesn't exists, return an empty array. |
||||
203 | if (!is_file($configFile)) { |
||||
204 | return []; |
||||
205 | } |
||||
206 | |||||
207 | // The key to use for the user options |
||||
208 | $userKey = 'user'; |
||||
209 | // Remove the provider field. |
||||
210 | unset($defaultOptions['provider']); |
||||
211 | |||||
212 | $setter = new ConfigSetter(); |
||||
213 | $reader = new ConfigReader($setter); |
||||
214 | $userConfig = $setter->newConfig([$userKey => $defaultOptions]); |
||||
215 | |||||
216 | $config = $reader->load($setter->newConfig(), $configFile); |
||||
217 | $commonOptions = $config->getOption('common', null); |
||||
218 | if (is_array($commonOptions)) { |
||||
219 | $userConfig = $setter->setOptions($userConfig, $commonOptions, $userKey); |
||||
220 | } |
||||
221 | |||||
222 | $fallbackOptions = $config->getOption('fallback', null); |
||||
223 | |||||
224 | $userList = $config->getOption('users', []); |
||||
225 | $userList = array_values(array_filter($userList, |
||||
226 | fn($options) => $this->userMatches($options))); |
||||
227 | $userOptions = $userList[0] ?? $fallbackOptions; |
||||
228 | |||||
229 | if (!is_array($userOptions)) { |
||||
230 | // Return nothing if no entry is found for the user. |
||||
231 | return []; |
||||
232 | } |
||||
233 | |||||
234 | // Remove the id field. |
||||
235 | unset($userOptions['id']); |
||||
236 | $userConfig = $setter->setOptions($userConfig, $userOptions, $userKey); |
||||
237 | |||||
238 | return $this->getOptionValues($userConfig->getOption($userKey)); |
||||
0 ignored issues
–
show
It seems like
$userConfig->getOption($userKey) can also be of type null ; however, parameter $values of Lagdo\DbAdmin\Config\Use...ader::getOptionValues() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
239 | } |
||||
240 | } |
||||
241 |