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: 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: class Mage_Admin_Model_User extends Mage_Core_Model_Abstract
62: {
63: 64: 65:
66: const XML_PATH_FORGOT_EMAIL_TEMPLATE = 'admin/emails/forgot_email_template';
67: const XML_PATH_FORGOT_EMAIL_IDENTITY = 'admin/emails/forgot_email_identity';
68: const XML_PATH_STARTUP_PAGE = 'admin/startup/page';
69:
70: 71: 72:
73: const MIN_PASSWORD_LENGTH = 7;
74:
75: 76: 77: 78: 79:
80: protected $_eventPrefix = 'admin_user';
81:
82: 83: 84: 85: 86:
87: protected $_role;
88:
89: 90: 91: 92: 93:
94: protected $_hasAvailableResources = true;
95:
96: 97: 98:
99: protected function _construct()
100: {
101: $this->_init('admin/user');
102: }
103:
104: 105: 106: 107: 108:
109: protected function _beforeSave()
110: {
111: $data = array(
112: 'firstname' => $this->getFirstname(),
113: 'lastname' => $this->getLastname(),
114: 'email' => $this->getEmail(),
115: 'modified' => now(),
116: 'extra' => serialize($this->getExtra())
117: );
118:
119: if($this->getId() > 0) {
120: $data['user_id'] = $this->getId();
121: }
122:
123: if( $this->getUsername() ) {
124: $data['username'] = $this->getUsername();
125: }
126:
127: if ($this->getNewPassword()) {
128:
129: $data['password'] = $this->_getEncodedPassword($this->getNewPassword());
130: } elseif ($this->getPassword() && $this->getPassword() != $this->getOrigData('password')) {
131:
132: $data['password'] = $this->_getEncodedPassword($this->getPassword());
133: }
134:
135: if (!is_null($this->getIsActive())) {
136: $data['is_active'] = intval($this->getIsActive());
137: }
138:
139: $this->addData($data);
140:
141: return parent::_beforeSave();
142: }
143:
144: 145: 146: 147: 148: 149:
150: public function ($data)
151: {
152: if (is_array($data)) {
153: $data = serialize($data);
154: }
155: $this->_getResource()->saveExtra($this, $data);
156: return $this;
157: }
158:
159: 160: 161: 162: 163:
164: public function saveRelations()
165: {
166: $this->_getResource()->_saveRelations($this);
167: return $this;
168: }
169:
170: 171: 172: 173: 174:
175: public function getRoles()
176: {
177: return $this->_getResource()->getRoles($this);
178: }
179:
180: 181: 182: 183: 184:
185: public function getRole()
186: {
187: if (null === $this->_role) {
188: $this->_role = Mage::getModel('admin/roles');
189: $roles = $this->getRoles();
190: if ($roles && isset($roles[0]) && $roles[0]) {
191: $this->_role->load($roles[0]);
192: }
193: }
194: return $this->_role;
195: }
196:
197: 198: 199: 200: 201:
202: public function deleteFromRole()
203: {
204: $this->_getResource()->deleteFromRole($this);
205: return $this;
206: }
207:
208: 209: 210: 211: 212:
213: public function roleUserExists()
214: {
215: $result = $this->_getResource()->roleUserExists($this);
216: return (is_array($result) && count($result) > 0) ? true : false;
217: }
218:
219: 220: 221: 222: 223:
224: public function add()
225: {
226: $this->_getResource()->add($this);
227: return $this;
228: }
229:
230: 231: 232: 233: 234:
235: public function userExists()
236: {
237: $result = $this->_getResource()->userExists($this);
238: return (is_array($result) && count($result) > 0) ? true : false;
239: }
240:
241: 242: 243: 244: 245:
246: public function getCollection() {
247: return Mage::getResourceModel('admin/user_collection');
248: }
249:
250: 251: 252: 253: 254: 255:
256: public function sendNewPasswordEmail()
257: {
258: return $this;
259: }
260:
261: 262: 263: 264: 265:
266: public function sendPasswordResetConfirmationEmail()
267: {
268:
269: $mailer = Mage::getModel('core/email_template_mailer');
270: $emailInfo = Mage::getModel('core/email_info');
271: $emailInfo->addTo($this->getEmail(), $this->getName());
272: $mailer->addEmailInfo($emailInfo);
273:
274:
275: $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_FORGOT_EMAIL_IDENTITY));
276: $mailer->setStoreId(0);
277: $mailer->setTemplateId(Mage::getStoreConfig(self::XML_PATH_FORGOT_EMAIL_TEMPLATE));
278: $mailer->setTemplateParams(array(
279: 'user' => $this
280: ));
281: $mailer->send();
282:
283: return $this;
284: }
285:
286: 287: 288: 289: 290: 291:
292: public function getName($separator = ' ')
293: {
294: return $this->getFirstname() . $separator . $this->getLastname();
295: }
296:
297: 298: 299: 300: 301:
302: public function getId()
303: {
304: return $this->getUserId();
305: }
306:
307: 308: 309: 310: 311:
312: public function getAclRole()
313: {
314: return 'U' . $this->getUserId();
315: }
316:
317: 318: 319: 320: 321: 322: 323: 324:
325: public function authenticate($username, $password)
326: {
327: $config = Mage::getStoreConfigFlag('admin/security/use_case_sensitive_login');
328: $result = false;
329:
330: try {
331: Mage::dispatchEvent('admin_user_authenticate_before', array(
332: 'username' => $username,
333: 'user' => $this
334: ));
335: $this->loadByUsername($username);
336: $sensitive = ($config) ? $username == $this->getUsername() : true;
337:
338: if ($sensitive && $this->getId() && Mage::helper('core')->validateHash($password, $this->getPassword())) {
339: if ($this->getIsActive() != '1') {
340: Mage::throwException(Mage::helper('adminhtml')->__('This account is inactive.'));
341: }
342: if (!$this->hasAssigned2Role($this->getId())) {
343: Mage::throwException(Mage::helper('adminhtml')->__('Access denied.'));
344: }
345: $result = true;
346: }
347:
348: Mage::dispatchEvent('admin_user_authenticate_after', array(
349: 'username' => $username,
350: 'password' => $password,
351: 'user' => $this,
352: 'result' => $result,
353: ));
354: }
355: catch (Mage_Core_Exception $e) {
356: $this->unsetData();
357: throw $e;
358: }
359:
360: if (!$result) {
361: $this->unsetData();
362: }
363: return $result;
364: }
365:
366: 367: 368: 369: 370: 371: 372:
373: public function login($username, $password)
374: {
375: if ($this->authenticate($username, $password)) {
376: $this->getResource()->recordLogin($this);
377: }
378: return $this;
379: }
380:
381: 382: 383: 384: 385:
386: public function reload()
387: {
388: $id = $this->getId();
389: $this->setId(null);
390: $this->load($id);
391: return $this;
392: }
393:
394: 395: 396: 397: 398: 399:
400: public function loadByUsername($username)
401: {
402: $this->setData($this->getResource()->loadByUsername($username));
403: return $this;
404: }
405:
406: 407: 408: 409: 410: 411:
412: public function hasAssigned2Role($user)
413: {
414: return $this->getResource()->hasAssigned2Role($user);
415: }
416:
417: 418: 419: 420: 421: 422:
423: protected function _getEncodedPassword($password)
424: {
425: return Mage::helper('core')->getHash($password, 2);
426: }
427:
428: 429: 430: 431: 432: 433: 434: 435:
436: public function ($parent = null, $path = '', $level = 0)
437: {
438: if ($parent == null) {
439: $parent = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu');
440: }
441: foreach ($parent->children() as $childName => $child) {
442: $aclResource = 'admin/' . $path . $childName;
443: if (Mage::getSingleton('admin/session')->isAllowed($aclResource)) {
444: if (!$child->children) {
445: return (string)$child->action;
446: } else if ($child->children) {
447: $action = $this->findFirstAvailableMenu($child->children, $path . $childName . '/', $level + 1);
448: return $action ? $action : (string)$child->action;
449: }
450: }
451: }
452: $this->_hasAvailableResources = false;
453: return '*/*/denied';
454: }
455:
456: 457: 458: 459: 460:
461: public function hasAvailableResources()
462: {
463: return $this->_hasAvailableResources;
464: }
465:
466: 467: 468: 469: 470: 471: 472:
473: public function getStatrupPageUrl()
474: {
475: return $this->getStartupPageUrl();
476: }
477:
478: 479: 480: 481: 482:
483: public function getStartupPageUrl()
484: {
485: $startupPage = Mage::getStoreConfig(self::XML_PATH_STARTUP_PAGE);
486: $aclResource = 'admin/' . $startupPage;
487: if (Mage::getSingleton('admin/session')->isAllowed($aclResource)) {
488: $nodePath = 'menu/' . join('/children/', explode('/', $startupPage)) . '/action';
489: $url = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode($nodePath);
490: if ($url) {
491: return $url;
492: }
493: }
494: return $this->findFirstAvailableMenu();
495: }
496:
497: 498: 499: 500: 501: 502:
503: public function validate()
504: {
505: $errors = array();
506:
507: if (!Zend_Validate::is($this->getUsername(), 'NotEmpty')) {
508: $errors[] = Mage::helper('adminhtml')->__('User Name is required field.');
509: }
510:
511: if (!Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
512: $errors[] = Mage::helper('adminhtml')->__('First Name is required field.');
513: }
514:
515: if (!Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
516: $errors[] = Mage::helper('adminhtml')->__('Last Name is required field.');
517: }
518:
519: if (!Zend_Validate::is($this->getEmail(), 'EmailAddress')) {
520: $errors[] = Mage::helper('adminhtml')->__('Please enter a valid email.');
521: }
522:
523: if ($this->hasNewPassword()) {
524: if (Mage::helper('core/string')->strlen($this->getNewPassword()) < self::MIN_PASSWORD_LENGTH) {
525: $errors[] = Mage::helper('adminhtml')->__('Password must be at least of %d characters.', self::MIN_PASSWORD_LENGTH);
526: }
527:
528: if (!preg_match('/[a-z]/iu', $this->getNewPassword())
529: || !preg_match('/[0-9]/u', $this->getNewPassword())
530: ) {
531: $errors[] = Mage::helper('adminhtml')->__('Password must include both numeric and alphabetic characters.');
532: }
533:
534: if ($this->hasPasswordConfirmation() && $this->getNewPassword() != $this->getPasswordConfirmation()) {
535: $errors[] = Mage::helper('adminhtml')->__('Password confirmation must be same as password.');
536: }
537: }
538:
539: if ($this->userExists()) {
540: $errors[] = Mage::helper('adminhtml')->__('A user with the same user name or email aleady exists.');
541: }
542:
543: if (empty($errors)) {
544: return true;
545: }
546: return $errors;
547: }
548:
549: 550: 551: 552: 553: 554: 555: 556: 557:
558: public function changeResetPasswordLinkToken($newResetPasswordLinkToken) {
559: if (!is_string($newResetPasswordLinkToken) || empty($newResetPasswordLinkToken)) {
560: throw Mage::exception('Mage_Core', Mage::helper('adminhtml')->__('Invalid password reset token.'));
561: }
562: $this->setRpToken($newResetPasswordLinkToken);
563: $currentDate = Varien_Date::now();
564: $this->setRpTokenCreatedAt($currentDate);
565:
566: return $this;
567: }
568:
569: 570: 571: 572: 573:
574: public function isResetPasswordLinkTokenExpired()
575: {
576: $resetPasswordLinkToken = $this->getRpToken();
577: $resetPasswordLinkTokenCreatedAt = $this->getRpTokenCreatedAt();
578:
579: if (empty($resetPasswordLinkToken) || empty($resetPasswordLinkTokenCreatedAt)) {
580: return true;
581: }
582:
583: $tokenExpirationPeriod = Mage::helper('admin')->getResetPasswordLinkExpirationPeriod();
584:
585: $currentDate = Varien_Date::now();
586: $currentTimestamp = Varien_Date::toTimestamp($currentDate);
587: $tokenTimestamp = Varien_Date::toTimestamp($resetPasswordLinkTokenCreatedAt);
588: if ($tokenTimestamp > $currentTimestamp) {
589: return true;
590: }
591:
592: $dayDifference = floor(($currentTimestamp - $tokenTimestamp) / (24 * 60 * 60));
593: if ($dayDifference >= $tokenExpirationPeriod) {
594: return true;
595: }
596:
597: return false;
598: }
599:
600: }
601: