- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
We have made a new customer attribute but we are stuck in what to do to trigger magento to not allow the login.
We recently migrated from 1.9.x and are still figuring things out.
Management wants “to ensure that customer can not continue to place orders” after some sort of violation or problem. We know customers can simply create a new account with a new email address but this is more of a “we need to make management feel better about having a functional thing they can do”.
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello @bcadevteam
You can manage the customer login using a custom attribute.
Suppose there is a custom attribute code "allow_login" with the options of "Yes (1)" and "No (0)"
Now you can create a plugin to check if the customer is allowed to login or not.
Declare the plugin in your custom module.
app/code/Vendor/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\LoginPost"> <plugin name="check_customer_login_permission" type="Vendor\Module\Plugin\CheckCustomerLoginPermission" /> </type> </config>
Create your plugin class
app/code/Vendor/Module/Plugin/CheckCustomerLoginPermission.php
<?php namespace Vendor\Module\Plugin; class CheckCustomerLoginPermission { protected $_customerRepository; public function __construct( \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository ) { $this->_customerRepository = $customerRepository; } public function beforeExecute(\Magento\Customer\Controller\Account\LoginPost $subject) { $loginData = $subject->getRequest()->getPost('login'); $email = $loginData['username']; $customer = $this->_customerRepository->get($email); if ($customer->getId()) { $customerData = $customer->__toArray(); $isCustomerAllowedToLogin = $customerData['custom_attributes']['allow_login']['value']; if ($isCustomerAllowedToLogin == 0) { // do whatever you want when the customer is not allowed to login // for example: // return the customer to login page and show an error message that you are now allowed to login. } } } }
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello @bcadevteam
You can manage the customer login using a custom attribute.
Suppose there is a custom attribute code "allow_login" with the options of "Yes (1)" and "No (0)"
Now you can create a plugin to check if the customer is allowed to login or not.
Declare the plugin in your custom module.
app/code/Vendor/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\LoginPost"> <plugin name="check_customer_login_permission" type="Vendor\Module\Plugin\CheckCustomerLoginPermission" /> </type> </config>
Create your plugin class
app/code/Vendor/Module/Plugin/CheckCustomerLoginPermission.php
<?php namespace Vendor\Module\Plugin; class CheckCustomerLoginPermission { protected $_customerRepository; public function __construct( \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository ) { $this->_customerRepository = $customerRepository; } public function beforeExecute(\Magento\Customer\Controller\Account\LoginPost $subject) { $loginData = $subject->getRequest()->getPost('login'); $email = $loginData['username']; $customer = $this->_customerRepository->get($email); if ($customer->getId()) { $customerData = $customer->__toArray(); $isCustomerAllowedToLogin = $customerData['custom_attributes']['allow_login']['value']; if ($isCustomerAllowedToLogin == 0) { // do whatever you want when the customer is not allowed to login // for example: // return the customer to login page and show an error message that you are now allowed to login. } } } }