Skip to content

Commit a7ccb3a

Browse files
authored
Merge branch '2.4-develop' into CABPI-479
2 parents d592c3d + fa3d6ec commit a7ccb3a

File tree

166 files changed

+5403
-388
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+5403
-388
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\Api;
9+
10+
use Magento\Framework\Exception\CouldNotSaveException;
11+
12+
/**
13+
* Interface SaveImsUserInterface
14+
* Save Ims User & Role
15+
*/
16+
interface SaveImsUserInterface
17+
{
18+
/**
19+
* Add Admin Adobe IMS User with Default Role i.e "Adobe Ims" & No Permissions
20+
*
21+
* @param array $profile
22+
* @return void
23+
* @throws CouldNotSaveException
24+
*/
25+
public function save(array $profile): void;
26+
}

app/code/Magento/AdminAdobeIms/Model/Authorization/AdobeImsAdminTokenUserService.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\AdminAdobeIms\Model\Authorization;
99

10+
use Magento\AdminAdobeIms\Api\SaveImsUserInterface;
1011
use Magento\AdminAdobeIms\Exception\AdobeImsAuthorizationException;
1112
use Magento\AdminAdobeIms\Service\AdminLoginProcessService;
1213
use Magento\AdminAdobeIms\Service\AdminReauthProcessService;
@@ -71,6 +72,11 @@ class AdobeImsAdminTokenUserService
7172
*/
7273
private TokenResponseInterfaceFactory $tokenResponseFactory;
7374

75+
/**
76+
* @var SaveImsUserInterface
77+
*/
78+
private SaveImsUserInterface $saveImsUser;
79+
7480
/**
7581
* @param ImsConfig $adminImsConfig
7682
* @param OrganizationMembershipInterface $organizationMembership
@@ -80,6 +86,7 @@ class AdobeImsAdminTokenUserService
8086
* @param GetTokenInterface $token
8187
* @param GetProfileInterface $profile
8288
* @param TokenResponseInterfaceFactory $tokenResponseFactory
89+
* @param SaveImsUserInterface $saveImsUser
8390
*/
8491
public function __construct(
8592
ImsConfig $adminImsConfig,
@@ -89,7 +96,8 @@ public function __construct(
8996
RequestInterface $request,
9097
GetTokenInterface $token,
9198
GetProfileInterface $profile,
92-
TokenResponseInterfaceFactory $tokenResponseFactory
99+
TokenResponseInterfaceFactory $tokenResponseFactory,
100+
SaveImsUserInterface $saveImsUser
93101
) {
94102
$this->adminImsConfig = $adminImsConfig;
95103
$this->organizationMembership = $organizationMembership;
@@ -99,6 +107,7 @@ public function __construct(
99107
$this->token = $token;
100108
$this->profile = $profile;
101109
$this->tokenResponseFactory = $tokenResponseFactory;
110+
$this->saveImsUser = $saveImsUser;
102111
}
103112

104113
/**
@@ -108,7 +117,8 @@ public function __construct(
108117
* @return void
109118
* @throws AdobeImsAuthorizationException
110119
* @throws AdobeImsOrganizationAuthorizationException
111-
* @throws AuthenticationException|AuthorizationException
120+
* @throws AuthenticationException
121+
* @throws AuthorizationException
112122
*/
113123
public function processLoginRequest(bool $isReauthorize = false): void
114124
{
@@ -188,6 +198,7 @@ private function getLoggedIn(bool $isReauthorize, TokenResponseInterface $tokenR
188198
if ($isReauthorize) {
189199
$this->adminReauthProcessService->execute($tokenResponse);
190200
} else {
201+
$this->saveImsUser->save($profile);
191202
$this->adminLoginProcessService->execute($tokenResponse, $profile);
192203
}
193204
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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\Api\SaveImsUserInterface;
11+
use Magento\User\Model\User;
12+
use Magento\User\Model\ResourceModel\User\CollectionFactory as UserCollectionFactory;
13+
use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory as RoleCollectionFactory;
14+
use Magento\AdminAdobeIms\Logger\AdminAdobeImsLogger;
15+
use Magento\AdminAdobeIms\Service\ImsConfig;
16+
use Magento\Authorization\Model\Acl\Role\User as UserRoleType;
17+
use Exception;
18+
use Magento\Framework\Exception\CouldNotSaveException;
19+
20+
/**
21+
* Class SaveImsUser
22+
* Save Adobe IMS User with Default Role i.e "Adobe Ims" & No Permissions
23+
*/
24+
class SaveImsUser implements SaveImsUserInterface
25+
{
26+
private const ADMIN_IMS_ROLE = 'Adobe Ims';
27+
28+
/**
29+
* @var User
30+
*/
31+
private User $user;
32+
33+
/**
34+
* @var UserCollectionFactory
35+
*/
36+
private UserCollectionFactory $userCollectionFactory;
37+
38+
/**
39+
* @var RoleCollectionFactory
40+
*/
41+
private RoleCollectionFactory $roleCollectionFactory;
42+
43+
/**
44+
* @var AdminAdobeImsLogger
45+
*/
46+
private AdminAdobeImsLogger $logger;
47+
48+
/**
49+
* @var ImsConfig
50+
*/
51+
private ImsConfig $adminImsConfig;
52+
53+
/**
54+
* SaveImsUser constructor.
55+
* @param User $user
56+
* @param UserCollectionFactory $userCollectionFactory
57+
* @param RoleCollectionFactory $roleCollectionFactory
58+
* @param AdminAdobeImsLogger $logger
59+
* @param ImsConfig $adminImsConfig
60+
*/
61+
public function __construct(
62+
User $user,
63+
UserCollectionFactory $userCollectionFactory,
64+
RoleCollectionFactory $roleCollectionFactory,
65+
AdminAdobeImsLogger $logger,
66+
ImsConfig $adminImsConfig
67+
) {
68+
$this->user = $user;
69+
$this->userCollectionFactory = $userCollectionFactory;
70+
$this->roleCollectionFactory = $roleCollectionFactory;
71+
$this->logger = $logger;
72+
$this->adminImsConfig = $adminImsConfig;
73+
}
74+
75+
/**
76+
* @inheritdoc
77+
*/
78+
public function save(array $profile): void
79+
{
80+
if (!$this->adminImsConfig->enabled() || empty($profile['email'])) {
81+
throw new CouldNotSaveException(__('Could not save ims user.'));
82+
}
83+
84+
$username = strtolower(strstr($profile['email'], '@', true));
85+
$userCollection = $this->userCollectionFactory->create()
86+
->addFieldToFilter('email', ['eq' => $profile['email']])
87+
->addFieldToFilter('username', ['eq' => $username]);
88+
89+
if (!$userCollection->getSize()) {
90+
$roleId = $this->getImsDefaultRole();
91+
if ($roleId > 0) {
92+
try {
93+
$this->user->setFirstname($profile['first_name'])
94+
->setLastname($profile['last_name'])
95+
->setUsername($username)
96+
->setPassword($this->generateRandomPassword())
97+
->setEmail($profile['email'])
98+
->setRoleType(UserRoleType::ROLE_TYPE)
99+
->setPrivileges("")
100+
->setAssertId(0)
101+
->setRoleId((int)$roleId)
102+
->setPermission('allow')
103+
->save();
104+
unset($this->user);
105+
} catch (Exception $e) {
106+
$this->logger->critical($e->getMessage());
107+
throw new CouldNotSaveException(__('Could not save ims user.'));
108+
}
109+
}
110+
}
111+
$userCollection->clear();
112+
}
113+
114+
/**
115+
* Fetch Default Role "Adobe Ims"
116+
*
117+
* @return int
118+
*/
119+
private function getImsDefaultRole(): int
120+
{
121+
$roleId = 0;
122+
$roleCollection = $this->roleCollectionFactory->create()
123+
->addFieldToFilter('role_name', ['eq' => self::ADMIN_IMS_ROLE])
124+
->addFieldToSelect('role_id');
125+
126+
if ($roleCollection->getSize() > 0) {
127+
$objRole = $roleCollection->fetchItem();
128+
$roleId = (int) $objRole->getId();
129+
}
130+
$roleCollection->clear();
131+
132+
return $roleId;
133+
}
134+
135+
/**
136+
* Generate random password string
137+
*
138+
* @return string
139+
*/
140+
private function generateRandomPassword(): string
141+
{
142+
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.';
143+
$pass = [];
144+
$alphaLength = strlen($characters) - 1;
145+
for ($i = 0; $i < 100; $i++) {
146+
$n = random_int(0, $alphaLength);
147+
$pass[] = $characters[$n];
148+
}
149+
return implode($pass);
150+
}
151+
}

app/code/Magento/AdminAdobeIms/Test/Unit/Model/Authorization/AdobeImsAdminTokenUserServiceTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\AdminAdobeIms\Test\Unit\Model\Authorization;
99

10+
use Magento\AdminAdobeIms\Api\SaveImsUserInterface;
1011
use Magento\AdminAdobeIms\Exception\AdobeImsAuthorizationException;
1112
use Magento\AdminAdobeIms\Model\Authorization\AdobeImsAdminTokenUserService;
1213
use Magento\AdminAdobeIms\Service\AdminLoginProcessService;
@@ -23,6 +24,8 @@
2324

2425
/**
2526
* Tests Magento\AdminAdobeIms\Model\Authorization\AdobeImsAdminTokenUserService
27+
*
28+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2629
*/
2730
class AdobeImsAdminTokenUserServiceTest extends TestCase
2831
{
@@ -73,6 +76,11 @@ class AdobeImsAdminTokenUserServiceTest extends TestCase
7376
*/
7477
private $tokenResponseFactoryMock;
7578

79+
/**
80+
* @var SaveImsUserInterface
81+
*/
82+
private $saveImsUser;
83+
7684
protected function setUp(): void
7785
{
7886
$this->adminImsConfigMock = $this->createMock(ImsConfig::class);
@@ -86,7 +94,7 @@ protected function setUp(): void
8694
->getMockForAbstractClass();
8795
$this->adminReauthProcessService = $this->createMock(AdminReauthProcessService::class);
8896
$this->tokenResponseFactoryMock = $this->createMock(TokenResponseInterfaceFactory::class);
89-
97+
$this->saveImsUser = $this->createMock(SaveImsUserInterface::class);
9098
$this->adminImsConfigMock->expects($this->any())
9199
->method('enabled')
92100
->willReturn(true);
@@ -99,7 +107,8 @@ protected function setUp(): void
99107
$this->requestInterfaceMock,
100108
$this->token,
101109
$this->profile,
102-
$this->tokenResponseFactoryMock
110+
$this->tokenResponseFactoryMock,
111+
$this->saveImsUser
103112
);
104113
}
105114

@@ -137,6 +146,10 @@ public function testProcessLoginRequest(array $responseData): void
137146
->method('checkOrganizationMembership')
138147
->with($responseData['access_token']);
139148

149+
$this->saveImsUser->expects($this->once())
150+
->method('save')
151+
->with($responseData);
152+
140153
$this->adminLoginProcessService->expects($this->once())
141154
->method('execute')
142155
->with($tokenResponse, $responseData);
@@ -182,6 +195,10 @@ public function testProcessLoginRequestWithAuthorizationHeader(array $responseDa
182195
->method('checkOrganizationMembership')
183196
->with($responseData['access_token']);
184197

198+
$this->saveImsUser->expects($this->once())
199+
->method('save')
200+
->with($responseData);
201+
185202
$this->adminLoginProcessService->expects($this->once())
186203
->method('execute')
187204
->with($tokenResponse, $responseData);
@@ -302,7 +319,9 @@ public function responseDataProvider(): array
302319
'email' => '[email protected]',
303320
'access_token' => 'kladjflakdjf3423rfzddsf',
304321
'refresh_token' => 'kladjflakdjf3423rfzddsf',
305-
'expires_in' => 1642259230998
322+
'expires_in' => 1642259230998,
323+
'first_name' => 'Test',
324+
'last_name' => 'User'
306325
]
307326
]
308327
];

app/code/Magento/AdminAdobeIms/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<preference for="Magento\AdminAdobeIms\Api\Data\ImsWebapiInterface" type="Magento\AdminAdobeIms\Model\ImsWebapi"/>
1212
<preference for="Magento\AdobeImsApi\Api\GetAccessTokenInterface" type="Magento\AdminAdobeIms\Model\GetAccessTokenProxy"/>
1313
<preference for="Magento\AdobeImsApi\Api\UserAuthorizedInterface" type="Magento\AdminAdobeIms\Model\UserAuthorizedProxy"/>
14+
<preference for="Magento\AdminAdobeIms\Api\SaveImsUserInterface" type="Magento\AdminAdobeIms\Model\SaveImsUser"/>
1415

1516
<type name="Magento\Framework\Console\CommandListInterface">
1617
<arguments>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"Admin Adobe IMS integration is disabled","Admin Adobe IMS integration is disabled"
2+
"Admin Adobe IMS integration is enabled","Admin Adobe IMS integration is enabled"
3+
"The Client ID, Client Secret, Organization ID and 2FA are required when enabling the Admin Adobe IMS Module","The Client ID, Client Secret, Organization ID and 2FA are required when enabling the Admin Adobe IMS Module"
4+
"Module is disabled","Module is disabled"
5+
"Admin Adobe IMS integration is %1","Admin Adobe IMS integration is %1"
6+
"Adobe Sign-In is disabled.","Adobe Sign-In is disabled."
7+
"Authorization was successful","Authorization was successful"
8+
"Session Access Token is not valid","Session Access Token is not valid"
9+
"Login request error %1","Login request error %1"
10+
"An authentication error occurred. Verify and try again.","An authentication error occurred. Verify and try again."
11+
"You don't have access to this Commerce instance","You don't have access to this Commerce instance"
12+
"Unable to sign in with the Adobe ID","Unable to sign in with the Adobe ID"
13+
"Could not save ims token.","Could not save ims token."
14+
"Could not find ims token id: %id.","Could not find ims token id: %id."
15+
"Could not delete ims tokens for admin user id %1.","Could not delete ims tokens for admin user id %1."
16+
"Could not save ims user.","Could not save ims user."
17+
"The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later.","The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later."
18+
"More permissions are needed to access this.","More permissions are needed to access this."
19+
"Please sign in with Adobe ID","Please sign in with Adobe ID"
20+
"Admin token generation is disabled. Please use Adobe IMS ACCESS_TOKEN.","Admin token generation is disabled. Please use Adobe IMS ACCESS_TOKEN."
21+
"Identity Verification","Identity Verification"
22+
"Verify Identity with Adobe IMS","Verify Identity with Adobe IMS"
23+
"Confirm Identity","Confirm Identity"
24+
"To apply changes you need to verify your Adobe identity.","To apply changes you need to verify your Adobe identity."
25+
"Identity Verified with Adobe IMS","Identity Verified with Adobe IMS"
26+
"Please perform the AdobeIms reAuth and try again.","Please perform the AdobeIms reAuth and try again."
27+
"Use the same email user has in Adobe IMS organization.","Use the same email user has in Adobe IMS organization."
28+
"The tokens couldn't be revoked.","The tokens couldn't be revoked."
29+
"No matching admin user found for Adobe ID.","No matching admin user found for Adobe ID."
30+
"This field is required to enable the Admin Adobe IMS Module","This field is required to enable the Admin Adobe IMS Module"
31+
"No valid Organization ID provided","No valid Organization ID provided"
32+
"No valid Client ID provided","No valid Client ID provided"
33+
"No valid Client Secret provided","No valid Client Secret provided"
34+
"The ims token wasn't found.","The ims token wasn't found."
35+
"Sign in to access the Adobe Commerce for your organization.","Sign in to access the Adobe Commerce for your organization."
36+
"Sign In","Sign In"
37+
"This Commerce instance is managed by an organization. Contact your organization administrator to request access.","This Commerce instance is managed by an organization. Contact your organization administrator to request access."
38+
"Sign in with Adobe ID","Sign in with Adobe ID"
39+
Footer,Footer
40+
"User Guides","User Guides"
41+
"Customer Support","Customer Support"
42+
Forums,Forums
43+
Header,Header
44+
"%user_name, you now have access to Adobe Commerce","%user_name, you now have access to Adobe Commerce"
45+
"Your administrator at %store_name has given you access to Adobe Commerce","Your administrator at %store_name has given you access to Adobe Commerce"
46+
"Get started","Get started"
47+
"Here are a few links to help you get up and running:","Here are a few links to help you get up and running:"
48+
Documentation,Documentation
49+
"Release notes","Release notes"
50+
"If you have any questions about access to Adobe Commerce, contact your administrator or your Adobe account team for more information.","If you have any questions about access to Adobe Commerce, contact your administrator or your Adobe account team for more information."
51+
"Enable Logging for Admin Adobe IMS Module","Enable Logging for Admin Adobe IMS Module"
52+
"Adobe Commerce","Adobe Commerce"

0 commit comments

Comments
 (0)