Skip to content

Commit

Permalink
FRW-7668 Fixed agent validation during refresh user action. (#2484)
Browse files Browse the repository at this point in the history
FRW-7668 Fixed agent user validation during refresh user action.
  • Loading branch information
geega authored Aug 8, 2024
1 parent 2c0c330 commit 580351f
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"spryker-shop/customer-page-extension": "^1.2.0",
"spryker-shop/shop-application": "^1.1.0",
"spryker-shop/shop-ui": "^1.3.0",
"spryker/agent": "^1.4.0",
"spryker/agent": "^1.7.0",
"spryker/application": "^3.9.0",
"spryker/customer": "^7.10.0",
"spryker/kernel": "^3.52.0",
Expand Down
22 changes: 20 additions & 2 deletions src/SprykerShop/Yves/AgentPage/Controller/AgentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@
*/
class AgentController extends AbstractController
{
/**
* @var string
*/
protected const LOGIN_REDIRECT_URL = '/agent/login';

/**
* @return \Spryker\Yves\Kernel\View\View
*/
public function indexAction(): View
{
$viewData = $this->executeIndexAction();

return $this->view($viewData, [], '@AgentPage/views/overview/overview.twig');
return $this->view($viewData, [], $this->getTemplatePath());
}

/**
Expand All @@ -31,7 +36,20 @@ public function indexAction(): View
protected function executeIndexAction(): array
{
return [
'agent' => $this->getFactory()->getAgentClient()->getAgent(),
'agent' => $this->getFactory()->getAgentClient()->isLoggedIn() ? $this->getFactory()->getAgentClient()->getAgent() : null,
'loginRedirectUrl' => static::LOGIN_REDIRECT_URL,
];
}

/**
* @return string
*/
protected function getTemplatePath(): string
{
if ($this->getFactory()->getAgentClient()->isLoggedIn() === false) {
return '@AgentPage/views/login/redirect-to-login.twig';
}

return '@AgentPage/views/overview/overview.twig';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public function setAgent(UserTransfer $userTransfer): void
$this->agentClient->setAgent($userTransfer);
}

/**
* @return void
*/
public function invalidateAgentSession(): void
{
$this->agentClient->invalidateAgentSession();
}

/**
* @return void
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function getAgent(): UserTransfer;
*/
public function setAgent(UserTransfer $userTransfer): void;

/**
* @return void
*/
public function invalidateAgentSession(): void;

/**
* @return void
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ public function refreshUser(UserInterface $user)
return $user;
}

$userTransfer = $this->getUserTransfer($user);
$agentUserTransfer = $this->getUserTransfer($user);

return $this->getFactory()->createSecurityUser($userTransfer);
if ($agentUserTransfer === null) {
return $user;
}

return $this->getFactory()->createSecurityUser($agentUserTransfer);
}

/**
Expand Down Expand Up @@ -109,15 +113,17 @@ protected function findUserByUsername(string $username): ?UserTransfer
*/
protected function getUserTransfer(UserInterface $user): ?UserTransfer
{
if ($this->getFactory()->getAgentClient()->isLoggedIn() === false) {
return $this->findUserByUsername(
$this->getUserIdentifier($user),
);
$userTransfer = $this->findUserByUsername(
$this->getUserIdentifier($user),
);

if ($userTransfer === null) {
$this->getFactory()
->getAgentClient()
->invalidateAgentSession();
}

return $this->getFactory()
->getAgentClient()
->getAgent();
return $userTransfer;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
namespace SprykerShop\Yves\AgentPage\Plugin\Subscriber;

use Generated\Shared\Transfer\QuoteTransfer;
use Generated\Shared\Transfer\UserTransfer;
use Spryker\Yves\Kernel\AbstractPlugin;
use SprykerShop\Yves\AgentPage\Security\Agent;
use SprykerShop\Yves\CustomerPage\Security\Customer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\Security\Http\SecurityEvents;

Expand All @@ -20,6 +22,13 @@
*/
class SwitchUserEventSubscriber extends AbstractPlugin implements EventSubscriberInterface
{
/**
* @uses \Orm\Zed\User\Persistence\Map\SpyUserTableMap::COL_STATUS_ACTIVE
*
* @var string
*/
protected const COL_STATUS_ACTIVE = 'active';

/**
* @return array<string, mixed>
*/
Expand All @@ -39,6 +48,14 @@ public function switchUser(SwitchUserEvent $switchUserEvent)
{
$targetUser = $switchUserEvent->getTargetUser();

$agentUserTransfer = $this->findAgentUserByUsername($this->findAgentUsername($switchUserEvent));

if ($agentUserTransfer === null) {
$this->onImpersonationEnd();

return;
}

if ($targetUser instanceof Customer) {
$this->onImpersonationStart($targetUser);

Expand Down Expand Up @@ -82,4 +99,45 @@ protected function clearAgentsQuote(): void
->getQuoteClient()
->setQuote(new QuoteTransfer());
}

/**
* @param \Symfony\Component\Security\Http\Event\SwitchUserEvent $switchUserEvent
*
* @return string|null
*/
protected function findAgentUsername(SwitchUserEvent $switchUserEvent): ?string
{
$token = $switchUserEvent->getToken();
if (!$token instanceof SwitchUserToken) {
return null;
}

$originalUser = $token->getOriginalToken()->getUser();
if (!$originalUser instanceof Agent) {
return null;
}

return $originalUser->getUsername();
}

/**
* @param string $username
*
* @return \Generated\Shared\Transfer\UserTransfer|null
*/
protected function findAgentUserByUsername(string $username): ?UserTransfer
{
$userTransfer = new UserTransfer();
$userTransfer->setUsername($username);

$userTransfer = $this->getFactory()
->getAgentClient()
->findAgentByUsername($userTransfer);

if ($userTransfer && $userTransfer->getStatus() === static::COL_STATUS_ACTIVE) {
return $userTransfer;
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends template('page-layout-main') %}

{% define data = {
loginRedirectUrl: _view.loginRedirectUrl
} %}

{% block content %}
<div class="container">
<div class="row">
<div class="col-md-12">
<h5>{{ 'agent.account.redirect-to-login' | trans }}</h5>
<meta http-equiv="refresh" content="0; url={{ data.loginRedirectUrl }}">
</div>
</div>
</div>
{% endblock %}

0 comments on commit 580351f

Please sign in to comment.