7

Anyone know how to block customer registration with specific email extension? I am getting so many automated user registrations with ...@163.com and ...@mail.ru how to block this type email registrations?

10

In your case, I think we can try with Plugin. The Magento\Customer\Controller\Account\CreatePost controller will execute the customer registration request. So, we can build the logic with this class.

app/code/Company/Module/etc/frontend/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Customer\Controller\Account\CreatePost">
        <plugin name="restrictCustomerEmail"
                type="Company\Module\Model\Plugin\Controller\Account\RestrictCustomerEmail"/>
    </type>
</config>

app/code/Company/Module/Model/Plugin/Controller/Account/RestrictCustomerEmail.php

<?php

namespace Company\Module\Model\Plugin\Controller\Account;

use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\UrlFactory;
use Magento\Framework\Message\ManagerInterface;

class RestrictCustomerEmail
{

    /** @var \Magento\Framework\UrlInterface */
    protected $urlModel;

    /**
     * @var \Magento\Framework\Controller\Result\RedirectFactory
     */
    protected $resultRedirectFactory;

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;

    /**
     * RestrictCustomerEmail constructor.
     * @param UrlFactory $urlFactory
     * @param RedirectFactory $redirectFactory
     * @param ManagerInterface $messageManager
     */
    public function __construct(
        UrlFactory $urlFactory,
        RedirectFactory $redirectFactory,
        ManagerInterface $messageManager

    )
    {
        $this->urlModel = $urlFactory->create();
        $this->resultRedirectFactory = $redirectFactory;
        $this->messageManager = $messageManager;
    }

    /**
     * @param \Magento\Customer\Controller\Account\CreatePost $subject
     * @param \Closure $proceed
     * @return mixed
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function aroundExecute(
        \Magento\Customer\Controller\Account\CreatePost $subject,
        \Closure $proceed
    )
    {
        /** @var \Magento\Framework\App\RequestInterface $request */
        $email = $subject->getRequest()->getParam('email');
        list($nick, $domain) = explode('@', $email, 2);
        if (in_array($domain, ['163.com', 'mail.ru'], true)) {

            $this->messageManager->addErrorMessage(
                'Registration is disabled for you domain'
            );
            $defaultUrl = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();

            return $resultRedirect->setUrl($defaultUrl);

        }
        return $proceed();
    }
}

NOTE: This way can prevent customer who registers via Front page. It's better if we check before saving customer entity.

|improve this answer|||||
7

You should create plugin on \Magento\Customer\Api\CustomerRepositoryInterface::save method and verify email of customer.

For example:

class Vendor\Module\Plugin\CustomerValidation 
{
     /**
     * Before customer save.
     *
     * @param CustomerRepositoryInterface $customerRepository
     * @param CustomerInterface $customer
     * @param null $passwordHash
     * @return array
     *

     */
    public function beforeSave(
        CustomerRepositoryInterface $customerRepository,
        CustomerInterface $customer,
        $passwordHash = null
    ) {
       list($nick, $domain) = explode('@', $customer->getEmail(), 2);

       if (in_array($domain, ['163.com', 'mail.ru'], true)) {
           throw new \Magento\Framework\Exception\LocalizedException(
                __(
                    'Registration is disabled for you domain'
                )
            );       
        }

        return [$customer, $passwordHash];
    }
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.