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: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56:
57: class Mage_Api_Model_User extends Mage_Core_Model_Abstract
58: {
59: 60: 61: 62: 63:
64: protected $_eventPrefix = 'api_user';
65:
66: protected function _construct()
67: {
68: $this->_init('api/user');
69: }
70:
71: public function save()
72: {
73: $this->_beforeSave();
74: $data = array(
75: 'firstname' => $this->getFirstname(),
76: 'lastname' => $this->getLastname(),
77: 'email' => $this->getEmail(),
78: 'modified' => Mage::getSingleton('core/date')->gmtDate()
79: );
80:
81: if($this->getId() > 0) {
82: $data['user_id'] = $this->getId();
83: }
84:
85: if( $this->getUsername() ) {
86: $data['username'] = $this->getUsername();
87: }
88:
89: if ($this->getApiKey()) {
90: $data['api_key'] = $this->_getEncodedApiKey($this->getApiKey());
91: }
92:
93: if ($this->getNewApiKey()) {
94: $data['api_key'] = $this->_getEncodedApiKey($this->getNewApiKey());
95: }
96:
97: if ( !is_null($this->getIsActive()) ) {
98: $data['is_active'] = intval($this->getIsActive());
99: }
100:
101: $this->setData($data);
102: $this->_getResource()->save($this);
103: $this->_afterSave();
104: return $this;
105: }
106:
107: public function delete()
108: {
109: $this->_beforeDelete();
110: $this->_getResource()->delete($this);
111: $this->_afterDelete();
112: return $this;
113: }
114:
115: public function saveRelations()
116: {
117: $this->_getResource()->_saveRelations($this);
118: return $this;
119: }
120:
121: public function getRoles()
122: {
123: return $this->_getResource()->_getRoles($this);
124: }
125:
126: public function deleteFromRole()
127: {
128: $this->_getResource()->deleteFromRole($this);
129: return $this;
130: }
131:
132: public function roleUserExists()
133: {
134: $result = $this->_getResource()->roleUserExists($this);
135: return ( is_array($result) && count($result) > 0 ) ? true : false;
136: }
137:
138: public function add()
139: {
140: $this->_getResource()->add($this);
141: return $this;
142: }
143:
144: public function userExists()
145: {
146: $result = $this->_getResource()->userExists($this);
147: return ( is_array($result) && count($result) > 0 ) ? true : false;
148: }
149:
150: public function getCollection() {
151: return Mage::getResourceModel('api/user_collection');
152: }
153:
154: public function getName($separator=' ')
155: {
156: return $this->getFirstname().$separator.$this->getLastname();
157: }
158:
159: public function getId()
160: {
161: return $this->getUserId();
162: }
163:
164: 165: 166: 167: 168:
169: public function getAclRole()
170: {
171: return 'U'.$this->getUserId();
172: }
173:
174: 175: 176: 177: 178: 179: 180:
181: public function authenticate($username, $apiKey)
182: {
183: $this->loadByUsername($username);
184: if (!$this->getId()) {
185: return false;
186: }
187: $auth = Mage::helper('core')->validateHash($apiKey, $this->getApiKey());
188: if ($auth) {
189: return true;
190: } else {
191: $this->unsetData();
192: return false;
193: }
194: }
195:
196: 197: 198: 199: 200: 201: 202:
203: public function login($username, $apiKey)
204: {
205: $sessId = $this->getSessid();
206: if ($this->authenticate($username, $apiKey)) {
207: $this->setSessid($sessId);
208: $this->getResource()->cleanOldSessions($this)
209: ->recordLogin($this)
210: ->recordSession($this);
211: Mage::dispatchEvent('api_user_authenticated', array(
212: 'model' => $this,
213: 'api_key' => $apiKey,
214: ));
215: }
216:
217: return $this;
218: }
219:
220: public function reload()
221: {
222: $this->load($this->getId());
223: return $this;
224: }
225:
226: public function loadByUsername($username)
227: {
228: $this->setData($this->getResource()->loadByUsername($username));
229: return $this;
230: }
231:
232: public function loadBySessId ($sessId)
233: {
234: $this->setData($this->getResource()->loadBySessId($sessId));
235: return $this;
236: }
237:
238: public function logoutBySessId($sessid)
239: {
240: $this->getResource()->clearBySessId($sessid);
241: return $this;
242: }
243:
244: public function hasAssigned2Role($user)
245: {
246: return $this->getResource()->hasAssigned2Role($user);
247: }
248:
249: protected function _getEncodedApiKey($apiKey)
250: {
251: return Mage::helper('core')->getHash($apiKey, 2);
252: }
253:
254: }
255: