Skip to content

Commit

Permalink
Merge pull request #1 from wubinworks/features/webapi-support
Browse files Browse the repository at this point in the history
[Feature] Add WebAPI support
  • Loading branch information
wubinworks authored Oct 18, 2024
2 parents dd56d22 + ab44fbc commit 704e76b
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 104 deletions.
44 changes: 0 additions & 44 deletions Helper/Data.php

This file was deleted.

207 changes: 207 additions & 0 deletions Helper/System.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php
/**
* Copyright © Wubinworks. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Wubinworks\DisableChangeEmail\Helper;

use Magento\Framework\App\Area;
use Magento\Framework\App\State as AppState;
use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
use Magento\Framework\Phrase;

/**
* System helper
*/
class System extends \Magento\Framework\App\Helper\AbstractHelper
{
public const XML_PATH_CUSTOMER_ACCOUNT_INFORMATION_DISABLE_CHANGE_EMAIL
= 'customer/account_information/disable_change_email';

/**
* @var AppState
*/
protected $appState;

/**
* @var UserContextInterface
*/
protected $userContext;

/**
* @var MessageManagerInterface
*/
protected $messageManager;

/**
* Application Event Dispatcher
*
* @var EventManagerInterface
*/
protected $eventManager;

/**
* Constructor
*
* @param AppState $appState
* @param UserContextInterface $userContext
* @param MessageManagerInterface $messageManager
* @param EventManagerInterface $eventManager
* @param \Magento\Framework\App\Helper\Context $context
*/
public function __construct(
AppState $appState,
UserContextInterface $userContext,
MessageManagerInterface $messageManager,
EventManagerInterface $eventManager,
\Magento\Framework\App\Helper\Context $context
) {
parent::__construct($context);
$this->appState = $appState;
$this->userContext= $userContext;
$this->messageManager = $messageManager;
$this->eventManager = $eventManager;
}

/**
* Get current store system configuration value
*
* @param string $path
* @param string $scopeType
* @param null|int|string $scopeCode
* @return mixed
*/
public function getConfig($path, $scopeType = \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $scopeCode = null)
{
return $this->scopeConfig->getValue(
$path,
$scopeType,
$scopeCode
);
}

/**
* Get current area code.
*
* @return string
*/
public function getArea(): string
{
try {
$areaCode = $this->appState->getAreaCode();
} catch (LocalizedException $e) {
$areaCode = 'unknown';
}

return $areaCode;
}

/**
* Is admin or integration context
*
* @return bool
*/
public function isAdminOrIntegration(): bool
{
return in_array(
$this->userContext->getUserType(),
[UserContextInterface::USER_TYPE_INTEGRATION, UserContextInterface::USER_TYPE_ADMIN]
);
}

/**
* Is in webapi area
*
* @return bool
*/
public function isWebapi(): bool
{
return in_array(
$this->getArea(),
[Area::AREA_WEBAPI_REST, Area::AREA_WEBAPI_SOAP, Area::AREA_GRAPHQL]
);
}

/**
* Get current full action name
*
* @param string $delimiter
* @return string|null
*/
public function getFullActionName(string $delimiter = '/')
{
if ($this->getArea() === Area::AREA_FRONTEND
|| $this->getArea() === Area::AREA_ADMINHTML) {
return $this->_request->getFullActionName($delimiter);
}

return null;
}

/**
* Get message manager
*
* @return MessageManagerInterface
*/
public function getMessageManager(): MessageManagerInterface
{
return $this->messageManager;
}

/**
* Get user context object
*
* @return UserContextInterface
*/
public function getUserContextObject(): UserContextInterface
{
return $this->userContext;
}

/**
* Get event manager
*
* @return EventManagerInterface
*/
public function getEventManager(): EventManagerInterface
{
return $this->eventManager;
}

/**
* Build url
*
* @param string $route
* @param array $params
* @return string
*/
public function getUrl(string $route, $params = [])
{
return $this->_getUrl($route, $params);
}

/**
* Is change email disabled
*
* @return bool
*/
public function isChangeEmailDisabled(): bool
{
return (bool)$this->getConfig(self::XML_PATH_CUSTOMER_ACCOUNT_INFORMATION_DISABLE_CHANGE_EMAIL);
}

/**
* Get change email ertor phrase
*
* @return Phrase
*/
public function getChangeEmailErrorPhrase(): Phrase
{
return new Phrase('You cannot change email address.');
}
}
62 changes: 19 additions & 43 deletions Observer/EditPostObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,93 +10,69 @@
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\ActionFlag;
use Magento\Framework\UrlInterface;
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
use Wubinworks\DisableChangeEmail\Helper\Data as Helper;
use Wubinworks\DisableChangeEmail\Helper\System as SystemHelper;

/**
* Prevent customer from changing account email address
*/
class EditPostObserver implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var RequestInterface
*/
private $request;

/**
* @var ResponseInterface
*/
private $response;
protected $response;

/**
* @var ActionFlag
*/
private $actionFlag;

/**
* @var UrlInterface
*/
private $urlBuilder;
protected $actionFlag;

/**
* @var MessageManagerInterface
* @var SystemHelper
*/
private $messageManager;

/**
* @var Helper
*/
private $helper;
protected $systemHelper;

/**
* Constructor
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @param ActionFlag $actionFlag
* @param UrlInterface $urlBuilder
* @param MessageManagerInterface $messageManager
* @param Helper $helper
* @param SystemHelper $systemHelper
*/
public function __construct(
RequestInterface $request,
ResponseInterface $response,
ActionFlag $actionFlag,
UrlInterface $urlBuilder,
MessageManagerInterface $messageManager,
Helper $helper
SystemHelper $systemHelper
) {
$this->request = $request;
$this->response = $response;
$this->actionFlag = $actionFlag;
$this->urlBuilder = $urlBuilder;
$this->messageManager = $messageManager;
$this->helper = $helper;
$this->systemHelper = $systemHelper;
}

/**
* Check change_email parameter
* Prevent logout and sending notification email if 'change_email' parameter is set
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(\Magento\Framework\Event\Observer $observer): void
{
if (!$this->request->isPost()
|| !$this->request->getPost('change_email', false)
|| !$this->helper->isDisableChangeEmail()) {
/** $request RequestInterface */
$request = $observer->getRequest();
if (!$request->isPost()
|| !$request->getPost('change_email', false)
|| !$this->systemHelper->isChangeEmailDisabled()) {
return;
}

$this->response->setRedirect(
$this->urlBuilder->getUrl('customer/account/edit'),
$this->systemHelper->getUrl('customer/account/edit'),
301
);
$this->messageManager->addErrorMessage(__('You cannot change email address.'));
/* Stop further response processing */
$this->systemHelper->getMessageManager()->addErrorMessage(
$this->systemHelper->getChangeEmailErrorPhrase()
);
/** Stop further response processing */
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
}
}
Loading

0 comments on commit 704e76b

Please sign in to comment.