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