Mage_Api_Model_Mysql4_User Class Reference

Inheritance diagram for Mage_Api_Model_Mysql4_User:

Mage_Core_Model_Mysql4_Abstract Mage_Core_Model_Resource_Abstract

List of all members.

Public Member Functions

 recordLogin (Mage_Api_Model_User $user)
 recordSession (Mage_Api_Model_User $user)
 cleanOldSessions (Mage_Api_Model_User $user)
 loadByUsername ($username)
 loadBySessId ($sessId)
 clearBySessId ($sessid)
 hasAssigned2Role ($user)
 load (Mage_Core_Model_Abstract $user, $value, $field=null)
 delete (Mage_Core_Model_Abstract $user)
 _saveRelations (Mage_Core_Model_Abstract $user)
 _getRoles (Mage_Core_Model_Abstract $user)
 add (Mage_Core_Model_Abstract $user)
 deleteFromRole (Mage_Core_Model_Abstract $user)
 roleUserExists (Mage_Core_Model_Abstract $user)
 userExists (Mage_Core_Model_Abstract $user)

Protected Member Functions

 _construct ()
 _initUniqueFields ()
 _beforeSave (Mage_Core_Model_Abstract $user)


Detailed Description

Definition at line 34 of file User.php.


Member Function Documentation

_beforeSave ( Mage_Core_Model_Abstract object  )  [protected]

Perform actions before object save

Parameters:
Varien_Object $object

Reimplemented from Mage_Core_Model_Mysql4_Abstract.

Definition at line 171 of file User.php.

00172     {
00173         if (!$user->getId()) {
00174             $user->setCreated(now());
00175         }
00176         $user->setModified(now());
00177         return $this;
00178     }

_construct (  )  [protected]

Resource initialization

Reimplemented from Mage_Core_Model_Resource_Abstract.

Definition at line 37 of file User.php.

00038     {
00039         $this->_init('api/user', 'user_id');
00040     }

_getRoles ( Mage_Core_Model_Abstract user  ) 

Definition at line 243 of file User.php.

00244     {
00245         if ( !$user->getId() ) {
00246             return array();
00247         }
00248         $table  = $this->getTable('api/role');
00249         $read   = $this->_getReadAdapter();
00250         $select = $read->select()->from($table, array())
00251                     ->joinLeft(array('ar' => $table), "(ar.role_id = `{$table}`.parent_id and ar.role_type = 'G')", array('role_id'))
00252                     ->where("`{$table}`.user_id = {$user->getId()}");
00253 
00254         return (($roles = $read->fetchCol($select)) ? $roles : array());
00255     }

_initUniqueFields (  )  [protected]

Initialize unique fields

Returns:
Mage_Core_Model_Mysql4_Abstract

Reimplemented from Mage_Core_Model_Mysql4_Abstract.

Definition at line 47 of file User.php.

00048     {
00049         $this->_uniqueFields = array(
00050             array(
00051                 'field' => 'email',
00052                 'title' => Mage::helper('api')->__('Email')
00053             ),
00054             array(
00055                 'field' => 'username',
00056                 'title' => Mage::helper('api')->__('User Name')
00057             ),
00058         );
00059         return $this;
00060     }

_saveRelations ( Mage_Core_Model_Abstract user  ) 

Definition at line 204 of file User.php.

00205     {
00206         $rolesIds = $user->getRoleIds();
00207 
00208         if( !is_array($rolesIds) || count($rolesIds) == 0 ) {
00209             return $user;
00210         }
00211 
00212         $this->_getWriteAdapter()->beginTransaction();
00213 
00214         try {
00215             $this->_getWriteAdapter()->delete($this->getTable('api/role'), "user_id = {$user->getId()}");
00216             foreach ($rolesIds as $rid) {
00217                 $rid = intval($rid);
00218                 if ($rid > 0) {
00219                     //$row = $this->load($user, $rid);
00220                 } else {
00221                     $row = array('tree_level' => 0);
00222                 }
00223                 $row = array('tree_level' => 0);
00224 
00225                 $data = array(
00226                     'parent_id'     => $rid,
00227                     'tree_level'    => $row['tree_level'] + 1,
00228                     'sort_order'    => 0,
00229                     'role_type'     => 'U',
00230                     'user_id'       => $user->getId(),
00231                     'role_name'     => $user->getFirstname()
00232                 );
00233                 $this->_getWriteAdapter()->insert($this->getTable('api/role'), $data);
00234             }
00235             $this->_getWriteAdapter()->commit();
00236         } catch (Mage_Core_Exception $e) {
00237             throw $e;
00238         } catch (Exception $e){
00239             $this->_getWriteAdapter()->rollBack();
00240         }
00241     }

add ( Mage_Core_Model_Abstract user  ) 

Definition at line 257 of file User.php.

00257                                                         {
00258 
00259         $dbh = $this->_getWriteAdapter();
00260 
00261         $aRoles = $this->hasAssigned2Role($user);
00262         if ( sizeof($aRoles) > 0 ) {
00263             foreach($aRoles as $idx => $data){
00264                 $dbh->delete($this->getTable('api/role'), "role_id = {$data['role_id']}");
00265             }
00266         }
00267 
00268         if ($user->getId() > 0) {
00269             $role = Mage::getModel('api/role')->load($user->getRoleId());
00270         } else {
00271             $role = array('tree_level' => 0);
00272         }
00273         $dbh->insert($this->getTable('api/role'), array(
00274             'parent_id' => $user->getRoleId(),
00275             'tree_level'=> ($role->getTreeLevel() + 1),
00276             'sort_order'=> 0,
00277             'role_type' => 'U',
00278             'user_id'   => $user->getUserId(),
00279             'role_name' => $user->getFirstname()
00280         ));
00281 
00282         return $this;
00283     }

cleanOldSessions ( Mage_Api_Model_User user  ) 

Definition at line 107 of file User.php.

00108     {
00109         $timeout = Mage::getStoreConfig('api/config/session_timeout');
00110         $this->_getWriteAdapter()->delete(
00111             $this->getTable('api/session'),
00112             $this->_getReadAdapter()->quoteInto('user_id = ?', $user->getId()) . ' AND '
00113             . new Zend_Db_Expr('(UNIX_TIMESTAMP(\'' . now() . '\') - UNIX_TIMESTAMP(logdate)) > ' . $timeout)
00114         );
00115         return $this;
00116     }

clearBySessId ( sessid  ) 

Definition at line 141 of file User.php.

00142     {
00143         $this->_getWriteAdapter()->delete(
00144             $this->getTable('api/session'),
00145             $this->_getReadAdapter()->quoteInto('sessid = ?', $sessid)
00146         );
00147         return $this;
00148     }

delete ( Mage_Core_Model_Abstract object  ) 

Delete the object

Parameters:
Varien_Object $object
Returns:
Mage_Core_Model_Mysql4_Abstract

Reimplemented from Mage_Core_Model_Mysql4_Abstract.

Definition at line 185 of file User.php.

00186     {
00187         $dbh = $this->_getWriteAdapter();
00188         $uid = (int) $user->getId();
00189         $dbh->beginTransaction();
00190         try {
00191             $dbh->delete($this->getTable('api/user'), "user_id=$uid");
00192             $dbh->delete($this->getTable('api/role'), "user_id=$uid");
00193         } catch (Mage_Core_Exception $e) {
00194             throw $e;
00195             return false;
00196         } catch (Exception $e){
00197             $dbh->rollBack();
00198             return false;
00199         }
00200         $dbh->commit();
00201         return true;
00202     }

deleteFromRole ( Mage_Core_Model_Abstract user  ) 

Definition at line 285 of file User.php.

00285                                                                    {
00286         if ( $user->getUserId() <= 0 ) {
00287             return $this;
00288         }
00289         if ( $user->getRoleId() <= 0 ) {
00290             return $this;
00291         }
00292         $dbh = $this->_getWriteAdapter();
00293         $condition = "`{$this->getTable('api/role')}`.user_id = ".$dbh->quote($user->getUserId())." AND `{$this->getTable('api/role')}`.parent_id = ".$dbh->quote($user->getRoleId());
00294         $dbh->delete($this->getTable('api/role'), $condition);
00295         return $this;
00296     }

hasAssigned2Role ( user  ) 

Definition at line 150 of file User.php.

00151     {
00152         if (is_numeric($user)) {
00153             $userId = $user;
00154         } else if ($user instanceof Mage_Core_Model_Abstract) {
00155             $userId = $user->getUserId();
00156         } else {
00157             return null;
00158         }
00159 
00160         if ( $userId > 0 ) {
00161             $dbh = $this->_getReadAdapter();
00162             $select = $dbh->select();
00163             $select->from($this->getTable('api/role'))
00164                 ->where("parent_id > 0 AND user_id = {$userId}");
00165             return $dbh->fetchAll($select);
00166         } else {
00167             return null;
00168         }
00169     }

load ( Mage_Core_Model_Abstract object,
value,
field = null 
)

Load an object

Parameters:
Mage_Core_Model_Abstract $object
mixed $value
string $field field to load by (defaults to model id)
Returns:
Mage_Core_Model_Mysql4_Abstract

Reimplemented from Mage_Core_Model_Mysql4_Abstract.

Definition at line 180 of file User.php.

00181     {
00182         return parent::load($user, $value, $field);
00183     }

loadBySessId ( sessId  ) 

Definition at line 125 of file User.php.

00126     {
00127         $select = $this->_getReadAdapter()->select()
00128             ->from($this->getTable('api/session'))
00129             ->where('sessid = ?', $sessId);
00130         if ($apiSession = $this->_getReadAdapter()->fetchRow($select)) {
00131             $selectUser = $this->_getReadAdapter()->select()
00132                 ->from($this->getTable('api/user'))
00133                 ->where('user_id = ?', $apiSession['user_id']);
00134                 if ($user = $this->_getReadAdapter()->fetchRow($selectUser)) {
00135                     return array_merge($user, $apiSession);
00136                 }
00137         }
00138         return array();
00139     }

loadByUsername ( username  ) 

Definition at line 118 of file User.php.

00119     {
00120         $select = $this->_getReadAdapter()->select()->from($this->getTable('api/user'))
00121             ->where('username=:username');
00122         return $this->_getReadAdapter()->fetchRow($select, array('username'=>$username));
00123     }

recordLogin ( Mage_Api_Model_User user  ) 

Authenticate user by $username and $password

Parameters:
string $username
string $password
Returns:
boolean|Object

Definition at line 69 of file User.php.

00070     {
00071         $data = array(
00072             'lognum'  => $user->getLognum()+1,
00073         );
00074         $condition = $this->_getWriteAdapter()->quoteInto('user_id=?', $user->getUserId());
00075         $this->_getWriteAdapter()->update($this->getTable('api/user'), $data, $condition);
00076         return $this;
00077     }

recordSession ( Mage_Api_Model_User user  ) 

Definition at line 79 of file User.php.

00080     {
00081         $select = $this->_getReadAdapter()->select()
00082             ->from($this->getTable('api/session'), 'user_id')
00083             ->where('user_id = ?', $user->getId())
00084             ->where('sessid = ?', $user->getSessid());
00085         $logdate = now();
00086         if ($this->_getReadAdapter()->fetchRow($select)) {
00087             $this->_getWriteAdapter()->update(
00088                 $this->getTable('api/session'),
00089                 array ('logdate' => $logdate),
00090                 $this->_getReadAdapter()->quoteInto('user_id = ?', $user->getId()) . ' AND '
00091                 . $this->_getReadAdapter()->quoteInto('sessid = ?', $user->getSessid())
00092             );
00093         } else {
00094             $this->_getWriteAdapter()->insert(
00095                 $this->getTable('api/session'),
00096                 array(
00097                     'user_id' => $user->getId(),
00098                     'logdate' => $logdate,
00099                     'sessid' => $user->getSessid()
00100                 )
00101             );
00102         }
00103         $user->setLogdate($logdate);
00104         return $this;
00105     }

roleUserExists ( Mage_Core_Model_Abstract user  ) 

Definition at line 298 of file User.php.

00299     {
00300         if ( $user->getUserId() > 0 ) {
00301             $roleTable = $this->getTable('api/role');
00302             $dbh    = $this->_getReadAdapter();
00303             $select = $dbh->select()->from($roleTable)
00304                 ->where("parent_id = {$user->getRoleId()} AND user_id = {$user->getUserId()}");
00305             return $dbh->fetchCol($select);
00306         } else {
00307             return array();
00308         }
00309     }

userExists ( Mage_Core_Model_Abstract user  ) 

Definition at line 311 of file User.php.

00312     {
00313         $usersTable = $this->getTable('api/user');
00314         $select = $this->_getReadAdapter()->select();
00315         $select->from($usersTable);
00316         $select->where("({$usersTable}.username = '{$user->getUsername()}' OR {$usersTable}.email = '{$user->getEmail()}') AND {$usersTable}.user_id != '{$user->getId()}'");
00317         return $this->_getReadAdapter()->fetchRow($select);
00318     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:33 2009 for Magento by  doxygen 1.5.8