Cómo dejar de iniciar sesión en CustomerLogin Observer
-
16-10-2019 - |
Pregunta
Me gustaría negar el inicio de sesión para un grupo de clientes. Siguiendo un ejemplo Obtuve un observador en funcionamiento pero no puedo detener el proceso de inicio de sesión:
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.'));
}
}
Solución
Esto es bastante sencillo, solo necesita desestimar los detalles almacenados con el cliente/sesión.
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;
}
}
Otros consejos
yo encontre algo Y ahora esto funciona, pero Luke's La solución es mejor.
// redirect to new page
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('index'));
Mage::app()->getResponse()->sendResponse();
// Logout
Mage::getSingleton('customer/session')->logout();
exit;
Tenga en cuenta que deja de funcionar si cierre sesión antes de redirigir o si throwException()
.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange