00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Customer_Model_Session extends Mage_Core_Model_Session_Abstract
00035 {
00036 protected $_customer;
00037
00038
00039
00040
00041
00042
00043 public function getCustomerConfigShare()
00044 {
00045 return Mage::getSingleton('customer/config_share');
00046 }
00047
00048 public function __construct()
00049 {
00050 $namespace = 'customer';
00051 if ($this->getCustomerConfigShare()->isWebsiteScope()) {
00052 $namespace .= '_' . (Mage::app()->getStore()->getWebsite()->getCode());
00053 }
00054
00055 $this->init($namespace);
00056 Mage::dispatchEvent('customer_session_init', array('customer_session'=>$this));
00057 }
00058
00059
00060
00061
00062
00063
00064
00065 public function setCustomer(Mage_Customer_Model_Customer $customer)
00066 {
00067
00068 if ($customer->isConfirmationRequired()) {
00069 if ($customer->getConfirmation()) {
00070 throw new Exception('This customer is not confirmed and cannot log in.',
00071 Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED
00072 );
00073 }
00074 }
00075 $this->_customer = $customer;
00076 $this->setId($customer->getId());
00077
00078 if ((!$customer->isConfirmationRequired()) && $customer->getConfirmation()) {
00079 $customer->setConfirmation(null)->save();
00080 $customer->setIsJustConfirmed(true);
00081 }
00082 return $this;
00083 }
00084
00085
00086
00087
00088
00089
00090 public function getCustomer()
00091 {
00092 if ($this->_customer instanceof Mage_Customer_Model_Customer) {
00093 return $this->_customer;
00094 }
00095
00096 $customer = Mage::getModel('customer/customer')
00097 ->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
00098 if ($this->getId()) {
00099 $customer->load($this->getId());
00100 }
00101
00102 $this->setCustomer($customer);
00103 return $this->_customer;
00104 }
00105
00106
00107
00108
00109
00110
00111 public function getCustomerId()
00112 {
00113 return $this->getId();
00114 }
00115
00116
00117
00118
00119
00120
00121
00122 public function getCustomerGroupId()
00123 {
00124 if ($this->isLoggedIn()) {
00125 return $this->getCustomer()->getGroupId();
00126 } else {
00127 return Mage_Customer_Model_Group::NOT_LOGGED_IN_ID;
00128 }
00129 }
00130
00131
00132
00133
00134
00135
00136 public function isLoggedIn()
00137 {
00138 return (bool)$this->getId() && (bool)$this->checkCustomerId($this->getId());
00139 }
00140
00141
00142
00143
00144
00145
00146
00147 public function checkCustomerId($customerId)
00148 {
00149 return Mage::getResourceSingleton('customer/customer')
00150 ->checkCustomerId($customerId);
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160 public function login($username, $password)
00161 {
00162 $customer = Mage::getModel('customer/customer')
00163 ->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
00164
00165 if ($customer->authenticate($username, $password)) {
00166 $this->setCustomer($customer);
00167 Mage::dispatchEvent('customer_login', array('customer'=>$customer));
00168 return true;
00169 }
00170 return false;
00171 }
00172
00173 public function setCustomerAsLoggedIn($customer)
00174 {
00175 $this->setCustomer($customer);
00176 Mage::dispatchEvent('customer_login', array('customer'=>$customer));
00177 return $this;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186 public function loginById($customerId)
00187 {
00188 $customer = Mage::getModel('customer/customer')->load($customerId);
00189 if ($customer) {
00190 $this->setCustomer($customer);
00191 Mage::dispatchEvent('customer_login', array('customer'=>$customer));
00192 return true;
00193 }
00194 return false;
00195 }
00196
00197
00198
00199
00200
00201
00202 public function logout()
00203 {
00204 if ($this->isLoggedIn()) {
00205 Mage::dispatchEvent('customer_logout', array('customer' => $this->getCustomer()) );
00206 $this->setId(null);
00207 }
00208 return $this;
00209 }
00210
00211
00212
00213
00214
00215
00216
00217 public function authenticate(Mage_Core_Controller_Varien_Action $action, $loginUrl = null)
00218 {
00219 if (!$this->isLoggedIn()) {
00220 $this->setBeforeAuthUrl(Mage::getUrl('*/*/*', array('_current'=>true)));
00221 if (is_null($loginUrl)) {
00222 $loginUrl = Mage::helper('customer')->getLoginUrl();
00223 }
00224 $action->getResponse()->setRedirect($loginUrl);
00225 return false;
00226 }
00227 return true;
00228 }
00229 }