4

I would like to deny login for a customer group. Following an example I got a working observer but I am not able to stop the login process:

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

    if ($customer->getData('group_id') == 4)
    {
         $session->setCustomer(Mage::getModel('customer/customer'))->setId(null);
         Mage::throwException(__('This account is not activated.'));
    }
}

2 Answers 2

7

This is pretty straight forward, you just need to unset the details stored against the customer/session.

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

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

        Mage::throwException(__('This account is not activated.'));
        return;
    }
}
0

I found something and now this works, but Luke's solution is better.

// redirect to new page
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('index'));
Mage::app()->getResponse()->sendResponse();

// Logout
Mage::getSingleton('customer/session')->logout();

exit;

Note, that it stops working if you logout before redirecting or if you throwException().

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.