Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.19% |
23 / 27 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| UserContextExtender | |
85.19% |
23 / 27 |
|
60.00% |
3 / 5 |
14.64 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| extend | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| getContextUserData | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| getUserIdentifier | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
4.59 | |||
| getUserRoles | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
4.12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2021 (original work) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\oatbox\log\logger\extender; |
| 24 | |
| 25 | use Throwable; |
| 26 | use oat\oatbox\session\SessionService; |
| 27 | |
| 28 | class UserContextExtender implements ContextExtenderInterface |
| 29 | { |
| 30 | public const ACL_SERVICE_ID = self::class . '::ACL'; |
| 31 | |
| 32 | /** @var SessionService */ |
| 33 | private $sessionService; |
| 34 | |
| 35 | /** @var array|null */ |
| 36 | private $userData; |
| 37 | |
| 38 | /** @var bool */ |
| 39 | private $extendWithUserRoles; |
| 40 | |
| 41 | public function __construct(SessionService $sessionService, bool $extendWithUserRoles = false) |
| 42 | { |
| 43 | $this->sessionService = $sessionService; |
| 44 | $this->extendWithUserRoles = $extendWithUserRoles; |
| 45 | } |
| 46 | |
| 47 | public function extend(array $context): array |
| 48 | { |
| 49 | if (isset($context[self::CONTEXT_USER_DATA]) && is_array($context[self::CONTEXT_USER_DATA])) { |
| 50 | $context[self::CONTEXT_USER_DATA] = array_merge( |
| 51 | $context[self::CONTEXT_USER_DATA], |
| 52 | $this->getContextUserData() |
| 53 | ); |
| 54 | |
| 55 | return $context; |
| 56 | } |
| 57 | |
| 58 | $context[self::CONTEXT_USER_DATA] = $this->getContextUserData(); |
| 59 | |
| 60 | return $context; |
| 61 | } |
| 62 | |
| 63 | private function getContextUserData(): array |
| 64 | { |
| 65 | if ($this->userData === null) { |
| 66 | $this->userData = [ |
| 67 | 'id' => $this->getUserIdentifier(), |
| 68 | ]; |
| 69 | |
| 70 | if ($this->extendWithUserRoles) { |
| 71 | $this->userData['roles'] = $this->getUserRoles(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return $this->userData; |
| 76 | } |
| 77 | |
| 78 | private function getUserIdentifier(): ?string |
| 79 | { |
| 80 | try { |
| 81 | if ($this->sessionService->isAnonymous()) { |
| 82 | return 'anonymous'; |
| 83 | } |
| 84 | |
| 85 | $user = $this->sessionService->getCurrentUser(); |
| 86 | |
| 87 | return $user ? $user->getIdentifier() : 'system'; |
| 88 | } catch (Throwable $exception) { |
| 89 | return 'unreachable'; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private function getUserRoles(): array |
| 94 | { |
| 95 | try { |
| 96 | $user = $this->sessionService->getCurrentUser(); |
| 97 | |
| 98 | return $user ? $user->getRoles() : []; |
| 99 | } catch (Throwable $exception) { |
| 100 | return []; |
| 101 | } |
| 102 | } |
| 103 | } |