Skip to content

Commit 54e1a52

Browse files
authored
Merge pull request #442 from magento-performance/CABPI-364
CABPI-364: AdobeStock UI changes
2 parents e37b3c1 + 3a49152 commit 54e1a52

File tree

13 files changed

+629
-13
lines changed

13 files changed

+629
-13
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\AdminAdobeIms\Model;
9+
10+
use Magento\AdminAdobeIms\Service\ImsConfig;
11+
use Magento\AdobeIms\Model\GetAccessToken;
12+
use Magento\AdobeImsApi\Api\GetAccessTokenInterface;
13+
14+
/**
15+
* Represent the get user access token from session or from db functionality
16+
*/
17+
class GetAccessTokenProxy implements GetAccessTokenInterface
18+
{
19+
/**
20+
* @var GetAccessToken
21+
*/
22+
private GetAccessToken $getAccessTokenFromDb;
23+
24+
/**
25+
* @var GetAccessTokenSession
26+
*/
27+
private GetAccessTokenSession $getAccessTokenFromSession;
28+
29+
/**
30+
* @var ImsConfig
31+
*/
32+
private ImsConfig $adminAdobeImsConfig;
33+
34+
/**
35+
* @param GetAccessToken $getAccessTokenFromDb
36+
* @param GetAccessTokenSession $getAccessTokenFromSession
37+
* @param ImsConfig $adminAdobeImsConfig
38+
*/
39+
public function __construct(
40+
GetAccessToken $getAccessTokenFromDb,
41+
GetAccessTokenSession $getAccessTokenFromSession,
42+
ImsConfig $adminAdobeImsConfig
43+
) {
44+
$this->getAccessTokenFromDb = $getAccessTokenFromDb;
45+
$this->getAccessTokenFromSession = $getAccessTokenFromSession;
46+
$this->adminAdobeImsConfig = $adminAdobeImsConfig;
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function execute(int $adminUserId = null): ?string
53+
{
54+
if ($this->adminAdobeImsConfig->enabled()) {
55+
return $this->getAccessTokenFromSession->execute($adminUserId);
56+
}
57+
58+
return $this->getAccessTokenFromDb->execute($adminUserId);
59+
}
60+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\AdminAdobeIms\Model;
9+
10+
use Magento\AdobeImsApi\Api\GetAccessTokenInterface;
11+
12+
/**
13+
* Represents get user access token from session functionality
14+
*/
15+
class GetAccessTokenSession implements GetAccessTokenInterface
16+
{
17+
/**
18+
* @var Auth
19+
*/
20+
private Auth $auth;
21+
22+
/**
23+
* @param Auth $auth
24+
*/
25+
public function __construct(Auth $auth)
26+
{
27+
$this->auth = $auth;
28+
}
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function execute(int $adminUserId = null): ?string
34+
{
35+
return $this->auth->getAuthStorage()->getAdobeAccessToken() ?? null;
36+
}
37+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AdminAdobeIms\Model;
10+
11+
use Magento\AdminAdobeIms\Service\ImsConfig;
12+
use Magento\AdobeIms\Model\UserAuthorized;
13+
use Magento\AdobeImsApi\Api\UserAuthorizedInterface;
14+
15+
/**
16+
* Represents functionality for getting information from session or from db about if user is authorised or not
17+
*/
18+
class UserAuthorizedProxy implements UserAuthorizedInterface
19+
{
20+
/**
21+
* @var UserAuthorized
22+
*/
23+
private UserAuthorized $userAuthorizedDb;
24+
25+
/**
26+
* @var UserAuthorizedSession
27+
*/
28+
private UserAuthorizedSession $userAuthorizedSession;
29+
30+
/**
31+
* @var ImsConfig
32+
*/
33+
private ImsConfig $adminAdobeImsConfig;
34+
35+
/**
36+
* @param UserAuthorized $userAuthorizedDb
37+
* @param UserAuthorizedSession $userAuthorizedSession
38+
* @param ImsConfig $adminAdobeImsConfig
39+
*/
40+
public function __construct(
41+
UserAuthorized $userAuthorizedDb,
42+
UserAuthorizedSession $userAuthorizedSession,
43+
ImsConfig $adminAdobeImsConfig
44+
) {
45+
$this->userAuthorizedDb = $userAuthorizedDb;
46+
$this->userAuthorizedSession = $userAuthorizedSession;
47+
$this->adminAdobeImsConfig = $adminAdobeImsConfig;
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
public function execute(int $adminUserId = null): bool
54+
{
55+
if ($this->adminAdobeImsConfig->enabled()) {
56+
return $this->userAuthorizedSession->execute($adminUserId);
57+
}
58+
59+
return $this->userAuthorizedDb->execute($adminUserId);
60+
}
61+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AdminAdobeIms\Model;
10+
11+
use Magento\AdobeImsApi\Api\UserAuthorizedInterface;
12+
use Magento\Framework\Exception\AuthorizationException;
13+
14+
/**
15+
* Represent functionality for getting information from session if user is authorised or not
16+
*/
17+
class UserAuthorizedSession implements UserAuthorizedInterface
18+
{
19+
/**
20+
* @var Auth
21+
*/
22+
private Auth $auth;
23+
24+
/**
25+
* @var ImsConnection
26+
*/
27+
private ImsConnection $imsConnection;
28+
29+
/**
30+
* @param Auth $auth
31+
* @param ImsConnection $imsConnection
32+
*/
33+
public function __construct(
34+
Auth $auth,
35+
ImsConnection $imsConnection
36+
) {
37+
$this->auth = $auth;
38+
$this->imsConnection = $imsConnection;
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
public function execute(int $adminUserId = null): bool
45+
{
46+
$token = $this->auth->getAuthStorage()->getAdobeAccessToken();
47+
48+
if (empty($token) || empty($this->auth->getUser()->getId())) {
49+
return false;
50+
}
51+
52+
try {
53+
return $this->imsConnection->validateToken($token);
54+
} catch (AuthorizationException $e) {
55+
return false;
56+
}
57+
}
58+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AdminAdobeIms\Plugin\Block\Adminhtml;
10+
11+
use Magento\AdminAdobeIms\Model\Auth;
12+
use Magento\AdobeIms\Block\Adminhtml\SignIn;
13+
use Magento\AdminAdobeIms\Service\ImsConfig;
14+
use Magento\AdobeImsApi\Api\ConfigInterface;
15+
use Magento\AdobeImsApi\Api\ConfigProviderInterface;
16+
use Magento\AdobeImsApi\Api\UserAuthorizedInterface;
17+
use Magento\Framework\Serialize\Serializer\JsonHexTag;
18+
19+
/**
20+
* Plugin to get authentication component configuration if Admin Adobe IMS is enabled
21+
*/
22+
class SignInPlugin
23+
{
24+
/**
25+
* @var ImsConfig
26+
*/
27+
private ImsConfig $adminAdobeImsConfig;
28+
29+
/**
30+
* @var Auth
31+
*/
32+
private Auth $auth;
33+
34+
/**
35+
* @var UserAuthorizedInterface
36+
*/
37+
private UserAuthorizedInterface $userAuthorized;
38+
39+
/**
40+
* JsonHexTag Serializer Instance
41+
*
42+
* @var JsonHexTag
43+
*/
44+
private JsonHexTag $serializer;
45+
46+
/**
47+
* @var ConfigInterface
48+
*/
49+
private ConfigInterface $config;
50+
51+
/**
52+
* @param ImsConfig $adminAdobeImsConfig
53+
* @param Auth $auth
54+
* @param UserAuthorizedInterface $userAuthorized
55+
* @param JsonHexTag $serializer
56+
* @param ConfigInterface $config
57+
*/
58+
public function __construct(
59+
ImsConfig $adminAdobeImsConfig,
60+
Auth $auth,
61+
UserAuthorizedInterface $userAuthorized,
62+
JsonHexTag $serializer,
63+
ConfigInterface $config
64+
) {
65+
$this->adminAdobeImsConfig = $adminAdobeImsConfig;
66+
$this->auth = $auth;
67+
$this->userAuthorized = $userAuthorized;
68+
$this->serializer = $serializer;
69+
$this->config = $config;
70+
}
71+
72+
/**
73+
* Get authentication component configuration if Admin Adobe IMS is enabled
74+
*
75+
* @param SignIn $subject
76+
* @param callable $proceed
77+
* @return string
78+
*/
79+
public function aroundGetComponentJsonConfig(SignIn $subject, callable $proceed): string
80+
{
81+
if (!$this->adminAdobeImsConfig->enabled()) {
82+
return $proceed();
83+
}
84+
85+
return $this->serializer->serialize(
86+
array_replace_recursive(
87+
$this->getDefaultComponentConfig($subject),
88+
...$this->getExtendedComponentConfig($subject)
89+
)
90+
);
91+
}
92+
93+
/**
94+
* Get default UI component configuration
95+
*
96+
* @param SignIn $subject
97+
* @return array
98+
*/
99+
private function getDefaultComponentConfig(SignIn $subject): array
100+
{
101+
return [
102+
'component' => SignIn::ADOBE_IMS_JS_SIGNIN,
103+
'template' => SignIn::ADOBE_IMS_SIGNIN,
104+
'profileUrl' => $subject->getUrl(SignIn::ADOBE_IMS_USER_PROFILE),
105+
'logoutUrl' => $subject->getUrl(SignIn::ADOBE_IMS_USER_LOGOUT),
106+
'user' => $this->getUserData(),
107+
'isGlobalSignInEnabled' => true,
108+
'loginConfig' => [
109+
'url' => $this->config->getAuthUrl(),
110+
'callbackParsingParams' => [
111+
'regexpPattern' => SignIn::RESPONSE_REGEXP_PATTERN,
112+
'codeIndex' => SignIn::RESPONSE_CODE_INDEX,
113+
'messageIndex' => SignIn::RESPONSE_MESSAGE_INDEX,
114+
'successCode' => SignIn::RESPONSE_SUCCESS_CODE,
115+
'errorCode' => SignIn::RESPONSE_ERROR_CODE
116+
]
117+
]
118+
];
119+
}
120+
121+
/**
122+
* Get UI component configuration extension specified in layout configuration for block instance
123+
*
124+
* @param SignIn $subject
125+
* @return array
126+
*/
127+
private function getExtendedComponentConfig(SignIn $subject): array
128+
{
129+
$configProviders = $subject->getData(SignIn::DATA_ARGUMENT_KEY_CONFIG_PROVIDERS);
130+
if (empty($configProviders)) {
131+
return [];
132+
}
133+
134+
$configExtensions = [];
135+
foreach ($configProviders as $configProvider) {
136+
if ($configProvider instanceof ConfigProviderInterface) {
137+
$configExtensions[] = $configProvider->get();
138+
}
139+
}
140+
return $configExtensions;
141+
}
142+
143+
/**
144+
* Get user profile information
145+
*
146+
* @return array
147+
*/
148+
private function getUserData(): array
149+
{
150+
if (!$this->userAuthorized->execute()) {
151+
return $this->getDefaultUserData();
152+
}
153+
154+
$user = $this->auth->getUser();
155+
156+
return [
157+
'isAuthorized' => true,
158+
'name' => $user->getFirstName() . ' ' . $user->getLastName(),
159+
'email' => $user->getEmail(),
160+
'image' => ''
161+
];
162+
}
163+
164+
/**
165+
* Get default user data for not authenticated or missing user profile
166+
*
167+
* @return array
168+
*/
169+
private function getDefaultUserData(): array
170+
{
171+
return [
172+
'isAuthorized' => false,
173+
'name' => '',
174+
'email' => '',
175+
'image' => '',
176+
];
177+
}
178+
}

0 commit comments

Comments
 (0)