Skip to content

Commit

Permalink
CC-33738 Audit Logs with CloudWatch. (#10986)
Browse files Browse the repository at this point in the history
CC-33738 Audit Logs with CloudWatch
  • Loading branch information
ilyakubanov authored Jul 9, 2024
1 parent 5038003 commit 98f224b
Show file tree
Hide file tree
Showing 10 changed files with 437 additions and 0 deletions.
1 change: 1 addition & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace: Customer
include:
- tests/SprykerTest/Zed/Customer
- tests/SprykerTest/Client/Customer
- tests/SprykerTest/Yves/Customer

paths:
tests: tests
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"spryker/container": "*",
"spryker/event-dispatcher": "*",
"spryker/form": "*",
"spryker/log": "*",
"spryker/sales": "*",
"spryker/testify": "*",
"spryker/twig": "*",
Expand All @@ -46,6 +47,7 @@
},
"suggest": {
"spryker/checkout": "If you want to use Checkout plugins.",
"spryker/log": "If you want to use Log plugins.",
"spryker/sales": "If you want customer information in sales."
},
"autoload": {
Expand Down
50 changes: 50 additions & 0 deletions src/Spryker/Yves/Customer/CustomerDependencyProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Yves\Customer;

use Spryker\Yves\Kernel\AbstractBundleDependencyProvider;
use Spryker\Yves\Kernel\Container;

/**
* @method \Spryker\Yves\Customer\CustomerConfig getConfig()
*/
class CustomerDependencyProvider extends AbstractBundleDependencyProvider
{
/**
* @uses \Spryker\Yves\Http\Plugin\Application\HttpApplicationPlugin::SERVICE_REQUEST_STACK
*
* @var string
*/
public const SERVICE_REQUEST_STACK = 'request_stack';

/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
public function provideDependencies(Container $container): Container
{
$container = $this->addRequestStackService($container);

return $container;
}

/**
* @param \Spryker\Yves\Kernel\Container $container
*
* @return \Spryker\Yves\Kernel\Container
*/
protected function addRequestStackService(Container $container): Container
{
$container->set(static::SERVICE_REQUEST_STACK, function (Container $container) {
return $container->getApplicationService(static::SERVICE_REQUEST_STACK);
});

return $container;
}
}
32 changes: 32 additions & 0 deletions src/Spryker/Yves/Customer/CustomerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Yves\Customer;

use Spryker\Yves\Customer\Processor\CurrentCustomerDataRequestLogProcessor;
use Spryker\Yves\Customer\Processor\CurrentCustomerDataRequestLogProcessorInterface;
use Spryker\Yves\Kernel\AbstractFactory;
use Symfony\Component\HttpFoundation\RequestStack;

class CustomerFactory extends AbstractFactory
{
/**
* @return \Spryker\Yves\Customer\Processor\CurrentCustomerDataRequestLogProcessorInterface
*/
public function createCurrentCustomerDataRequestLogProcessor(): CurrentCustomerDataRequestLogProcessorInterface
{
return new CurrentCustomerDataRequestLogProcessor($this->getRequestStackService());
}

/**
* @return \Symfony\Component\HttpFoundation\RequestStack
*/
public function getRequestStackService(): RequestStack
{
return $this->getProvidedDependency(CustomerDependencyProvider::SERVICE_REQUEST_STACK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Yves\Customer\Plugin\Log;

use Spryker\Shared\Log\Dependency\Plugin\LogProcessorPluginInterface;
use Spryker\Yves\Kernel\AbstractPlugin;

/**
* @method \Spryker\Yves\Customer\CustomerFactory getFactory()
* @method \Spryker\Yves\Customer\CustomerConfig getConfig()
*/
class CurrentCustomerDataRequestProcessorPlugin extends AbstractPlugin implements LogProcessorPluginInterface
{
/**
* {@inheritDoc}
* - Adds customer email and customer reference from the current request.
*
* @api
*
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*/
public function __invoke(array $data): array
{
return $this->getFactory()->createCurrentCustomerDataRequestLogProcessor()->__invoke($data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Yves\Customer\Processor;

use Generated\Shared\Transfer\CustomerTransfer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class CurrentCustomerDataRequestLogProcessor implements CurrentCustomerDataRequestLogProcessorInterface
{
/**
* @var string
*/
protected const RECORD_KEY_EXTRA = 'extra';

/**
* @var string
*/
protected const RECORD_KEY_REQUEST = 'request';

/**
* @uses \Spryker\Client\Customer\Session\CustomerSession::SESSION_KEY
*
* @var string
*/
protected const SESSION_KEY_CUSTOMER_DATA = 'customer data';

/**
* @var string
*/
protected const RECORD_KEY_USERNAME = 'username';

/**
* @var string
*/
protected const RECORD_KEY_CUSTOMER_REFERENCE = 'customer_reference';

/**
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected RequestStack $requestStack;

/**
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}

/**
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*/
public function __invoke(array $data): array
{
$customerTransfer = $this->findCurrentCustomer();

if (!$customerTransfer) {
return $data;
}

$currentRequestData = $this->getCurrentRequestData($customerTransfer);

if (isset($data[static::RECORD_KEY_EXTRA][static::RECORD_KEY_REQUEST])) {
$data[static::RECORD_KEY_EXTRA][static::RECORD_KEY_REQUEST] = array_merge(
$data[static::RECORD_KEY_EXTRA][static::RECORD_KEY_REQUEST],
$currentRequestData,
);

return $data;
}

$data[static::RECORD_KEY_EXTRA][static::RECORD_KEY_REQUEST] = $currentRequestData;

return $data;
}

/**
* @return \Generated\Shared\Transfer\CustomerTransfer|null
*/
protected function findCurrentCustomer(): ?CustomerTransfer
{
$currentRequest = $this->requestStack->getCurrentRequest();

if (!$currentRequest || !$currentRequest->hasSession()) {
return null;
}

return $this->findCustomerInRequest($currentRequest);
}

/**
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return array<string, mixed>
*/
protected function getCurrentRequestData(CustomerTransfer $customerTransfer): array
{
$currentRequestData = [];

$currentRequestData[static::RECORD_KEY_USERNAME] = $customerTransfer->getEmail();
$currentRequestData[static::RECORD_KEY_CUSTOMER_REFERENCE] = $customerTransfer->getCustomerReference();

return $currentRequestData;
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Generated\Shared\Transfer\CustomerTransfer|null
*/
protected function findCustomerInRequest(Request $request): ?CustomerTransfer
{
return $request->getSession()->get(static::SESSION_KEY_CUSTOMER_DATA);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Yves\Customer\Processor;

interface CurrentCustomerDataRequestLogProcessorInterface
{
/**
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*/
public function __invoke(array $data): array;
}
Loading

0 comments on commit 98f224b

Please sign in to comment.