PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/weburnit/magento-lite
PHP | 290 lines | 213 code | 28 blank | 49 comment | 23 complexity | 0c8ad88e06737f429e34a8ec19fde27f 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. * ACL user resource
  28. *
  29. * @category Mage
  30. * @package Mage_Admin
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Admin_Model_Mysql4_User extends Mage_Core_Model_Mysql4_Abstract
  34. {
  35. protected function _construct()
  36. {
  37. $this->_init('admin/user', 'user_id');
  38. }
  39. /**
  40. * Initialize unique fields
  41. *
  42. * @return Mage_Core_Model_Mysql4_Abstract
  43. */
  44. protected function _initUniqueFields()
  45. {
  46. $this->_uniqueFields = array(
  47. array(
  48. 'field' => 'email',
  49. 'title' => Mage::helper('adminhtml')->__('Email')
  50. ),
  51. array(
  52. 'field' => 'username',
  53. 'title' => Mage::helper('adminhtml')->__('User Name')
  54. ),
  55. );
  56. return $this;
  57. }
  58. /**
  59. * Authenticate user by $username and $password
  60. *
  61. * @param string $username
  62. * @param string $password
  63. * @return boolean|Object
  64. */
  65. public function recordLogin(Mage_Admin_Model_User $user)
  66. {
  67. $data = array(
  68. 'logdate' => now(),
  69. 'lognum' => $user->getLognum()+1
  70. );
  71. $condition = $this->_getWriteAdapter()->quoteInto('user_id=?', $user->getUserId());
  72. $this->_getWriteAdapter()->update($this->getTable('admin/user'), $data, $condition);
  73. return $this;
  74. }
  75. public function loadByUsername($username)
  76. {
  77. $select = $this->_getReadAdapter()->select()->from($this->getTable('admin/user'))
  78. ->where('username=:username');
  79. return $this->_getReadAdapter()->fetchRow($select, array('username'=>$username));
  80. }
  81. public function hasAssigned2Role($user)
  82. {
  83. if (is_numeric($user)) {
  84. $userId = $user;
  85. } else if ($user instanceof Mage_Core_Model_Abstract) {
  86. $userId = $user->getUserId();
  87. } else {
  88. return null;
  89. }
  90. if ( $userId > 0 ) {
  91. $dbh = $this->_getReadAdapter();
  92. $select = $dbh->select();
  93. $select->from($this->getTable('admin/role'))
  94. ->where("parent_id > 0 AND user_id = {$userId}");
  95. return $dbh->fetchAll($select);
  96. } else {
  97. return null;
  98. }
  99. }
  100. private function _encryptPassword($pwStr)
  101. {
  102. return Mage::helper('core')->getHash($pwStr, 2);
  103. }
  104. protected function _beforeSave(Mage_Core_Model_Abstract $user)
  105. {
  106. if (!$user->getId()) {
  107. $user->setCreated(now());
  108. }
  109. $user->setModified(now());
  110. return $this;
  111. }
  112. protected function _afterSave(Mage_Core_Model_Abstract $user)
  113. {
  114. $user->setExtra(unserialize($user->getExtra()));
  115. return $this;
  116. }
  117. protected function _afterLoad(Mage_Core_Model_Abstract $user)
  118. {
  119. if (is_string($user->getExtra())) {
  120. $user->setExtra(unserialize($user->getExtra()));
  121. }
  122. return parent::_afterLoad($user);
  123. }
  124. public function load(Mage_Core_Model_Abstract $user, $value, $field=null)
  125. {
  126. // if (!intval($value) && is_string($value)) {
  127. // $field = 'user_id';
  128. // }
  129. return parent::load($user, $value, $field);
  130. }
  131. public function delete(Mage_Core_Model_Abstract $user)
  132. {
  133. $dbh = $this->_getWriteAdapter();
  134. $uid = $user->getId();
  135. $dbh->beginTransaction();
  136. try {
  137. $dbh->delete($this->getTable('admin/user'), "user_id=$uid");
  138. $dbh->delete($this->getTable('admin/role'), "user_id=$uid");
  139. } catch (Mage_Core_Exception $e) {
  140. throw $e;
  141. return false;
  142. } catch (Exception $e){
  143. $dbh->rollBack();
  144. return false;
  145. }
  146. $dbh->commit();
  147. return true;
  148. }
  149. /**
  150. * TODO: unify _saveRelations() and add() methods, they make same things
  151. */
  152. public function _saveRelations(Mage_Core_Model_Abstract $user)
  153. {
  154. $rolesIds = $user->getRoleIds();
  155. if( !is_array($rolesIds) || count($rolesIds) == 0 ) {
  156. return $user;
  157. }
  158. $this->_getWriteAdapter()->beginTransaction();
  159. try {
  160. $this->_getWriteAdapter()->delete($this->getTable('admin/role'), "user_id = {$user->getId()}");
  161. foreach ($rolesIds as $rid) {
  162. $rid = intval($rid);
  163. if ($rid > 0) {
  164. $row = Mage::getModel('admin/role')->load($rid)->getData();
  165. } else {
  166. $row = array('tree_level' => 0);
  167. }
  168. $data = array(
  169. 'parent_id' => $rid,
  170. 'tree_level' => $row['tree_level'] + 1,
  171. 'sort_order' => 0,
  172. 'role_type' => 'U',
  173. 'user_id' => $user->getId(),
  174. 'role_name' => $user->getFirstname()
  175. );
  176. $this->_getWriteAdapter()->insert($this->getTable('admin/role'), $data);
  177. }
  178. $this->_getWriteAdapter()->commit();
  179. } catch (Mage_Core_Exception $e) {
  180. throw $e;
  181. } catch (Exception $e){
  182. $this->_getWriteAdapter()->rollBack();
  183. }
  184. }
  185. public function getRoles(Mage_Core_Model_Abstract $user)
  186. {
  187. if ( !$user->getId() ) {
  188. return array();
  189. }
  190. $table = $this->getTable('admin/role');
  191. $read = $this->_getReadAdapter();
  192. $select = $read->select()->from($table, array())
  193. ->joinLeft(array('ar' => $table), "(ar.role_id = `{$table}`.parent_id and ar.role_type = 'G')", array('role_id'))
  194. ->where("`{$table}`.user_id = {$user->getId()}");
  195. return (($roles = $read->fetchCol($select)) ? $roles : array());
  196. }
  197. public function add(Mage_Core_Model_Abstract $user)
  198. {
  199. $dbh = $this->_getWriteAdapter();
  200. $aRoles = $this->hasAssigned2Role($user);
  201. if ( sizeof($aRoles) > 0 ) {
  202. foreach($aRoles as $idx => $data){
  203. $dbh->delete($this->getTable('admin/role'), "role_id = {$data['role_id']}");
  204. }
  205. }
  206. if ($user->getId() > 0) {
  207. $role = Mage::getModel('admin/role')->load($user->getRoleId());
  208. } else {
  209. $role = new Varien_Object();
  210. $role->setTreeLevel(0);
  211. }
  212. $dbh->insert($this->getTable('admin/role'), array(
  213. 'parent_id' => $user->getRoleId(),
  214. 'tree_level'=> ($role->getTreeLevel() + 1),
  215. 'sort_order'=> 0,
  216. 'role_type' => 'U',
  217. 'user_id' => $user->getUserId(),
  218. 'role_name' => $user->getFirstname()
  219. ));
  220. return $this;
  221. }
  222. public function deleteFromRole(Mage_Core_Model_Abstract $user)
  223. {
  224. if ( $user->getUserId() <= 0 ) {
  225. return $this;
  226. }
  227. if ( $user->getRoleId() <= 0 ) {
  228. return $this;
  229. }
  230. $dbh = $this->_getWriteAdapter();
  231. $condition = "`{$this->getTable('admin/role')}`.user_id = ".$dbh->quote($user->getUserId())." AND `{$this->getTable('admin/role')}`.parent_id = ".$dbh->quote($user->getRoleId());
  232. $dbh->delete($this->getTable('admin/role'), $condition);
  233. return $this;
  234. }
  235. public function roleUserExists(Mage_Core_Model_Abstract $user)
  236. {
  237. if ( $user->getUserId() > 0 ) {
  238. $roleTable = $this->getTable('admin/role');
  239. $dbh = $this->_getReadAdapter();
  240. $select = $dbh->select()->from($roleTable)
  241. ->where("parent_id = {$user->getRoleId()} AND user_id = {$user->getUserId()}");
  242. return $dbh->fetchCol($select);
  243. } else {
  244. return array();
  245. }
  246. }
  247. public function userExists(Mage_Core_Model_Abstract $user)
  248. {
  249. $usersTable = $this->getTable('admin/user');
  250. $select = $this->_getReadAdapter()->select();
  251. $select->from($usersTable);
  252. $select->where("({$usersTable}.username = '{$user->getUsername()}' OR {$usersTable}.email = '{$user->getEmail()}') AND {$usersTable}.user_id != '{$user->getId()}'");
  253. return $this->_getReadAdapter()->fetchRow($select);
  254. }
  255. public function saveExtra($object, $data)
  256. {
  257. if ($object->getId()) {
  258. $this->_getWriteAdapter()->update($this->getMainTable(), array('extra'=>$data));
  259. }
  260. return $this;
  261. }
  262. }