Skip to content

Commit fd686a0

Browse files
author
Stanislav Idolov
authored
ENGCOM-1159: [Port 2.3-develop] Move isAllowed method from AccessChangeQuoteControl to separate service #13776
2 parents 0f169cb + a8b88db commit fd686a0

File tree

5 files changed

+125
-41
lines changed

5 files changed

+125
-41
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Quote\Api;
10+
11+
use Magento\Quote\Api\Data\CartInterface;
12+
13+
/**
14+
* Service checks if the user has ability to change the quote.
15+
*/
16+
interface ChangeQuoteControlInterface
17+
{
18+
/**
19+
* Checks if user is allowed to change the quote.
20+
*
21+
* @param CartInterface $quote
22+
* @return bool
23+
*/
24+
public function isAllowed(CartInterface $quote): bool;
25+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Quote\Model;
10+
11+
use Magento\Authorization\Model\UserContextInterface;
12+
use Magento\Quote\Api\ChangeQuoteControlInterface;
13+
use Magento\Quote\Api\Data\CartInterface;
14+
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
class ChangeQuoteControl implements ChangeQuoteControlInterface
19+
{
20+
/**
21+
* @var UserContextInterface $userContext
22+
*/
23+
private $userContext;
24+
25+
/**
26+
* @param UserContextInterface $userContext
27+
*/
28+
public function __construct(UserContextInterface $userContext)
29+
{
30+
$this->userContext = $userContext;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function isAllowed(CartInterface $quote): bool
37+
{
38+
switch ($this->userContext->getUserType()) {
39+
case UserContextInterface::USER_TYPE_CUSTOMER:
40+
$isAllowed = ($quote->getCustomerId() == $this->userContext->getUserId());
41+
break;
42+
case UserContextInterface::USER_TYPE_GUEST:
43+
$isAllowed = ($quote->getCustomerId() === null);
44+
break;
45+
case UserContextInterface::USER_TYPE_ADMIN:
46+
case UserContextInterface::USER_TYPE_INTEGRATION:
47+
$isAllowed = true;
48+
break;
49+
default:
50+
$isAllowed = false;
51+
}
52+
53+
return $isAllowed;
54+
}
55+
}

app/code/Magento/Quote/Model/QuoteRepository/Plugin/AccessChangeQuoteControl.php

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Quote\Model\QuoteRepository\Plugin;
78

8-
use Magento\Authorization\Model\UserContextInterface;
9-
use Magento\Quote\Model\Quote;
9+
use Magento\Quote\Api\ChangeQuoteControlInterface;
1010
use Magento\Framework\Exception\StateException;
1111
use Magento\Quote\Api\CartRepositoryInterface;
1212
use Magento\Quote\Api\Data\CartInterface;
@@ -17,59 +17,32 @@
1717
class AccessChangeQuoteControl
1818
{
1919
/**
20-
* @var UserContextInterface
20+
* @var ChangeQuoteControlInterface $changeQuoteControl
2121
*/
22-
private $userContext;
22+
private $changeQuoteControl;
2323

2424
/**
25-
* @param UserContextInterface $userContext
25+
* @param ChangeQuoteControlInterface $changeQuoteControl
2626
*/
27-
public function __construct(
28-
UserContextInterface $userContext
29-
) {
30-
$this->userContext = $userContext;
27+
public function __construct(ChangeQuoteControlInterface $changeQuoteControl)
28+
{
29+
$this->changeQuoteControl = $changeQuoteControl;
3130
}
3231

3332
/**
3433
* Checks if change quote's customer id is allowed for current user.
3534
*
3635
* @param CartRepositoryInterface $subject
37-
* @param Quote $quote
36+
* @param CartInterface $quote
3837
* @throws StateException if Guest has customer_id or Customer's customer_id not much with user_id
3938
* or unknown user's type
4039
* @return void
4140
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4241
*/
4342
public function beforeSave(CartRepositoryInterface $subject, CartInterface $quote)
4443
{
45-
if (!$this->isAllowed($quote)) {
44+
if (! $this->changeQuoteControl->isAllowed($quote)) {
4645
throw new StateException(__("Invalid state change requested"));
4746
}
4847
}
49-
50-
/**
51-
* Checks if user is allowed to change the quote.
52-
*
53-
* @param Quote $quote
54-
* @return bool
55-
*/
56-
private function isAllowed(Quote $quote)
57-
{
58-
switch ($this->userContext->getUserType()) {
59-
case UserContextInterface::USER_TYPE_CUSTOMER:
60-
$isAllowed = ($quote->getCustomerId() == $this->userContext->getUserId());
61-
break;
62-
case UserContextInterface::USER_TYPE_GUEST:
63-
$isAllowed = ($quote->getCustomerId() === null);
64-
break;
65-
case UserContextInterface::USER_TYPE_ADMIN:
66-
case UserContextInterface::USER_TYPE_INTEGRATION:
67-
$isAllowed = true;
68-
break;
69-
default:
70-
$isAllowed = false;
71-
}
72-
73-
return $isAllowed;
74-
}
7548
}

app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/Plugin/AccessChangeQuoteControlTest.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Quote\Test\Unit\Model\QuoteRepository\Plugin;
78

89
use Magento\Authorization\Model\UserContextInterface;
10+
use Magento\Quote\Model\ChangeQuoteControl;
911
use Magento\Quote\Model\QuoteRepository\Plugin\AccessChangeQuoteControl;
1012
use Magento\Quote\Model\Quote;
1113
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -34,6 +36,11 @@ class AccessChangeQuoteControlTest extends \PHPUnit\Framework\TestCase
3436
*/
3537
private $quoteRepositoryMock;
3638

39+
/**
40+
* @var ChangeQuoteControl|MockObject
41+
*/
42+
private $changeQuoteControlMock;
43+
3744
protected function setUp()
3845
{
3946
$this->userContextMock = $this->getMockBuilder(UserContextInterface::class)
@@ -50,15 +57,19 @@ protected function setUp()
5057
->disableOriginalConstructor()
5158
->getMock();
5259

60+
$this->changeQuoteControlMock = $this->getMockBuilder(ChangeQuoteControl::class)
61+
->disableOriginalConstructor()
62+
->getMock();
63+
5364
$objectManagerHelper = new ObjectManager($this);
5465
$this->accessChangeQuoteControl = $objectManagerHelper->getObject(
5566
AccessChangeQuoteControl::class,
56-
['userContext' => $this->userContextMock]
67+
['changeQuoteControl' => $this->changeQuoteControlMock]
5768
);
5869
}
5970

6071
/**
61-
* User with role Customer and customer_id much with context user_id.
72+
* User with role Customer and customer_id matches context user_id.
6273
*/
6374
public function testBeforeSaveForCustomer()
6475
{
@@ -68,6 +79,9 @@ public function testBeforeSaveForCustomer()
6879
$this->userContextMock->method('getUserType')
6980
->willReturn(UserContextInterface::USER_TYPE_CUSTOMER);
7081

82+
$this->changeQuoteControlMock->method('isAllowed')
83+
->willReturn(true);
84+
7185
$result = $this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
7286

7387
$this->assertNull($result);
@@ -81,11 +95,15 @@ public function testBeforeSaveForCustomer()
8195
*/
8296
public function testBeforeSaveException()
8397
{
84-
$this->userContextMock->method('getUserType')
85-
->willReturn(UserContextInterface::USER_TYPE_CUSTOMER);
8698
$this->quoteMock->method('getCustomerId')
8799
->willReturn(2);
88100

101+
$this->userContextMock->method('getUserType')
102+
->willReturn(UserContextInterface::USER_TYPE_CUSTOMER);
103+
104+
$this->changeQuoteControlMock->method('isAllowed')
105+
->willReturn(false);
106+
89107
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
90108
}
91109

@@ -100,6 +118,9 @@ public function testBeforeSaveForAdmin()
100118
$this->userContextMock->method('getUserType')
101119
->willReturn(UserContextInterface::USER_TYPE_ADMIN);
102120

121+
$this->changeQuoteControlMock->method('isAllowed')
122+
->willReturn(true);
123+
103124
$result = $this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
104125

105126
$this->assertNull($result);
@@ -116,6 +137,9 @@ public function testBeforeSaveForGuest()
116137
$this->userContextMock->method('getUserType')
117138
->willReturn(UserContextInterface::USER_TYPE_GUEST);
118139

140+
$this->changeQuoteControlMock->method('isAllowed')
141+
->willReturn(true);
142+
119143
$result = $this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
120144

121145
$this->assertNull($result);
@@ -135,6 +159,9 @@ public function testBeforeSaveForGuestException()
135159
$this->userContextMock->method('getUserType')
136160
->willReturn(UserContextInterface::USER_TYPE_GUEST);
137161

162+
$this->changeQuoteControlMock->method('isAllowed')
163+
->willReturn(false);
164+
138165
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
139166
}
140167

@@ -152,6 +179,9 @@ public function testBeforeSaveForUnknownUserTypeException()
152179
$this->userContextMock->method('getUserType')
153180
->willReturn(10);
154181

182+
$this->changeQuoteControlMock->method('isAllowed')
183+
->willReturn(false);
184+
155185
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
156186
}
157187
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<preference for="Magento\Quote\Api\CouponManagementInterface" type="Magento\Quote\Model\CouponManagement" />
2323
<preference for="Magento\Quote\Api\CartManagementInterface" type="Magento\Quote\Model\QuoteManagement" />
2424
<preference for="Magento\Quote\Api\CartTotalRepositoryInterface" type="Magento\Quote\Model\Cart\CartTotalRepository" />
25+
<preference for="Magento\Quote\Api\ChangeQuoteControlInterface" type="Magento\Quote\Model\ChangeQuoteControl" />
2526
<preference for="Magento\Quote\Api\CartTotalManagementInterface" type="Magento\Quote\Model\Cart\CartTotalManagement" />
2627
<preference for="Magento\Quote\Api\Data\TotalsInterface" type="Magento\Quote\Model\Cart\Totals" />
2728
<preference for="Magento\Quote\Api\Data\TotalSegmentInterface" type="Magento\Quote\Model\Cart\TotalSegment" />

0 commit comments

Comments
 (0)