00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class Mage_Api_Model_User extends Mage_Core_Model_Abstract
00028 {
00029 protected function _construct()
00030 {
00031 $this->_init('api/user');
00032 }
00033
00034 public function save()
00035 {
00036 $this->_beforeSave();
00037 $data = array(
00038 'firstname' => $this->getFirstname(),
00039 'lastname' => $this->getLastname(),
00040 'email' => $this->getEmail(),
00041 'modified' => Mage::getSingleton('core/date')->gmtDate()
00042 );
00043
00044 if($this->getId() > 0) {
00045 $data['user_id'] = $this->getId();
00046 }
00047
00048 if( $this->getUsername() ) {
00049 $data['username'] = $this->getUsername();
00050 }
00051
00052 if ($this->getApiKey()) {
00053 $data['api_key'] = $this->_getEncodedApiKey($this->getApiKey());
00054 }
00055
00056 if ($this->getNewApiKey()) {
00057 $data['api_key'] = $this->_getEncodedApiKey($this->getNewApiKey());
00058 }
00059
00060 if ( !is_null($this->getIsActive()) ) {
00061 $data['is_active'] = intval($this->getIsActive());
00062 }
00063
00064 $this->setData($data);
00065 $this->_getResource()->save($this);
00066 $this->_afterSave();
00067 return $this;
00068 }
00069
00070 public function delete()
00071 {
00072 $this->_getResource()->delete($this);
00073 return $this;
00074 }
00075
00076 public function saveRelations()
00077 {
00078 $this->_getResource()->_saveRelations($this);
00079 return $this;
00080 }
00081
00082 public function getRoles()
00083 {
00084 return $this->_getResource()->_getRoles($this);
00085 }
00086
00087 public function deleteFromRole()
00088 {
00089 $this->_getResource()->deleteFromRole($this);
00090 return $this;
00091 }
00092
00093 public function roleUserExists()
00094 {
00095 $result = $this->_getResource()->roleUserExists($this);
00096 return ( is_array($result) && count($result) > 0 ) ? true : false;
00097 }
00098
00099 public function add()
00100 {
00101 $this->_getResource()->add($this);
00102 return $this;
00103 }
00104
00105 public function userExists()
00106 {
00107 $result = $this->_getResource()->userExists($this);
00108 return ( is_array($result) && count($result) > 0 ) ? true : false;
00109 }
00110
00111 public function getCollection() {
00112 return Mage::getResourceModel('admin/user_collection');
00113 }
00114
00115 public function getName($separator=' ')
00116 {
00117 return $this->getFirstname().$separator.$this->getLastname();
00118 }
00119
00120 public function getId()
00121 {
00122 return $this->getUserId();
00123 }
00124
00125
00126
00127
00128
00129
00130 public function getAclRole()
00131 {
00132 return 'U'.$this->getUserId();
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142 public function authenticate($username, $apiKey)
00143 {
00144 $this->loadByUsername($username);
00145 if (!$this->getId()) {
00146 return false;
00147 }
00148 $auth = Mage::helper('core')->validateHash($apiKey, $this->getApiKey());
00149 if ($auth) {
00150 return true;
00151 } else {
00152 $this->unsetData();
00153 return false;
00154 }
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164 public function login($username, $apiKey)
00165 {
00166 $sessId = $this->getSessid();
00167 if ($this->authenticate($username, $apiKey)) {
00168 $this->setSessid($sessId);
00169 $this->getResource()->cleanOldSessions($this)
00170 ->recordLogin($this)
00171 ->recordSession($this);
00172 Mage::dispatchEvent('api_user_authenticated', array(
00173 'model' => $this,
00174 'api_key' => $apiKey,
00175 ));
00176 }
00177
00178 return $this;
00179 }
00180
00181 public function reload()
00182 {
00183 $this->load($this->getId());
00184 return $this;
00185 }
00186
00187 public function loadByUsername($username)
00188 {
00189 $this->setData($this->getResource()->loadByUsername($username));
00190 return $this;
00191 }
00192
00193 public function loadBySessId ($sessId)
00194 {
00195 $this->setData($this->getResource()->loadBySessId($sessId));
00196 return $this;
00197 }
00198
00199 public function logoutBySessId($sessid)
00200 {
00201 $this->getResource()->clearBySessId($sessid);
00202 return $this;
00203 }
00204
00205 public function hasAssigned2Role($user)
00206 {
00207 return $this->getResource()->hasAssigned2Role($user);
00208 }
00209
00210 protected function _getEncodedApiKey($apiKey)
00211 {
00212 return Mage::helper('core')->getHash($apiKey, 2);
00213 }
00214
00215 }