PageRenderTime 23ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Admin/Model/User.php

https://github.com/weburnit/magento-lite
PHP | 420 lines | 252 code | 53 blank | 115 comment | 42 complexity | 6a2ddd2bac637f0e69fe80a82076910b MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Admin
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Admin user model
  28. *
  29. * @category Mage
  30. * @package Mage_Admin
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Admin_Model_User extends Mage_Core_Model_Abstract
  34. {
  35. const XML_PATH_FORGOT_EMAIL_TEMPLATE = 'admin/emails/forgot_email_template';
  36. const XML_PATH_FORGOT_EMAIL_IDENTITY = 'admin/emails/forgot_email_identity';
  37. const XML_PATH_STARTUP_PAGE = 'admin/startup/page';
  38. const MIN_PASSWORD_LENGTH = 7;
  39. protected $_eventPrefix = 'admin_user';
  40. /**
  41. * @var Mage_Admin_Model_Roles
  42. */
  43. protected $_role;
  44. protected $_hasAvailableResources = true;
  45. /**
  46. * Varien constructor
  47. */
  48. protected function _construct()
  49. {
  50. $this->_init('admin/user');
  51. }
  52. /**
  53. * Processing data before model save
  54. *
  55. * @return Mage_Admin_Model_User
  56. */
  57. protected function _beforeSave()
  58. {
  59. $data = array(
  60. 'firstname' => $this->getFirstname(),
  61. 'lastname' => $this->getLastname(),
  62. 'email' => $this->getEmail(),
  63. 'modified' => now(),
  64. 'extra' => serialize($this->getExtra())
  65. );
  66. if($this->getId() > 0) {
  67. $data['user_id'] = $this->getId();
  68. }
  69. if( $this->getUsername() ) {
  70. $data['username'] = $this->getUsername();
  71. }
  72. if ($this->getNewPassword()) { // change password
  73. $data['password'] = $this->_getEncodedPassword($this->getNewPassword());
  74. } elseif ($this->getPassword() && $this->getPassword() != $this->getOrigData('password')) { // new user password
  75. $data['password'] = $this->_getEncodedPassword($this->getPassword());
  76. }
  77. if ( !is_null($this->getIsActive()) ) {
  78. $data['is_active'] = intval($this->getIsActive());
  79. }
  80. $this->addData($data);
  81. return parent::_beforeSave();
  82. }
  83. /**
  84. * Save admin user extra data (like configuration sections state)
  85. *
  86. * @param array $data
  87. * @return Mage_Admin_Model_User
  88. */
  89. public function saveExtra($data)
  90. {
  91. if (is_array($data)) {
  92. $data = serialize($data);
  93. }
  94. $this->_getResource()->saveExtra($this, $data);
  95. return $this;
  96. }
  97. /**
  98. * Save user roles
  99. *
  100. * @return Mage_Admin_Model_User
  101. */
  102. public function saveRelations()
  103. {
  104. $this->_getResource()->_saveRelations($this);
  105. return $this;
  106. }
  107. public function getRoles()
  108. {
  109. return $this->_getResource()->getRoles($this);
  110. }
  111. /**
  112. * Get admin role model
  113. *
  114. * @return Mage_Admin_Model_Roles
  115. */
  116. public function getRole()
  117. {
  118. if (null === $this->_role) {
  119. $this->_role = Mage::getModel('admin/roles');
  120. $roles = $this->getRoles();
  121. if ($roles && isset($roles[0]) && $roles[0]) {
  122. $this->_role->load($roles[0]);
  123. }
  124. }
  125. return $this->_role;
  126. }
  127. public function deleteFromRole()
  128. {
  129. $this->_getResource()->deleteFromRole($this);
  130. return $this;
  131. }
  132. public function roleUserExists()
  133. {
  134. $result = $this->_getResource()->roleUserExists($this);
  135. return ( is_array($result) && count($result) > 0 ) ? true : false;
  136. }
  137. public function add()
  138. {
  139. $this->_getResource()->add($this);
  140. return $this;
  141. }
  142. public function userExists()
  143. {
  144. $result = $this->_getResource()->userExists($this);
  145. return ( is_array($result) && count($result) > 0 ) ? true : false;
  146. }
  147. public function getCollection() {
  148. return Mage::getResourceModel('admin/user_collection');
  149. }
  150. /**
  151. * Send email with new user password
  152. *
  153. * @return Mage_Admin_Model_User
  154. */
  155. public function sendNewPasswordEmail()
  156. {
  157. $translate = Mage::getSingleton('core/translate');
  158. /* @var $translate Mage_Core_Model_Translate */
  159. $translate->setTranslateInline(false);
  160. Mage::getModel('core/email_template')
  161. ->setDesignConfig(array('area' => 'adminhtml', 'store' => $this->getStoreId()))
  162. ->sendTransactional(
  163. Mage::getStoreConfig(self::XML_PATH_FORGOT_EMAIL_TEMPLATE),
  164. Mage::getStoreConfig(self::XML_PATH_FORGOT_EMAIL_IDENTITY),
  165. $this->getEmail(),
  166. $this->getName(),
  167. array('user' => $this, 'password' => $this->getPlainPassword()));
  168. $translate->setTranslateInline(true);
  169. return $this;
  170. }
  171. public function getName($separator=' ')
  172. {
  173. return $this->getFirstname() . $separator . $this->getLastname();
  174. }
  175. public function getId()
  176. {
  177. return $this->getUserId();
  178. }
  179. /**
  180. * Get user ACL role
  181. *
  182. * @return string
  183. */
  184. public function getAclRole()
  185. {
  186. return 'U' . $this->getUserId();
  187. }
  188. /**
  189. * Authenticate user name and password and save loaded record
  190. *
  191. * @param string $username
  192. * @param string $password
  193. * @return boolean
  194. * @throws Mage_Core_Exception
  195. */
  196. public function authenticate($username, $password)
  197. {
  198. $config = Mage::getStoreConfigFlag('admin/security/use_case_sensitive_login');
  199. $result = false;
  200. try {
  201. $this->loadByUsername($username);
  202. $sensitive = ($config) ? $username==$this->getUsername() : true;
  203. if ($sensitive && $this->getId() && Mage::helper('core')->validateHash($password, $this->getPassword())) {
  204. if ($this->getIsActive() != '1') {
  205. Mage::throwException(Mage::helper('adminhtml')->__('This account is inactive.'));
  206. }
  207. if (!$this->hasAssigned2Role($this->getId())) {
  208. Mage::throwException(Mage::helper('adminhtml')->__('Access denied.'));
  209. }
  210. $result = true;
  211. }
  212. Mage::dispatchEvent('admin_user_authenticate_after', array(
  213. 'username' => $username,
  214. 'password' => $password,
  215. 'user' => $this,
  216. 'result' => $result,
  217. ));
  218. }
  219. catch (Mage_Core_Exception $e) {
  220. $this->unsetData();
  221. throw $e;
  222. }
  223. if (!$result) {
  224. $this->unsetData();
  225. }
  226. return $result;
  227. }
  228. /**
  229. * Login user
  230. *
  231. * @param string $login
  232. * @param string $password
  233. * @return Mage_Admin_Model_User
  234. */
  235. public function login($username, $password)
  236. {
  237. if ($this->authenticate($username, $password)) {
  238. $this->getResource()->recordLogin($this);
  239. }
  240. return $this;
  241. }
  242. public function reload()
  243. {
  244. $id = $this->getId();
  245. $this->setId(null);
  246. $this->load($id);
  247. return $this;
  248. }
  249. public function loadByUsername($username)
  250. {
  251. $this->setData($this->getResource()->loadByUsername($username));
  252. return $this;
  253. }
  254. public function hasAssigned2Role($user)
  255. {
  256. return $this->getResource()->hasAssigned2Role($user);
  257. }
  258. protected function _getEncodedPassword($pwd)
  259. {
  260. return Mage::helper('core')->getHash($pwd, 2);
  261. }
  262. /**
  263. * Find first menu item that user is able to access
  264. *
  265. * @param Mage_Core_Model_Config_Element $parent
  266. * @param string $path
  267. * @param integer $level
  268. * @return string
  269. */
  270. public function findFirstAvailableMenu($parent=null, $path='', $level=0)
  271. {
  272. if ($parent == null) {
  273. $parent = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu');
  274. }
  275. foreach ($parent->children() as $childName=>$child) {
  276. $aclResource = 'admin/' . $path . $childName;
  277. if (Mage::getSingleton('admin/session')->isAllowed($aclResource)) {
  278. if (!$child->children) {
  279. return (string)$child->action;
  280. } else if ($child->children) {
  281. $action = $this->findFirstAvailableMenu($child->children, $path . $childName . '/', $level+1);
  282. return $action ? $action : (string)$child->action;
  283. }
  284. }
  285. }
  286. $this->_hasAvailableResources = false;
  287. return '*/*/denied';
  288. }
  289. /**
  290. * Check if user has available resources
  291. *
  292. * @return bool
  293. */
  294. public function hasAvailableResources()
  295. {
  296. return $this->_hasAvailableResources;
  297. }
  298. /**
  299. * Find admin start page url
  300. *
  301. * @deprecated Please use getStartupPageUrl() method instead
  302. * @see getStartupPageUrl()
  303. * @return string
  304. */
  305. public function getStatrupPageUrl()
  306. {
  307. return $this->getStartupPageUrl();
  308. }
  309. /**
  310. * Find admin start page url
  311. *
  312. * @return string
  313. */
  314. public function getStartupPageUrl()
  315. {
  316. $startupPage = Mage::getStoreConfig(self::XML_PATH_STARTUP_PAGE);
  317. $aclResource = 'admin/' . $startupPage;
  318. if (Mage::getSingleton('admin/session')->isAllowed($aclResource)) {
  319. $nodePath = 'menu/' . join('/children/', explode('/', $startupPage)) . '/action';
  320. $url = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode($nodePath);
  321. if ($url) {
  322. return $url;
  323. }
  324. }
  325. return $this->findFirstAvailableMenu();
  326. }
  327. /**
  328. * Validate user attribute values.
  329. * Returns TRUE or array of errors.
  330. *
  331. * @return mixed
  332. */
  333. public function validate()
  334. {
  335. $errors = array();
  336. if (!Zend_Validate::is($this->getUsername(), 'NotEmpty')) {
  337. $errors[] = Mage::helper('adminhtml')->__('User Name is required field.');
  338. }
  339. if (!Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
  340. $errors[] = Mage::helper('adminhtml')->__('First Name is required field.');
  341. }
  342. if (!Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
  343. $errors[] = Mage::helper('adminhtml')->__('Last Name is required field.');
  344. }
  345. if (!Zend_Validate::is($this->getEmail(), 'EmailAddress')) {
  346. $errors[] = Mage::helper('adminhtml')->__('Please enter a valid email.');
  347. }
  348. if ($this->hasNewPassword()) {
  349. if (Mage::helper('core/string')->strlen($this->getNewPassword()) < self::MIN_PASSWORD_LENGTH) {
  350. $errors[] = Mage::helper('adminhtml')->__('Password must be at least of %d characters.', self::MIN_PASSWORD_LENGTH);
  351. }
  352. if (!preg_match('/[a-z]/iu', $this->getNewPassword()) || !preg_match('/[0-9]/u', $this->getNewPassword())) {
  353. $errors[] = Mage::helper('adminhtml')->__('Password must include both numeric and alphabetic characters.');
  354. }
  355. if ($this->hasPasswordConfirmation() && $this->getNewPassword() != $this->getPasswordConfirmation()) {
  356. $errors[] = Mage::helper('adminhtml')->__('Password confirmation must be same as password.');
  357. }
  358. }
  359. if ($this->userExists()) {
  360. $errors[] = Mage::helper('adminhtml')->__('A user with the same user name or email aleady exists.');
  361. }
  362. if (empty($errors)) {
  363. return true;
  364. }
  365. return $errors;
  366. }
  367. }