I'm trying to make a few amends to the core code, so I copied the relevant classes and placed them in the app/code/local/mage folder, which works a treat. However, as this is bad practice, I'm trying to turn it into a module. I have registered the module in app/etc/modules/Mymodule_Login.xml with the following code:
<?xml version="1.0"?>
<config>
<modules>
<Mymodule_Login>
<active>true</active>
<codePool>local</codePool>
</Mymodule_Login>
</modules>
</config>
I have the following file structure to hold my module
app/code/local/Mymodule/Login/etc/config.xml
app/code/local/Mymodule/Login/Model/Customer.php
app/code/local/Mymodule/Login/Resource/Customer.php
The config looks like:
<?xml version="1.0"?>
<config>
<global>
<models>
<customer>
<rewrite>
<customer>Mymodule_Login_Model_Customer</customer>
</rewrite>
</customer>
</models>
<models>
<customer_resource>
<rewrite>
<customer>Mymodule_Login_Model_Resource_Customer</customer>
</rewrite>
</customer_resource>
</models>
</global>
</config>
My model looks like:
class Mymodule_Login_Model_Customer extends Mage_Customer_Model_Customer
{
public function authenticate($login, $password)
{
$this->loadByUsername($login);
if ($this->getConfirmation() && $this->isConfirmationRequired()) {
throw Mage::exception('Mage_Core', Mage::helper('customer')->__('This account is not confirmed.'),
self::EXCEPTION_EMAIL_NOT_CONFIRMED
);
}
if (!$this->validatePassword($password)) {
throw Mage::exception('Mage_Core', Mage::helper('customer')->__('Invalid login or password.'),
self::EXCEPTION_INVALID_EMAIL_OR_PASSWORD
);
}
Mage::dispatchEvent('customer_customer_authenticated', array(
'model' => $this,
'password' => $password,
));
return true;
}
/**
* Load customer by username
*
* @param string $customerEmail
* @return Mage_Customer_Model_Customer
*/
public function loadByUsername($username)
{
$this->_getResource()->loadByUsername($this, $username);
return $this;
}
}
My resouce code looks like:
class Mymodule_Login_Model_Resource_Customer extends Mage_Customer_Model_Resource_Customer
{
/**
* Load customer by username
*
* @throws Mage_Core_Exception
*
* @param Mage_Customer_Model_Customer $customer
* @param string $username
* @param bool $testOnly
* @return Mage_Customer_Model_Resource_Customer
*/
public function loadByUsername(Mage_Customer_Model_Customer $customer, $username, $testOnly = false)
{
$adapter = $this->_getReadAdapter();
$bind = array('customer_username' => $username);
$select = $adapter->select()
->from($this->getEntityTable(), array($this->getEntityIdField()))
->where('username = :customer_username');
if ($customer->getSharingConfig()->isWebsiteScope()) {
if (!$customer->hasData('website_id')) {
Mage::throwException(
Mage::helper('customer')->__('Customer website ID must be specified when using the website scope')
);
}
$bind['website_id'] = (int)$customer->getWebsiteId();
$select->where('website_id = :website_id');
}
$customerId = $adapter->fetchOne($select, $bind);
if ($customerId) {
$this->load($customer, $customerId);
} else {
$customer->setData(array());
}
return $this;
}
}
When I try to login with the username, it fails and I get nothing in the error logs. Any help would be greatly appreciated!
app/code/local/Mymodule/Login/Model/Resource/Customer.phpnotapp/code/local/Mymodule/Login/Resource/Customer.php. I've updated the answer. – Marius Feb 17 '14 at 15:11