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: class Mage_Api_Model_Session extends Mage_Core_Model_Session_Abstract
35: {
36: public $sessionIds = array();
37: protected $_currentSessId = null;
38:
39: public function start($sessionName=null)
40: {
41:
42: $this->_currentSessId = md5(time() . uniqid('', true) . $sessionName);
43: $this->sessionIds[] = $this->getSessionId();
44: return $this;
45: }
46:
47: public function init($namespace, $sessionName=null)
48: {
49: if (is_null($this->_currentSessId)) {
50: $this->start();
51: }
52: return $this;
53: }
54:
55: public function getSessionId()
56: {
57: return $this->_currentSessId;
58: }
59:
60: public function setSessionId($sessId = null)
61: {
62: if (!is_null($sessId)) {
63: $this->_currentSessId = $sessId;
64: }
65: return $this;
66: }
67:
68: public function revalidateCookie()
69: {
70:
71: }
72:
73: public function clear() {
74: if ($sessId = $this->getSessionId()) {
75: try {
76: Mage::getModel('api/user')->logoutBySessId($sessId);
77: } catch (Exception $e) {
78: return false;
79: }
80: }
81: return true;
82: }
83:
84: public function login($username, $apiKey)
85: {
86: $user = Mage::getModel('api/user')
87: ->setSessid($this->getSessionId())
88: ->login($username, $apiKey);
89:
90: if ( $user->getId() && $user->getIsActive() != '1' ) {
91: Mage::throwException(Mage::helper('api')->__('Your account has been deactivated.'));
92: } elseif (!Mage::getModel('api/user')->hasAssigned2Role($user->getId())) {
93: Mage::throwException(Mage::helper('api')->__('Access denied.'));
94: } else {
95: if ($user->getId()) {
96: $this->setUser($user);
97: $this->setAcl(Mage::getResourceModel('api/acl')->loadAcl());
98: } else {
99: Mage::throwException(Mage::helper('api')->__('Unable to login.'));
100: }
101: }
102:
103: return $user;
104: }
105:
106: public function refreshAcl($user=null)
107: {
108: if (is_null($user)) {
109: $user = $this->getUser();
110: }
111: if (!$user) {
112: return $this;
113: }
114: if (!$this->getAcl() || $user->getReloadAclFlag()) {
115: $this->setAcl(Mage::getResourceModel('api/acl')->loadAcl());
116: }
117: if ($user->getReloadAclFlag()) {
118: $user->unsetData('api_key');
119: $user->setReloadAclFlag('0')->save();
120: }
121: return $this;
122: }
123:
124: 125: 126: 127: 128: 129: 130: 131:
132: public function isAllowed($resource, $privilege=null)
133: {
134: $user = $this->getUser();
135: $acl = $this->getAcl();
136:
137: if ($user && $acl) {
138: try {
139: if ($acl->isAllowed($user->getAclRole(), 'all', null)){
140: return true;
141: }
142: } catch (Exception $e) {}
143:
144: try {
145: return $acl->isAllowed($user->getAclRole(), $resource, $privilege);
146: } catch (Exception $e) {
147: return false;
148: }
149: }
150: return false;
151: }
152:
153: 154: 155: 156: 157:
158: public function isSessionExpired ($user)
159: {
160: if (!$user->getId()) {
161: return true;
162: }
163: $timeout = strtotime( now() ) - strtotime( $user->getLogdate() );
164: return $timeout > Mage::getStoreConfig('api/config/session_timeout');
165: }
166:
167:
168: public function isLoggedIn($sessId = false)
169: {
170: $userExists = $this->getUser() && $this->getUser()->getId();
171:
172: if (!$userExists && $sessId !== false) {
173: return $this->_renewBySessId($sessId);
174: }
175:
176: if ($userExists) {
177: Mage::register('isSecureArea', true, true);
178: }
179: return $userExists;
180: }
181:
182: 183: 184: 185: 186: 187:
188: protected function _renewBySessId ($sessId)
189: {
190: $user = Mage::getModel('api/user')->loadBySessId($sessId);
191: if (!$user->getId() || !$user->getSessid()) {
192: return false;
193: }
194:
195: if ($user->getSessid() == $sessId && !$this->isSessionExpired($user)) {
196: $this->setUser($user);
197: $this->setAcl(Mage::getResourceModel('api/acl')->loadAcl());
198:
199: $user->getResource()->recordLogin($user)
200: ->recordSession($user);
201:
202: return true;
203: }
204: return false;
205: }
206:
207: }
208: