2

I am looking for code how the customer can be enabled and disabled using customer Id in Magento 2.

When customer is disabled we should not allow customer to login from front end.

I tried setting is_active column as 0 in Database then also i can able to login from frontend.

I am using below code

  public function execute(){
     $user = $this->customerFactory->create();
        $userId = $this->getRequest()->getParam('user_id', false);
        $status = 0;
        if($userId):
          $user->load($userId)->setWebsiteId($oldUser->getWebsiteId());      
        endif;
      try {     
            $user->setIsActive($status);
            $user->save();  
         }catch (\Exception $e){
           $this->_messageManager->addError(__('Can\'t save user'));
         }
  }

is that can be done pro grammatically?

WaPoNe
  • 1,590
  • 3
  • 17
  • 35
Jafar Pinjar
  • 1,929
  • 7
  • 64
  • 139
  • there is little big process for doing this batter use free module for ithttps://github.com/amitshree/magento2-account-approval – Ansar Husain Aug 09 '18 at 12:47
  • It is not correct if we set is_active to 0 in customer_entity table? This module is not my requirement – Jafar Pinjar Aug 09 '18 at 12:55
  • it will not work with only set that attribute , you will have to use observer in registration time to check customer approved then login other wise logout it and redirect in same page with not active message same is for login condition you will have to create observer to check customer approved then continue login other wise logout and display message for not logged in like that process you will have to do – Ansar Husain Aug 09 '18 at 13:02
  • How to check customer is approved or not using customer Id? Because in my controller i get customer id with drop down value enable or disable, Using id i need to implement that functionality, – Jafar Pinjar Aug 09 '18 at 13:06
  • https://magento.stackexchange.com/questions/132577/magento2-best-way-to-load-customer-by-customer-id use this code to load customer by id then get attribute value $is_active=$customer->getCustomAttribute('is_active'); – Ansar Husain Aug 09 '18 at 13:15
  • is_active is not a custom attribute, its a default attribute, even after getting with getCustomAttribute also, how to restrict customer by logging in? – Jafar Pinjar Aug 09 '18 at 13:18
  • have you checked my answer? – Ketan Borada Jun 24 '19 at 09:57

1 Answers1

1

Create customer attribute Is blocked? (can only edit from admin,setby default NO) http://prntscr.com/n2yn78 , http://prntscr.com/n2yq5r then use observer event after login check it's value if it is blocked youcan logged out him and redirect to any page

Use customer_login event observer for customer login action

1) Create events.xml

app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_login">
        <observer name="customer_login_observer" instance="Vendor\Module\Observer\CustomerLogin" />
    </event>
</config>

2) Now create observer CustomerLogin.php

app/code/Vendor/Module/Observer/CustomerLogin.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class CustomerLogin implements ObserverInterface
{
    /**
     * @var \Magento\Framework\App\ResponseFactory
     */
    private $responseFactory;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    private $url;
    private $customerSession;
    private $messageManager;

    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\Message\ManagerInterface $messageManager
    ) {
        $this->responseFactory = $responseFactory;
        $this->url = $url;
        $this->customerSession= $customerSession;
        $this->messageManager = $messageManager;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $observer->getEvent()->getCustomer();

        if($customer->getIsBlocked()){
            $customerSession->logout();
            $this->messageManager->addErrorMessage(__('You are Blocked'));
            $redirectionUrl = $this->url->getUrl('set-your-redirect-url');
       $this->responseFactory->create()->setRedirect($redirectionUrl)->sendResponse();

            return $this;
        }
    }
}
Ketan Borada
  • 2,603
  • 2
  • 33
  • 77