Skip to content

Commit ab44fbc

Browse files
committed
[Feature] Add WebAPI support
1 parent dd56d22 commit ab44fbc

File tree

10 files changed

+342
-104
lines changed

10 files changed

+342
-104
lines changed

Helper/Data.php

-44
This file was deleted.

Helper/System.php

+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?php
2+
/**
3+
* Copyright © Wubinworks. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Wubinworks\DisableChangeEmail\Helper;
9+
10+
use Magento\Framework\App\Area;
11+
use Magento\Framework\App\State as AppState;
12+
use Magento\Authorization\Model\UserContextInterface;
13+
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
16+
use Magento\Framework\Phrase;
17+
18+
/**
19+
* System helper
20+
*/
21+
class System extends \Magento\Framework\App\Helper\AbstractHelper
22+
{
23+
public const XML_PATH_CUSTOMER_ACCOUNT_INFORMATION_DISABLE_CHANGE_EMAIL
24+
= 'customer/account_information/disable_change_email';
25+
26+
/**
27+
* @var AppState
28+
*/
29+
protected $appState;
30+
31+
/**
32+
* @var UserContextInterface
33+
*/
34+
protected $userContext;
35+
36+
/**
37+
* @var MessageManagerInterface
38+
*/
39+
protected $messageManager;
40+
41+
/**
42+
* Application Event Dispatcher
43+
*
44+
* @var EventManagerInterface
45+
*/
46+
protected $eventManager;
47+
48+
/**
49+
* Constructor
50+
*
51+
* @param AppState $appState
52+
* @param UserContextInterface $userContext
53+
* @param MessageManagerInterface $messageManager
54+
* @param EventManagerInterface $eventManager
55+
* @param \Magento\Framework\App\Helper\Context $context
56+
*/
57+
public function __construct(
58+
AppState $appState,
59+
UserContextInterface $userContext,
60+
MessageManagerInterface $messageManager,
61+
EventManagerInterface $eventManager,
62+
\Magento\Framework\App\Helper\Context $context
63+
) {
64+
parent::__construct($context);
65+
$this->appState = $appState;
66+
$this->userContext= $userContext;
67+
$this->messageManager = $messageManager;
68+
$this->eventManager = $eventManager;
69+
}
70+
71+
/**
72+
* Get current store system configuration value
73+
*
74+
* @param string $path
75+
* @param string $scopeType
76+
* @param null|int|string $scopeCode
77+
* @return mixed
78+
*/
79+
public function getConfig($path, $scopeType = \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $scopeCode = null)
80+
{
81+
return $this->scopeConfig->getValue(
82+
$path,
83+
$scopeType,
84+
$scopeCode
85+
);
86+
}
87+
88+
/**
89+
* Get current area code.
90+
*
91+
* @return string
92+
*/
93+
public function getArea(): string
94+
{
95+
try {
96+
$areaCode = $this->appState->getAreaCode();
97+
} catch (LocalizedException $e) {
98+
$areaCode = 'unknown';
99+
}
100+
101+
return $areaCode;
102+
}
103+
104+
/**
105+
* Is admin or integration context
106+
*
107+
* @return bool
108+
*/
109+
public function isAdminOrIntegration(): bool
110+
{
111+
return in_array(
112+
$this->userContext->getUserType(),
113+
[UserContextInterface::USER_TYPE_INTEGRATION, UserContextInterface::USER_TYPE_ADMIN]
114+
);
115+
}
116+
117+
/**
118+
* Is in webapi area
119+
*
120+
* @return bool
121+
*/
122+
public function isWebapi(): bool
123+
{
124+
return in_array(
125+
$this->getArea(),
126+
[Area::AREA_WEBAPI_REST, Area::AREA_WEBAPI_SOAP, Area::AREA_GRAPHQL]
127+
);
128+
}
129+
130+
/**
131+
* Get current full action name
132+
*
133+
* @param string $delimiter
134+
* @return string|null
135+
*/
136+
public function getFullActionName(string $delimiter = '/')
137+
{
138+
if ($this->getArea() === Area::AREA_FRONTEND
139+
|| $this->getArea() === Area::AREA_ADMINHTML) {
140+
return $this->_request->getFullActionName($delimiter);
141+
}
142+
143+
return null;
144+
}
145+
146+
/**
147+
* Get message manager
148+
*
149+
* @return MessageManagerInterface
150+
*/
151+
public function getMessageManager(): MessageManagerInterface
152+
{
153+
return $this->messageManager;
154+
}
155+
156+
/**
157+
* Get user context object
158+
*
159+
* @return UserContextInterface
160+
*/
161+
public function getUserContextObject(): UserContextInterface
162+
{
163+
return $this->userContext;
164+
}
165+
166+
/**
167+
* Get event manager
168+
*
169+
* @return EventManagerInterface
170+
*/
171+
public function getEventManager(): EventManagerInterface
172+
{
173+
return $this->eventManager;
174+
}
175+
176+
/**
177+
* Build url
178+
*
179+
* @param string $route
180+
* @param array $params
181+
* @return string
182+
*/
183+
public function getUrl(string $route, $params = [])
184+
{
185+
return $this->_getUrl($route, $params);
186+
}
187+
188+
/**
189+
* Is change email disabled
190+
*
191+
* @return bool
192+
*/
193+
public function isChangeEmailDisabled(): bool
194+
{
195+
return (bool)$this->getConfig(self::XML_PATH_CUSTOMER_ACCOUNT_INFORMATION_DISABLE_CHANGE_EMAIL);
196+
}
197+
198+
/**
199+
* Get change email ertor phrase
200+
*
201+
* @return Phrase
202+
*/
203+
public function getChangeEmailErrorPhrase(): Phrase
204+
{
205+
return new Phrase('You cannot change email address.');
206+
}
207+
}

Observer/EditPostObserver.php

+19-43
Original file line numberDiff line numberDiff line change
@@ -10,93 +10,69 @@
1010
use Magento\Framework\App\RequestInterface;
1111
use Magento\Framework\App\ResponseInterface;
1212
use Magento\Framework\App\ActionFlag;
13-
use Magento\Framework\UrlInterface;
14-
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
15-
use Wubinworks\DisableChangeEmail\Helper\Data as Helper;
13+
use Wubinworks\DisableChangeEmail\Helper\System as SystemHelper;
1614

1715
/**
1816
* Prevent customer from changing account email address
1917
*/
2018
class EditPostObserver implements \Magento\Framework\Event\ObserverInterface
2119
{
22-
/**
23-
* @var RequestInterface
24-
*/
25-
private $request;
26-
2720
/**
2821
* @var ResponseInterface
2922
*/
30-
private $response;
23+
protected $response;
3124

3225
/**
3326
* @var ActionFlag
3427
*/
35-
private $actionFlag;
36-
37-
/**
38-
* @var UrlInterface
39-
*/
40-
private $urlBuilder;
28+
protected $actionFlag;
4129

4230
/**
43-
* @var MessageManagerInterface
31+
* @var SystemHelper
4432
*/
45-
private $messageManager;
46-
47-
/**
48-
* @var Helper
49-
*/
50-
private $helper;
33+
protected $systemHelper;
5134

5235
/**
5336
* Constructor
5437
*
55-
* @param RequestInterface $request
5638
* @param ResponseInterface $response
5739
* @param ActionFlag $actionFlag
58-
* @param UrlInterface $urlBuilder
59-
* @param MessageManagerInterface $messageManager
60-
* @param Helper $helper
40+
* @param SystemHelper $systemHelper
6141
*/
6242
public function __construct(
63-
RequestInterface $request,
6443
ResponseInterface $response,
6544
ActionFlag $actionFlag,
66-
UrlInterface $urlBuilder,
67-
MessageManagerInterface $messageManager,
68-
Helper $helper
45+
SystemHelper $systemHelper
6946
) {
70-
$this->request = $request;
7147
$this->response = $response;
7248
$this->actionFlag = $actionFlag;
73-
$this->urlBuilder = $urlBuilder;
74-
$this->messageManager = $messageManager;
75-
$this->helper = $helper;
49+
$this->systemHelper = $systemHelper;
7650
}
7751

7852
/**
79-
* Check change_email parameter
53+
* Prevent logout and sending notification email if 'change_email' parameter is set
8054
*
8155
* @param \Magento\Framework\Event\Observer $observer
8256
* @return void
83-
84-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
8557
*/
8658
public function execute(\Magento\Framework\Event\Observer $observer): void
8759
{
88-
if (!$this->request->isPost()
89-
|| !$this->request->getPost('change_email', false)
90-
|| !$this->helper->isDisableChangeEmail()) {
60+
/** $request RequestInterface */
61+
$request = $observer->getRequest();
62+
if (!$request->isPost()
63+
|| !$request->getPost('change_email', false)
64+
|| !$this->systemHelper->isChangeEmailDisabled()) {
9165
return;
9266
}
9367

9468
$this->response->setRedirect(
95-
$this->urlBuilder->getUrl('customer/account/edit'),
69+
$this->systemHelper->getUrl('customer/account/edit'),
9670
301
9771
);
98-
$this->messageManager->addErrorMessage(__('You cannot change email address.'));
99-
/* Stop further response processing */
72+
$this->systemHelper->getMessageManager()->addErrorMessage(
73+
$this->systemHelper->getChangeEmailErrorPhrase()
74+
);
75+
/** Stop further response processing */
10076
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
10177
}
10278
}

0 commit comments

Comments
 (0)