5

Trying to prevent registered users from logging in based on their customer group id. I have this working in magento 1, however I haven't been successful in replicating it on magento2. Any help is appreciated.

Magento 1

public function customerlogin($observer)
{
   $customer = $observer->getEvent()->getCustomer();
   $session = Mage::getSingleton('customer/session');

   if ($customer->getData('group_id') == 2)
   {
    $session->setId(null)
       ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)
       ->getCookie()->delete('customer');

    Mage::throwException(__('Login not allowed'));
    return;
}

}

Magento 2 - unfinished

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $event      = $observer->getEvent();
    $customer   = $event->getCustomer();
    if($customer->getGroupId() == '2'){
        $this->messageManager->addNotice(__('Login not allowed'));
        //prevent login
    }
}
3
  • Which event you try to catch? Commented Apr 13, 2017 at 17:38
  • <event name="customer_login"> Commented Apr 13, 2017 at 17:54
  • See my answer. We should try with Plugin way. Commented Apr 14, 2017 at 4:56

1 Answer 1

6

Look into:

vendor/magento/module-customer/Controller/Account/Login.php

vendor/magento/module-customer/Controller/Ajax/Login.php

Magento will use \Magento\Customer\Model\AccountManagement::authenticate() to authenticate a customer by username and password. We should try Plugin with this method.

app/code/Vendor/Customer/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\Api\AccountManagementInterface">
        <plugin name="check_customer_group" type="Vendor\Customer\Plugin\AccountManagement" />
    </type>
</config>

app/code/Vendor/Customer/Plugin/AccountManagement.php

<?php

namespace Vendor\Customer\Plugin;

use Magento\Framework\Exception\LocalizedException;

class AccountManagement
{
    const BLACK_LIST_CUSTOMER_GROUP = 4;

    /**
     * Authenticate a customer
     * @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
     * @param \Magento\Customer\Api\Data\CustomerInterface $result
     * @return \Magento\Customer\Api\Data\CustomerInterface
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function afterAuthenticate(
        \Magento\Customer\Api\AccountManagementInterface $accountManagement,
        $result
    )
    {
        if($result->getGroupId() == self::BLACK_LIST_CUSTOMER_GROUP) {
            throw new LocalizedException(__('The customer group does not allow.'));
        }
        return $result;
    }
}

Remember to create module.xml and registration.php file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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