Skip to content

Commit

Permalink
Merge pull request #4 from spryker-eco/feature/cc-34041/cc-34136-pers…
Browse files Browse the repository at this point in the history
…ist-qs-user

CC-34136 Quicksight user registration and persistence
  • Loading branch information
AsonUnique authored Aug 7, 2024
2 parents a30a781 + 45fcd46 commit 20f2e87
Show file tree
Hide file tree
Showing 22 changed files with 984 additions and 11 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
},
"require-dev": {
"phpstan/phpstan": "*",
"spryker/code-sniffer": "*"
"spryker/code-sniffer": "*",
"spryker/propel": "*",
"spryker/testify": "*"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ parameters:
- '#Parameter .+ of method .+ has invalid typehint type .+Transfer#'
- '#Return typehint of method .+ has invalid type .+Transfer#'
- '#Instantiated class .+Transfer not found#'
- '#Instantiated class Orm\\Zed\\AmazonQuicksight\\Persistence\\.+ not found.+#'
- '#Return typehint of method .+ has invalid type Orm\\.+#'
- '#Call to method .+\(\) on an unknown class Orm\\.+#'
- '#Call to static method create\(\) on an unknown class .+Query.#'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,35 @@
<property name="updatedAt" type="string"/>
</transfer>

<transfer name="UserCollectionResponse" strict="true">
<property name="users" type="User[]" singular="user"/>
<property name="errors" type="Error[]" singular="error"/>
</transfer>

<transfer name="User">
<property name="idUser" type="int"/>
<property name="quicksightUser" type="QuicksightUser" strict="true"/>
<property name="idUser" type="int"/>
<property name="username" type="string"/>
</transfer>

<transfer name="QuicksightUserRegisterResponse" strict="true">
<property name="errors" type="Error[]" singular="error"/>
<property name="quicksightUser" type="QuicksightUser"/>
</transfer>

<transfer name="QuicksightUserRegisterRequest" strict="true">
<property name="awsAccountId" type="string"/>
<property name="email" type="string" />
<property name="identityType" type="string" />
<property name="namespace" type="string"/>
<property name="userRole" type="string" />
<property name="userName" type="string"/>
</transfer>

<transfer name="Error">
<property name="message" type="string"/>
<property name="entityIdentifier" type="string"/>
</transfer>

<transfer name="UserCollection" strict="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AmazonQuicksightConfig extends AbstractBundleConfig
/**
* @var string
*/
protected const QUICKSIGHT_USER_REGISTER_NAMESPACE = 'default';
protected const QUICKSIGHT_REGISTER_USER_NAMESPACE = 'default';

/**
* @var string
Expand Down Expand Up @@ -66,9 +66,9 @@ public function getAwsAccountId(): string
*
* @return string
*/
public function getQuicksightUserRegisterNamespace(): string
public function getQuicksightRegisterUserNamespace(): string
{
return static::QUICKSIGHT_USER_REGISTER_NAMESPACE;
return static::QUICKSIGHT_REGISTER_USER_NAMESPACE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@
namespace SprykerEco\Zed\AmazonQuicksight\Business;

use Spryker\Zed\Kernel\Business\AbstractBusinessFactory;
use SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightDependencyProvider;
use SprykerEco\Zed\AmazonQuicksight\Business\ApiClient\AmazonQuicksightApiClient;
use SprykerEco\Zed\AmazonQuicksight\Business\ApiClient\AmazonQuicksightApiClientInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Creator\QuicksightUserCreator;
use SprykerEco\Zed\AmazonQuicksight\Business\Creator\QuicksightUserCreatorInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpander;
use SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpanderInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatter;
use SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapper;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface;
use SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface;

/**
* @method \SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig getConfig()
Expand All @@ -25,4 +35,52 @@ public function createUserExpander(): UserExpanderInterface
{
return new UserExpander($this->getRepository());
}

/**
* @return \SprykerEco\Zed\AmazonQuicksight\Business\Creator\QuicksightUserCreatorInterface
*/
public function createQuicksightUserCreator(): QuicksightUserCreatorInterface
{
return new QuicksightUserCreator(
$this->createAmazonQuicksightApiClient(),
$this->getEntityManager(),
);
}

/**
* @return \SprykerEco\Zed\AmazonQuicksight\Business\ApiClient\AmazonQuicksightApiClientInterface
*/
public function createAmazonQuicksightApiClient(): AmazonQuicksightApiClientInterface
{
return new AmazonQuicksightApiClient(
$this->getConfig(),
$this->createQuicksightUserMapper(),
$this->createAmazonQuicksightRequestDataFormatter(),
$this->getAwsQuicksightClient(),
);
}

/**
* @return \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface
*/
public function createQuicksightUserMapper(): QuicksightUserMapperInterface
{
return new QuicksightUserMapper();
}

/**
* @return \SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface
*/
public function createAmazonQuicksightRequestDataFormatter(): AmazonQuicksightRequestDataFormatterInterface
{
return new AmazonQuicksightRequestDataFormatter();
}

/**
* @return \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface
*/
public function getAwsQuicksightClient(): AmazonQuicksightToAwsQuicksightClientInterface
{
return $this->getProvidedDependency(AmazonQuicksightDependencyProvider::AWS_QUICKSIGHT_CLIENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace SprykerEco\Zed\AmazonQuicksight\Business;

use Generated\Shared\Transfer\UserCollectionResponseTransfer;
use Generated\Shared\Transfer\UserCollectionTransfer;
use Spryker\Zed\Kernel\Business\AbstractFacade;

Expand All @@ -31,4 +32,21 @@ public function expandUserCollectionWithQuicksightUsers(
): UserCollectionTransfer {
return $this->getFactory()->createUserExpander()->expandUserCollectionWithQuicksightUser($userCollectionTransfer);
}

/**
* {@inheritDoc}
*
* @api
*
* @param \Generated\Shared\Transfer\UserCollectionResponseTransfer $userCollectionResponseTransfer
*
* @return \Generated\Shared\Transfer\UserCollectionResponseTransfer
*/
public function createQuicksightUsersForUserCollectionResponse(
UserCollectionResponseTransfer $userCollectionResponseTransfer
): UserCollectionResponseTransfer {
return $this->getFactory()
->createQuicksightUserCreator()
->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace SprykerEco\Zed\AmazonQuicksight\Business;

use Generated\Shared\Transfer\UserCollectionResponseTransfer;
use Generated\Shared\Transfer\UserCollectionTransfer;

interface AmazonQuicksightFacadeInterface
Expand All @@ -27,4 +28,23 @@ interface AmazonQuicksightFacadeInterface
public function expandUserCollectionWithQuicksightUsers(
UserCollectionTransfer $userCollectionTransfer
): UserCollectionTransfer;

/**
* Specification:
* - Expects `UserCollectionResponseTransfer.users.quicksightUser.role` to be set.
* - Does nothing if `UserTransfer.quicksightUser.role` is not set.
* - Sends request to AWS API to register Quicksight users. For more information see {@link https://docs.aws.amazon.com/quicksight/latest/APIReference/API_RegisterUser.html}.
* - Adds errors to `UserCollectionResponseTransfer.errors` if Quicksight user registration failed.
* - Persists successfully registered Quicksight users in the database.
* - Returns `UserCollectionResponseTransfer` with updated `UserTransfers`.
*
* @api
*
* @param \Generated\Shared\Transfer\UserCollectionResponseTransfer $userCollectionResponseTransfer
*
* @return \Generated\Shared\Transfer\UserCollectionResponseTransfer
*/
public function createQuicksightUsersForUserCollectionResponse(
UserCollectionResponseTransfer $userCollectionResponseTransfer
): UserCollectionResponseTransfer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

/**
* MIT License
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerEco\Zed\AmazonQuicksight\Business\ApiClient;

use Aws\QuickSight\Exception\QuickSightException;
use Generated\Shared\Transfer\ErrorTransfer;
use Generated\Shared\Transfer\QuicksightUserRegisterRequestTransfer;
use Generated\Shared\Transfer\QuicksightUserRegisterResponseTransfer;
use Generated\Shared\Transfer\UserTransfer;
use SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig;
use SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface;
use SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface;

class AmazonQuicksightApiClient implements AmazonQuicksightApiClientInterface
{
/**
* @var string
*/
protected const QUICKSIGHT_USER_IDENTITY_TYPE = 'QUICKSIGHT';

/**
* @link https://docs.aws.amazon.com/quicksight/latest/APIReference/API_RegisterUser.html#API_RegisterUser_ResponseSyntax
*
* @var string
*/
protected const RESPONSE_KEY_USER = 'User';

/**
* @var string
*/
protected const ERROR_MESSAGE_USER_REGISTRATION_FAILED = 'Failed to register Quicksight user.';

/**
* @var \SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig
*/
protected AmazonQuicksightConfig $amazonQuicksightConfig;

/**
* @var \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface
*/
protected QuicksightUserMapperInterface $quicksightUserMapper;

/**
* @var \SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface
*/
protected AmazonQuicksightRequestDataFormatterInterface $amazonQuicksightRequestDataFormatter;

/**
* @var \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface
*/
protected AmazonQuicksightToAwsQuicksightClientInterface $amazonQuicksightToAwsQuicksightClient;

/**
* @param \SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig $amazonQuicksightConfig
* @param \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface $quicksightUserMapper
* @param \SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface $amazonQuicksightRequestDataFormatter
* @param \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface $amazonQuicksightToAwsQuicksightClient
*/
public function __construct(
AmazonQuicksightConfig $amazonQuicksightConfig,
QuicksightUserMapperInterface $quicksightUserMapper,
AmazonQuicksightRequestDataFormatterInterface $amazonQuicksightRequestDataFormatter,
AmazonQuicksightToAwsQuicksightClientInterface $amazonQuicksightToAwsQuicksightClient
) {
$this->amazonQuicksightConfig = $amazonQuicksightConfig;
$this->quicksightUserMapper = $quicksightUserMapper;
$this->amazonQuicksightRequestDataFormatter = $amazonQuicksightRequestDataFormatter;
$this->amazonQuicksightToAwsQuicksightClient = $amazonQuicksightToAwsQuicksightClient;
}

/**
* @param \Generated\Shared\Transfer\UserTransfer $userTransfer
*
* @return \Generated\Shared\Transfer\QuicksightUserRegisterResponseTransfer
*/
public function registerUser(UserTransfer $userTransfer): QuicksightUserRegisterResponseTransfer
{
$quicksightUserRegisterRequestTransfer = $this->createQuicksightUserRegisterRequestTransfer();
$quicksightUserRegisterRequestTransfer = $this->quicksightUserMapper->mapUserTransferToQuicksightUserRegisterRequestTransfer(
$userTransfer,
$quicksightUserRegisterRequestTransfer,
);

$requestData = $this->amazonQuicksightRequestDataFormatter->formatRequestData(
$quicksightUserRegisterRequestTransfer->toArray(true, true),
);

$quicksightUserRegisterResponseTransfer = new QuicksightUserRegisterResponseTransfer();
try {
$response = $this->amazonQuicksightToAwsQuicksightClient->registerUser($requestData);
} catch (QuickSightException $quickSightException) {
return $quicksightUserRegisterResponseTransfer->addError(
(new ErrorTransfer())->setMessage($quickSightException->getAwsErrorMessage()),
);
}

if (!$response->hasKey(static::RESPONSE_KEY_USER)) {
return $quicksightUserRegisterResponseTransfer->addError(
(new ErrorTransfer())->setMessage(static::ERROR_MESSAGE_USER_REGISTRATION_FAILED),
);
}

$quicksightUserTransfer = $this->quicksightUserMapper->mapQuicksightUserDataToQuicksightUserTransfer(
$response->get(static::RESPONSE_KEY_USER),
$userTransfer->getQuicksightUserOrFail(),
);

return $quicksightUserRegisterResponseTransfer->setQuicksightUser($quicksightUserTransfer);
}

/**
* @return \Generated\Shared\Transfer\QuicksightUserRegisterRequestTransfer
*/
protected function createQuicksightUserRegisterRequestTransfer(): QuicksightUserRegisterRequestTransfer
{
return (new QuicksightUserRegisterRequestTransfer())
->setIdentityType(static::QUICKSIGHT_USER_IDENTITY_TYPE)
->setAwsAccountId($this->amazonQuicksightConfig->getAwsAccountId())
->setNamespace($this->amazonQuicksightConfig->getQuicksightRegisterUserNamespace());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* MIT License
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerEco\Zed\AmazonQuicksight\Business\ApiClient;

use Generated\Shared\Transfer\QuicksightUserRegisterResponseTransfer;
use Generated\Shared\Transfer\UserTransfer;

interface AmazonQuicksightApiClientInterface
{
/**
* @param \Generated\Shared\Transfer\UserTransfer $userTransfer
*
* @return \Generated\Shared\Transfer\QuicksightUserRegisterResponseTransfer
*/
public function registerUser(UserTransfer $userTransfer): QuicksightUserRegisterResponseTransfer;
}
Loading

0 comments on commit 20f2e87

Please sign in to comment.