3

I have imported customer recently. customer imported by default password which I have set same password for all customer.

Now I need two things to do for customer.

  1. How I can get customer login count, I want to check if customer login first time or not.

    if customer login first time to website, it should be redirect to reset password options.

  2. How to show customer reset password options, reset password page to show if customer login first time.

How can do these tasks to complete to best way?

1 Answer 1

0

A simple way to do it would be to check if there is an entry in the table password_reset_request_event with the user email address. If there is none, redirect your user to to the reset password option. The only issue with this option is that the user could make a request and never change it's password.

A better way to do it would be to add a custom customer attribute like "changed_default_password". Then create a plugin for magento/module-customer/Model/AccountManagement::resetPassword that will update the value of changed_default_password to true if the value of changed_default_password is false.

Since all user have the same password you should also make sure that no connected user can see any page of the website until the password has been reset. To do it create a frontend observer on controller_action_predispatch that look like this.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch">
        <observer name="check_reset_password" instance="Vendor\Module\Observer\RestrictWebsite" />
    </event>
</config>

In the restrict website observer should look like this. Note that you will have to change parts of the code to make it works, i didn't update all the class for you. The end of line comments were added to help you

<?php

namespace Vendor\Module\Observer;

use Magento\Customer\Model\Context;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Message\ManagerInterface;

/**
 * Class RestrictWebsite
 * @package PgModel\Customer\Observer
 */
class RestrictWebsite implements ObserverInterface
{
    /**
     * @var \Magento\Framework\UrlFactory
     */
    private $_urlFactory;

    /**
     * @var \Magento\Framework\App\Response\Http
     */
    private $_response;

    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    private $_actionFlag;

    /**
     * @var \Magento\Framework\App\Http\Context
     */
    private $_context;

    /**
     * @var \PgModel\Customer\Block\Account\Dashboard\Info
     */
    private $_dashboardInfo;

    /**
     * @var ManagerInterface
     */
    private $_messageManager;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    private $_urlInterface;

    /**
     * RestrictWebsite constructor.
     * @param \Magento\Framework\Event\ManagerInterface $eventManager
     * @param \Magento\Framework\App\Response\Http $response
     * @param \Magento\Framework\UrlFactory $urlFactory
     * @param ManagerInterface $messageManager
     * @param \PgModel\Customer\Block\Account\Dashboard\Info $dashboardInfo
     * @param \Magento\Framework\UrlInterface $urlInterface
     * @param \Magento\Framework\App\Http\Context $context
     * @param \Magento\Framework\App\ActionFlag $actionFlag
     */
    public function __construct(
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Framework\App\Response\Http $response,
        \Magento\Framework\UrlFactory $urlFactory,
        ManagerInterface $messageManager,
        \PgModel\Customer\Block\Account\Dashboard\Info $dashboardInfo,
        \Magento\Framework\UrlInterface $urlInterface,
        \Magento\Framework\App\Http\Context $context,
        \Magento\Framework\App\ActionFlag $actionFlag
    )
    {
        $this->_response = $response;
        $this->_urlFactory = $urlFactory;
        $this->_dashboardInfo = $dashboardInfo;
        $this->_urlInterface = $urlInterface;
        $this->_messageManager = $messageManager;
        $this->_context = $context;
        $this->_actionFlag = $actionFlag;
    }

    /**
     * * Detect if current customer avec accepted the terms
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $allowedRoutes = [
            'customer_account_login',
            'customer_account_index', //this one could be removed to make sure the user cannot see the dashboard
            'customer_account_loginpost',
            'customer_account_create',
            'customer_account_createpost',
            'customer_account_logoutsuccess',
            'customer_account_confirm',
            'customer_account_confirmation',
            'customer_account_forgotpassword',
            'customer_account_forgotpasswordpost',
            'customer_account_createpassword',
            'customer_account_resetpasswordpost',
            'customer_section_load'
        ];

        $request = $observer->getEvent()->getRequest();
        $isCustomerLoggedIn = $this->_context->getValue(Context::CONTEXT_AUTH);
        $actionFullName = strtolower($request->getFullActionName());

        if ($isCustomerLoggedIn && !in_array($actionFullName, $allowedRoutes)) {
            $haveAccepted = $this->_dashboardInfo->haveAcceptedTerms(); //change this to get the value of the custom field you will create

            $currentUrl = $this->_urlInterface->getCurrentUrl();
            if(!$haveAccepted && strpos($currentUrl, 'support/conditions') === false){
                $this->_messageManager->getMessages(true);
                $this->_messageManager->addError(__("Please accept the terms and conditions.")); //change the error message here for one more specific to the password reset
                $this->_response->setRedirect($this->_urlFactory->create()->getUrl('customer/account/index')); //redirect to the password reset link instead
            }
        }
    }
}
1
  • Thank you for you valuable reply. I’ll apply this solution and let you know if any help. Dec 11, 2019 at 19:40

Your Answer

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

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