1: <?php
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: class Mage_Customer_Model_Session extends Mage_Core_Model_Session_Abstract
35: {
36: 37: 38: 39: 40:
41: protected $_customer;
42:
43: 44: 45: 46: 47:
48: protected $_isCustomerIdChecked = null;
49:
50: 51: 52: 53: 54:
55: protected $_persistentCustomerGroupId = null;
56:
57: 58: 59: 60: 61:
62: public function getCustomerConfigShare()
63: {
64: return Mage::getSingleton('customer/config_share');
65: }
66:
67: public function __construct()
68: {
69: $namespace = 'customer';
70: if ($this->getCustomerConfigShare()->isWebsiteScope()) {
71: $namespace .= '_' . (Mage::app()->getStore()->getWebsite()->getCode());
72: }
73:
74: $this->init($namespace);
75: Mage::dispatchEvent('customer_session_init', array('customer_session'=>$this));
76: }
77:
78: 79: 80: 81: 82: 83:
84: public function setCustomer(Mage_Customer_Model_Customer $customer)
85: {
86:
87: if ($customer->isConfirmationRequired()) {
88: if ($customer->getConfirmation()) {
89: return $this->_logout();
90: }
91: }
92: $this->_customer = $customer;
93: $this->setId($customer->getId());
94:
95: if ((!$customer->isConfirmationRequired()) && $customer->getConfirmation()) {
96: $customer->setConfirmation(null)->save();
97: $customer->setIsJustConfirmed(true);
98: }
99: return $this;
100: }
101:
102: 103: 104: 105: 106:
107: public function getCustomer()
108: {
109: if ($this->_customer instanceof Mage_Customer_Model_Customer) {
110: return $this->_customer;
111: }
112:
113: $customer = Mage::getModel('customer/customer')
114: ->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
115: if ($this->getId()) {
116: $customer->load($this->getId());
117: }
118:
119: $this->setCustomer($customer);
120: return $this->_customer;
121: }
122:
123: 124: 125: 126: 127: 128:
129: public function setCustomerId($id)
130: {
131: $this->setData('customer_id', $id);
132: return $this;
133: }
134:
135: 136: 137: 138: 139:
140: public function getCustomerId()
141: {
142: if ($this->getData('customer_id')) {
143: return $this->getData('customer_id');
144: }
145: return ($this->isLoggedIn()) ? $this->getId() : null;
146: }
147:
148: 149: 150: 151: 152: 153:
154: public function setCustomerGroupId($id)
155: {
156: $this->setData('customer_group_id', $id);
157: return $this;
158: }
159:
160: 161: 162: 163: 164: 165:
166: public function getCustomerGroupId()
167: {
168: if ($this->getData('customer_group_id')) {
169: return $this->getData('customer_group_id');
170: }
171: if ($this->isLoggedIn() && $this->getCustomer()) {
172: return $this->getCustomer()->getGroupId();
173: }
174: return Mage_Customer_Model_Group::NOT_LOGGED_IN_ID;
175: }
176:
177: 178: 179: 180: 181:
182: public function isLoggedIn()
183: {
184: return (bool)$this->getId() && (bool)$this->checkCustomerId($this->getId());
185: }
186:
187: 188: 189: 190: 191: 192:
193: public function checkCustomerId($customerId)
194: {
195: if ($this->_isCustomerIdChecked === null) {
196: $this->_isCustomerIdChecked = Mage::getResourceSingleton('customer/customer')->checkCustomerId($customerId);
197: }
198: return $this->_isCustomerIdChecked;
199: }
200:
201: 202: 203: 204: 205: 206: 207:
208: public function login($username, $password)
209: {
210:
211: $customer = Mage::getModel('customer/customer')
212: ->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
213:
214: if ($customer->authenticate($username, $password)) {
215: $this->setCustomerAsLoggedIn($customer);
216: $this->renewSession();
217: return true;
218: }
219: return false;
220: }
221:
222: public function setCustomerAsLoggedIn($customer)
223: {
224: $this->setCustomer($customer);
225: Mage::dispatchEvent('customer_login', array('customer'=>$customer));
226: return $this;
227: }
228:
229: 230: 231: 232: 233: 234:
235: public function loginById($customerId)
236: {
237: $customer = Mage::getModel('customer/customer')->load($customerId);
238: if ($customer->getId()) {
239: $this->setCustomerAsLoggedIn($customer);
240: return true;
241: }
242: return false;
243: }
244:
245: 246: 247: 248: 249:
250: public function logout()
251: {
252: if ($this->isLoggedIn()) {
253: Mage::dispatchEvent('customer_logout', array('customer' => $this->getCustomer()) );
254: $this->_logout();
255: }
256: return $this;
257: }
258:
259: 260: 261: 262: 263: 264: 265:
266: public function authenticate(Mage_Core_Controller_Varien_Action $action, $loginUrl = null)
267: {
268: if ($this->isLoggedIn()) {
269: return true;
270: }
271:
272: $this->setBeforeAuthUrl(Mage::getUrl('*/*/*', array('_current' => true)));
273: if (isset($loginUrl)) {
274: $action->getResponse()->setRedirect($loginUrl);
275: } else {
276: $action->setRedirectWithCookieCheck(Mage_Customer_Helper_Data::ROUTE_ACCOUNT_LOGIN,
277: Mage::helper('customer')->getLoginUrlParams()
278: );
279: }
280:
281: return false;
282: }
283:
284: 285: 286: 287: 288: 289: 290:
291: protected function _setAuthUrl($key, $url)
292: {
293: $url = Mage::helper('core/url')
294: ->removeRequestParam($url, Mage::getSingleton('core/session')->getSessionIdQueryParam());
295:
296: $url = Mage::getModel('core/url')->getRebuiltUrl($url);
297: return $this->setData($key, $url);
298: }
299:
300: 301: 302: 303: 304:
305: protected function _logout()
306: {
307: $this->setId(null);
308: $this->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
309: $this->getCookie()->delete($this->getSessionName());
310: return $this;
311: }
312:
313: 314: 315: 316: 317: 318:
319: public function setBeforeAuthUrl($url)
320: {
321: return $this->_setAuthUrl('before_auth_url', $url);
322: }
323:
324: 325: 326: 327: 328: 329:
330: public function setAfterAuthUrl($url)
331: {
332: return $this->_setAuthUrl('after_auth_url', $url);
333: }
334:
335: 336: 337: 338: 339:
340: public function renewSession()
341: {
342: parent::renewSession();
343: Mage::getSingleton('core/session')->unsSessionHosts();
344:
345: return $this;
346: }
347: }
348: