forked from diglin/Diglin_Username
/
Customer.php
90 lines (82 loc) · 2.52 KB
/
Customer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Diglin GmbH
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category Diglin
* @package Diglin_Username
* @copyright Copyright (c) 2008-2015 Diglin GmbH - Switzerland (http://www.diglin.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Class Diglin_Username_Model_Customer
*/
class Diglin_Username_Model_Customer extends Mage_Customer_Model_Customer
{
/**
* Authenticate customer
*
* @param string $login
* @param string $password
* @return true
* @throws Exception
*/
public function authenticate($login, $password)
{
if (Zend_Validate::is($login, 'EmailAddress')) {
$this->loadByEmail($login);
} else if (Mage::getStoreConfigFlag('username/general/enabled')) {
$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 $customerUsername
* @return Mage_Customer_Model_Customer
*/
public function loadByUsername($customerUsername)
{
$this->_getResource()->loadByUsername($this, $customerUsername);
return $this;
}
/**
* Test if username already exists
*
* @param string $username
* @param int $websiteId
* @return Diglin_Username_Model_Customer|boolean
*/
public function customerUsernameExists($username, $websiteId = null)
{
if (!is_null($websiteId)) {
$this->setWebsiteId($websiteId);
}
$this->loadByUsername($username);
if ($this->getId()) {
return $this;
}
return false;
}
}