PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Api/Model/Resource/User.php

https://gitlab.com/blingbang2016/shop
PHP | 435 lines | 276 code | 28 blank | 131 comment | 21 complexity | 68f0455fadd667de2c49b361c0c63b48 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Api
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.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_Api
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Api_Model_Resource_User extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Resource initialization
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('api/user', 'user_id');
  42. }
  43. /**
  44. * Initialize unique fields
  45. *
  46. * @return Mage_Api_Model_Resource_User
  47. */
  48. protected function _initUniqueFields()
  49. {
  50. $this->_uniqueFields = array(
  51. array(
  52. 'field' => 'email',
  53. 'title' => Mage::helper('api')->__('Email')
  54. ),
  55. array(
  56. 'field' => 'username',
  57. 'title' => Mage::helper('api')->__('User Name')
  58. ),
  59. );
  60. return $this;
  61. }
  62. /**
  63. * Authenticate user by $username and $password
  64. *
  65. * @param Mage_Api_Model_User $user
  66. * @return Mage_Api_Model_Resource_User
  67. */
  68. public function recordLogin(Mage_Api_Model_User $user)
  69. {
  70. $data = array(
  71. 'lognum' => $user->getLognum()+1,
  72. );
  73. $condition = $this->_getReadAdapter()->quoteInto('user_id=?', $user->getUserId());
  74. $this->_getWriteAdapter()->update($this->getTable('api/user'), $data, $condition);
  75. return $this;
  76. }
  77. /**
  78. * Record api user session
  79. *
  80. * @param Mage_Api_Model_User $user
  81. * @return Mage_Api_Model_Resource_User
  82. */
  83. public function recordSession(Mage_Api_Model_User $user)
  84. {
  85. $readAdapter = $this->_getReadAdapter();
  86. $writeAdapter = $this->_getWriteAdapter();
  87. $select = $readAdapter->select()
  88. ->from($this->getTable('api/session'), 'user_id')
  89. ->where('user_id = ?', $user->getId())
  90. ->where('sessid = ?', $user->getSessid());
  91. $loginDate = now();
  92. if ($readAdapter->fetchRow($select)) {
  93. $writeAdapter->update(
  94. $this->getTable('api/session'),
  95. array ('logdate' => $loginDate),
  96. $readAdapter->quoteInto('user_id = ?', $user->getId()) . ' AND '
  97. . $readAdapter->quoteInto('sessid = ?', $user->getSessid())
  98. );
  99. } else {
  100. $writeAdapter->insert(
  101. $this->getTable('api/session'),
  102. array(
  103. 'user_id' => $user->getId(),
  104. 'logdate' => $loginDate,
  105. 'sessid' => $user->getSessid()
  106. )
  107. );
  108. }
  109. $user->setLogdate($loginDate);
  110. return $this;
  111. }
  112. /**
  113. * Clean old session
  114. *
  115. * @param Mage_Api_Model_User $user
  116. * @return Mage_Api_Model_Resource_User
  117. */
  118. public function cleanOldSessions(Mage_Api_Model_User $user)
  119. {
  120. $readAdapter = $this->_getReadAdapter();
  121. $writeAdapter = $this->_getWriteAdapter();
  122. $timeout = Mage::getStoreConfig('api/config/session_timeout');
  123. $timeSubtract = $readAdapter->getDateAddSql(
  124. 'logdate',
  125. $timeout,
  126. Varien_Db_Adapter_Interface::INTERVAL_SECOND);
  127. $writeAdapter->delete(
  128. $this->getTable('api/session'),
  129. array('user_id = ?' => $user->getId(), $readAdapter->quote(now()) . ' > '.$timeSubtract)
  130. );
  131. return $this;
  132. }
  133. /**
  134. * Load data by username
  135. *
  136. * @param string $username
  137. * @return array
  138. */
  139. public function loadByUsername($username)
  140. {
  141. $adapter = $this->_getReadAdapter();
  142. $select = $adapter->select()->from($this->getTable('api/user'))
  143. ->where('username=:username');
  144. return $adapter->fetchRow($select, array('username'=>$username));
  145. }
  146. /**
  147. * load by session id
  148. *
  149. * @param string $sessId
  150. * @return array
  151. */
  152. public function loadBySessId($sessId)
  153. {
  154. $result = array();
  155. $adapter = $this->_getReadAdapter();
  156. $select = $adapter->select()
  157. ->from($this->getTable('api/session'))
  158. ->where('sessid = ?', $sessId);
  159. if ($apiSession = $adapter->fetchRow($select)) {
  160. $selectUser = $adapter->select()
  161. ->from($this->getTable('api/user'))
  162. ->where('user_id = ?', $apiSession['user_id']);
  163. if ($user = $adapter->fetchRow($selectUser)) {
  164. $result = array_merge($user, $apiSession);
  165. }
  166. }
  167. return $result;
  168. }
  169. /**
  170. * Clear by session
  171. *
  172. * @param string $sessid
  173. * @return Mage_Api_Model_Resource_User
  174. */
  175. public function clearBySessId($sessid)
  176. {
  177. $this->_getWriteAdapter()->delete(
  178. $this->getTable('api/session'),
  179. array('sessid = ?' => $sessid)
  180. );
  181. return $this;
  182. }
  183. /**
  184. * Retrieve api user role data if it was assigned to role
  185. *
  186. * @param int | Mage_Api_Model_User $user
  187. * @return null | array
  188. */
  189. public function hasAssigned2Role($user)
  190. {
  191. $userId = null;
  192. $result = null;
  193. if (is_numeric($user)) {
  194. $userId = $user;
  195. } else if ($user instanceof Mage_Core_Model_Abstract) {
  196. $userId = $user->getUserId();
  197. }
  198. if ($userId) {
  199. $adapter = $this->_getReadAdapter();
  200. $select = $adapter->select();
  201. $select->from($this->getTable('api/role'))
  202. ->where('parent_id > 0 AND user_id = ?', $userId);
  203. $result = $adapter->fetchAll($select);
  204. }
  205. return $result;
  206. }
  207. /**
  208. * Action before save
  209. *
  210. * @param Mage_Core_Model_Abstract $user
  211. * @return Mage_Api_Model_Resource_User
  212. */
  213. protected function _beforeSave(Mage_Core_Model_Abstract $user)
  214. {
  215. if (!$user->getId()) {
  216. $user->setCreated(now());
  217. }
  218. $user->setModified(now());
  219. return $this;
  220. }
  221. /**
  222. * Delete the object
  223. *
  224. * @param Mage_Core_Model_Abstract $user
  225. * @return boolean
  226. */
  227. public function delete(Mage_Core_Model_Abstract $user)
  228. {
  229. $dbh = $this->_getWriteAdapter();
  230. $uid = (int) $user->getId();
  231. $dbh->beginTransaction();
  232. try {
  233. $dbh->delete($this->getTable('api/user'), array('user_id = ?' => $uid));
  234. $dbh->delete($this->getTable('api/role'), array('user_id = ?' => $uid));
  235. } catch (Mage_Core_Exception $e) {
  236. throw $e;
  237. return false;
  238. } catch (Exception $e) {
  239. $dbh->rollBack();
  240. return false;
  241. }
  242. $dbh->commit();
  243. return true;
  244. }
  245. /**
  246. * Save user roles
  247. *
  248. * @param Mage_Core_Model_Abstract $user
  249. * @return unknown
  250. */
  251. public function _saveRelations(Mage_Core_Model_Abstract $user)
  252. {
  253. $rolesIds = $user->getRoleIds();
  254. if (!is_array($rolesIds) || count($rolesIds) == 0) {
  255. return $user;
  256. }
  257. $adapter = $this->_getWriteAdapter();
  258. $adapter->beginTransaction();
  259. try {
  260. $adapter->delete(
  261. $this->getTable('api/role'),
  262. array('user_id = ?' => (int) $user->getId()));
  263. foreach ($rolesIds as $rid) {
  264. $rid = intval($rid);
  265. if ($rid > 0) {
  266. //$row = $this->load($user, $rid);
  267. } else {
  268. $row = array('tree_level' => 0);
  269. }
  270. $row = array('tree_level' => 0);
  271. $data = array(
  272. 'parent_id' => $rid,
  273. 'tree_level' => $row['tree_level'] + 1,
  274. 'sort_order' => 0,
  275. 'role_type' => Mage_Api_Model_Acl::ROLE_TYPE_USER,
  276. 'user_id' => $user->getId(),
  277. 'role_name' => $user->getFirstname()
  278. );
  279. $adapter->insert($this->getTable('api/role'), $data);
  280. }
  281. $adapter->commit();
  282. } catch (Mage_Core_Exception $e) {
  283. throw $e;
  284. } catch (Exception $e) {
  285. $adapter->rollBack();
  286. }
  287. return $this;
  288. }
  289. /**
  290. * Retrieve roles data
  291. *
  292. * @param Mage_Core_Model_Abstract $user
  293. * @return array
  294. */
  295. public function _getRoles(Mage_Core_Model_Abstract $user)
  296. {
  297. if (!$user->getId()) {
  298. return array();
  299. }
  300. $table = $this->getTable('api/role');
  301. $adapter = $this->_getReadAdapter();
  302. $select = $adapter->select()
  303. ->from($table, array())
  304. ->joinLeft(
  305. array('ar' => $table),
  306. $adapter->quoteInto(
  307. "ar.role_id = {$table}.parent_id AND ar.role_type = ?",
  308. Mage_Api_Model_Acl::ROLE_TYPE_GROUP),
  309. array('role_id'))
  310. ->where("{$table}.user_id = ?", $user->getId());
  311. return (($roles = $adapter->fetchCol($select)) ? $roles : array());
  312. }
  313. /**
  314. * Add Role
  315. *
  316. * @param Mage_Core_Model_Abstract $user
  317. * @return Mage_Api_Model_Resource_User
  318. */
  319. public function add(Mage_Core_Model_Abstract $user)
  320. {
  321. $adapter = $this->_getWriteAdapter();
  322. $aRoles = $this->hasAssigned2Role($user);
  323. if (sizeof($aRoles) > 0) {
  324. foreach ($aRoles as $idx => $data) {
  325. $adapter->delete(
  326. $this->getTable('api/role'),
  327. array('role_id = ?' => $data['role_id'])
  328. );
  329. }
  330. }
  331. if ($user->getId() > 0) {
  332. $role = Mage::getModel('api/role')->load($user->getRoleId());
  333. } else {
  334. $role = new Varien_Object(array('tree_level' => 0));
  335. }
  336. $adapter->insert($this->getTable('api/role'), array(
  337. 'parent_id' => $user->getRoleId(),
  338. 'tree_level'=> ($role->getTreeLevel() + 1),
  339. 'sort_order'=> 0,
  340. 'role_type' => Mage_Api_Model_Acl::ROLE_TYPE_USER,
  341. 'user_id' => $user->getUserId(),
  342. 'role_name' => $user->getFirstname()
  343. ));
  344. return $this;
  345. }
  346. /**
  347. * Delete from role
  348. *
  349. * @param Mage_Core_Model_Abstract $user
  350. * @return Mage_Api_Model_Resource_User
  351. */
  352. public function deleteFromRole(Mage_Core_Model_Abstract $user)
  353. {
  354. if ($user->getUserId() <= 0) {
  355. return $this;
  356. }
  357. if ($user->getRoleId() <= 0) {
  358. return $this;
  359. };
  360. $adapter = $this->_getWriteAdapter();
  361. $table = $this->getTable('api/role');
  362. $condition = array(
  363. "{$table}.user_id = ?" => $user->getUserId(),
  364. "{$table}.parent_id = ?"=> $user->getRoleId()
  365. );
  366. $adapter->delete($table, $condition);
  367. return $this;
  368. }
  369. /**
  370. * Retrieve roles which exists for user
  371. *
  372. * @param Mage_Core_Model_Abstract $user
  373. * @return array
  374. */
  375. public function roleUserExists(Mage_Core_Model_Abstract $user)
  376. {
  377. $result = array();
  378. if ($user->getUserId() > 0) {
  379. $adapter = $this->_getReadAdapter();
  380. $select = $adapter->select()->from($this->getTable('api/role'))
  381. ->where('parent_id = ?', $user->getRoleId())
  382. ->where('user_id = ?', $user->getUserId());
  383. $result = $adapter->fetchCol($select);
  384. }
  385. return $result;
  386. }
  387. /**
  388. * Check if user not unique
  389. *
  390. * @param Mage_Core_Model_Abstract $user
  391. * @return array
  392. */
  393. public function userExists(Mage_Core_Model_Abstract $user)
  394. {
  395. $usersTable = $this->getTable('api/user');
  396. $adapter = $this->_getReadAdapter();
  397. $condition = array(
  398. $adapter->quoteInto("{$usersTable}.username = ?", $user->getUsername()),
  399. $adapter->quoteInto("{$usersTable}.email = ?", $user->getEmail()),
  400. );
  401. $select = $adapter->select()
  402. ->from($usersTable)
  403. ->where(implode(' OR ', $condition))
  404. ->where($usersTable.'.user_id != ?', (int) $user->getId());
  405. return $adapter->fetchRow($select);
  406. }
  407. }