Skip to content

Commit

Permalink
Merge pull request #947 from oat-sa/release-15.11.0
Browse files Browse the repository at this point in the history
Release 15.11.0
  • Loading branch information
shpran authored Nov 30, 2021
2 parents 9b4a822 + bc9c850 commit ea27251
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 12 deletions.
59 changes: 53 additions & 6 deletions common/oatbox/log/ServiceProvider/LogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@

namespace oat\oatbox\log\ServiceProvider;

use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface;
use oat\oatbox\log\logger\AdvancedLogger;
use oat\oatbox\log\logger\extender\RequestContextExtender;
use oat\oatbox\log\logger\extender\UserContextExtender;
use oat\oatbox\log\LoggerService;
use oat\oatbox\session\SessionService;
use oat\oatbox\log\logger\AdvancedLogger;
use oat\oatbox\log\logger\extender\UserContextExtender;
use oat\oatbox\log\logger\extender\RequestContextExtender;
use oat\oatbox\log\logger\extender\ExceptionContextExtender;
use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

class LogServiceProvider implements ContainerServiceProviderInterface
Expand All @@ -38,16 +40,34 @@ public function __invoke(ContainerConfigurator $configurator): void
{
$services = $configurator->services();

$services->set(ExceptionContextExtender::class, ExceptionContextExtender::class);
$services->set(RequestContextExtender::class, RequestContextExtender::class);
$services->set(UserContextExtender::class, UserContextExtender::class)
$services
->set(UserContextExtender::class, UserContextExtender::class)
->args(
[
service(SessionService::SERVICE_ID),
]
);

$services->set(AdvancedLogger::class, AdvancedLogger::class)
$services
->set(UserContextExtender::ACL_SERVICE_ID, UserContextExtender::class)
->args(
[
service(SessionService::SERVICE_ID),
true,
]
);

$services
->set(AdvancedLogger::class, AdvancedLogger::class)
->public()
->call(
'addContextExtender',
[
service(ExceptionContextExtender::class),
]
)
->call(
'addContextExtender',
[
Expand All @@ -65,5 +85,32 @@ public function __invoke(ContainerConfigurator $configurator): void
service(LoggerService::SERVICE_ID),
]
);

$services
->set(AdvancedLogger::ACL_SERVICE_ID, AdvancedLogger::class)
->public()
->args(
[
service(LoggerService::SERVICE_ID),
]
)
->call(
'addContextExtender',
[
service(ExceptionContextExtender::class),
]
)
->call(
'addContextExtender',
[
service(RequestContextExtender::class),
]
)
->call(
'addContextExtender',
[
service(UserContextExtender::ACL_SERVICE_ID),
]
);
}
}
2 changes: 2 additions & 0 deletions common/oatbox/log/logger/AdvancedLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

class AdvancedLogger implements LoggerInterface
{
public const ACL_SERVICE_ID = self::class . '::ACL';

/** @var LoggerInterface */
private $logger;

Expand Down
63 changes: 63 additions & 0 deletions common/oatbox/log/logger/extender/ExceptionContextExtender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2021 (original work) Open Assessment Technologies SA
*/

declare(strict_types=1);

namespace oat\oatbox\log\logger\extender;

use Throwable;

class ExceptionContextExtender implements ContextExtenderInterface
{
public function extend(array $context): array
{
if (isset($context[self::CONTEXT_EXCEPTION]) && $context[self::CONTEXT_EXCEPTION] instanceof Throwable) {
$context[self::CONTEXT_EXCEPTION] = $this->buildLogMessage($context[self::CONTEXT_EXCEPTION]);
}

return $context;
}

private function buildLogMessage(Throwable $exception): string
{
$message = $this->createMessage($exception);

if ($exception->getPrevious()) {
$message = sprintf(
'%s, previous: %s',
$message,
$this->buildLogMessage($exception->getPrevious())
);
}

return $message;
}

private function createMessage(Throwable $exception): string
{
return sprintf(
'"%s", code: %s, file: "%s", line: %s',
$exception->getMessage(),
$exception->getCode(),
$exception->getFile(),
$exception->getLine()
);
}
}
25 changes: 23 additions & 2 deletions common/oatbox/log/logger/extender/UserContextExtender.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@

namespace oat\oatbox\log\logger\extender;

use oat\oatbox\session\SessionService;
use Throwable;
use oat\oatbox\session\SessionService;

class UserContextExtender implements ContextExtenderInterface
{
public const ACL_SERVICE_ID = self::class . '::ACL';

/** @var SessionService */
private $sessionService;

/** @var array|null */
private $userData;

public function __construct(SessionService $sessionService)
/** @var bool */
private $extendWithUserRoles;

public function __construct(SessionService $sessionService, bool $extendWithUserRoles = false)
{
$this->sessionService = $sessionService;
$this->extendWithUserRoles = $extendWithUserRoles;
}

public function extend(array $context): array
Expand All @@ -60,6 +66,10 @@ private function getContextUserData(): array
$this->userData = [
'id' => $this->getUserIdentifier(),
];

if ($this->extendWithUserRoles) {
$this->userData['roles'] = $this->getUserRoles();
}
}

return $this->userData;
Expand All @@ -79,4 +89,15 @@ private function getUserIdentifier(): ?string
return 'unreachable';
}
}

private function getUserRoles(): array
{
try {
$user = $this->sessionService->getCurrentUser();

return $user ? $user->getRoles() : [];
} catch (Throwable $exception) {
return [];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2021 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace oat\generis\test\unit\common\oatbox\log\logger\extender;

use Exception;
use Throwable;
use oat\generis\test\TestCase;
use oat\oatbox\log\logger\extender\ContextExtenderInterface;
use oat\oatbox\log\logger\extender\ExceptionContextExtender;

class ExceptionContextExtenderTest extends TestCase
{
/** @var ExceptionContextExtender */
private $sut;

public function setUp(): void
{
$this->sut = new ExceptionContextExtender();
}

public function testExtend(): void
{
$this->assertSame(
[
ContextExtenderInterface::CONTEXT_EXCEPTION => '"Last error", code: 200, file: "'
. __FILE__ . '", line: 67, previous: "Original error", code: 100, file: "'
. __FILE__ . '", line: 70',
],
$this->sut->extend(
[
ContextExtenderInterface::CONTEXT_EXCEPTION => $this->createException(),
]
)
);
}

public function testExtendWithoutException(): void
{
$this->assertSame(
[],
$this->sut->extend([])
);
}

private function createException(): Throwable
{
return new Exception(
'Last error',
200,
new Exception(
'Original error',
100
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@

class UserContextExtenderTest extends TestCase
{
/** @var AdvancedLogger */
/** @var UserContextExtender */
private $sut;

/** @var SessionService|MockObject */
private $sessionService;

public function setUp(): void
{
$this->logger = $this->createMock(LoggerInterface::class);
$this->sessionService = $this->createMock(SessionService::class);
$this->sut = new UserContextExtender($this->sessionService);
}

public function testExtend(): void
{
$user = $this->createMock(User::class);
$user->method('getIdentifier')
$user
->method('getIdentifier')
->willReturn('userUri');

$this->sessionService
Expand All @@ -73,7 +73,8 @@ public function testExtend(): void
public function testExtendWithoutUserData(): void
{
$user = $this->createMock(User::class);
$user->method('getIdentifier')
$user
->method('getIdentifier')
->willReturn('userUri');

$this->sessionService
Expand Down Expand Up @@ -105,4 +106,31 @@ public function testExtendWithAnonymous(): void
$this->sut->extend([])
);
}

public function testExtendWithUserRoles(): void
{
$user = $this->createMock(User::class);
$user
->method('getIdentifier')
->willReturn('userUri');
$user
->method('getRoles')
->willReturn(['userRole']);

$this->sessionService
->method('getCurrentUser')
->willReturn($user);

$sut = new UserContextExtender($this->sessionService, true);

$this->assertSame(
[
ContextExtenderInterface::CONTEXT_USER_DATA => [
'id' => 'userUri',
'roles' => ['userRole'],
],
],
$sut->extend([])
);
}
}

0 comments on commit ea27251

Please sign in to comment.