/** * Clean up old authorized tokens for specified consumer-user pairs * * @param Mage_Oauth_Model_Token $exceptToken Token just created to exclude from delete * @return int The number of affected rows */ public function cleanOldAuthorizedTokensExcept(Mage_Oauth_Model_Token $exceptToken) { if (!$exceptToken->getId() || !$exceptToken->getAuthorized()) { Mage::throwException('Invalid token to except'); } $adapter = $this->_getWriteAdapter(); $where = $adapter->quoteInto('authorized = 1 AND consumer_id = ?', $exceptToken->getConsumerId(), Zend_Db::INT_TYPE); $where .= $adapter->quoteInto(' AND entity_id <> ?', $exceptToken->getId(), Zend_Db::INT_TYPE); if ($exceptToken->getCustomerId()) { $where .= $adapter->quoteInto(' AND customer_id = ?', $exceptToken->getCustomerId(), Zend_Db::INT_TYPE); } elseif ($exceptToken->getAdminId()) { $where .= $adapter->quoteInto(' AND admin_id = ?', $exceptToken->getAdminId(), Zend_Db::INT_TYPE); } else { Mage::throwException('Invalid token to except'); } return $adapter->delete($this->getMainTable(), $where); }
/** * Send email notification to user about token status change * * @param Mage_Oauth_Model_Token $token Token object * @param string $newStatus Name of new token status */ protected function _sendTokenStatusChangeNotification($token, $newStatus) { if ($adminId = $token->getAdminId()) { /** @var $session Mage_Admin_Model_Session */ $session = Mage::getSingleton('admin/session'); /** @var $admin Mage_Admin_Model_User */ $admin = $session->getUser(); if ($admin->getId() == $adminId) { // skip own tokens return; } $email = $admin->getEmail(); $name = $admin->getName(' '); } else { /** @var $customer Mage_Customer_Model_Customer */ $customer = Mage::getModel('customer/customer'); $customer->load($token->getCustomerId()); $email = $customer->getEmail(); $name = $customer->getName(); } /** @var $helper Mage_Oauth_Helper_Data */ $helper = Mage::helper('oauth'); $helper->sendNotificationOnTokenStatusChange($email, $name, $token->getConsumer()->getName(), $newStatus); }
/** * Process request for temporary (initiative) token */ public function initiateToken() { try { $this->_processRequest(self::REQUEST_INITIATE); $response = $this->_token->toString() . '&oauth_callback_confirmed=true'; } catch (Exception $e) { $response = $this->reportProblem($e); } $this->_getResponse()->setBody($response); }
/** * Decorate user type column * * @param string $value * @param Mage_Oauth_Model_Token $row * @param Mage_Adminhtml_Block_Widget_Grid_Column $column * @param bool $isExport * @return mixed */ public function decorateUserId($value, $row, $column, $isExport) { $value = $row->getCustomerId() ? $row->getCustomerId() : $row->getAdminId(); $cell = $value; return $cell; }
/** * Return complete callback URL or boolean FALSE if no callback provided * * @param Mage_Oauth_Model_Token $token Token object * @param bool $rejected OPTIONAL Add user reject sign * @return bool|string */ public function getFullCallbackUrl(Mage_Oauth_Model_Token $token, $rejected = false) { $callbackUrl = $token->getCallbackUrl(); if (Mage_Oauth_Model_Server::CALLBACK_ESTABLISHED == $callbackUrl) { return false; } if ($rejected) { /** @var $consumer Mage_Oauth_Model_Consumer */ $consumer = Mage::getModel('oauth/consumer')->load($token->getConsumerId()); if ($consumer->getId() && $consumer->getRejectedCallbackUrl()) { $callbackUrl = $consumer->getRejectedCallbackUrl(); } } elseif (!$token->getAuthorized()) { Mage::throwException('Token is not authorized'); } $callbackUrl .= false === strpos($callbackUrl, '?') ? '?' : '&'; $callbackUrl .= 'oauth_token=' . $token->getToken() . '&'; $callbackUrl .= $rejected ? self::QUERY_PARAM_REJECTED . '=1' : 'oauth_verifier=' . $token->getVerifier(); return $callbackUrl; }
/** * Get delete link * * @param Mage_Oauth_Model_Token $model * @return string */ public function getDeleteLink(Mage_Oauth_Model_Token $model) { return Mage::getUrl('oauth/customer_token/delete/', array('id' => $model->getId())); }
/** * Send the registration data to Styla and request module configuration * * @param array $loginData * @param Mage_Oauth_Model_Consumer $consumer * @param Mage_Oauth_Model_Token $token * @param $scopeData * @return stdClass * @throws Exception * @throws Styla_Connect_Exception */ public function sendRegistrationRequest($loginData, $consumer, $token, $scopeData) { //at this point we have all the login data we need for styla to access our api $stylaApi = Mage::getSingleton('styla_connect/styla_api'); //make the api request to styla api $apiRequest = $stylaApi->getRequest(Styla_Connect_Model_Styla_Api::REQUEST_TYPE_REGISTER_MAGENTO_API); $apiRequest->setConnectionType(Zend_Http_Client::POST); $apiRequest->setParams(array('styla_email' => $loginData['email'], 'styla_password' => $loginData['password'], 'consumer_key' => $consumer->getKey(), 'consumer_secret' => $consumer->getSecret(), 'token_key' => $token->getToken(), 'token_secret' => $token->getSecret())); $apiResponse = $stylaApi->callService($apiRequest, false); if (!$apiResponse->isOk()) { throw new Exception("Couldn't connect to Styla API. Error result: " . $apiResponse->getHttpStatus() . ($apiResponse->getError() ? ' - ' . $apiResponse->getError() : '')); } //setup the api urls for this client /** @var array $connectionData */ $connectionData = $apiResponse->getResult(); return $connectionData; }
/** * Send the registration data to Styla and request module configuration * * @param array $loginData * @param Mage_Oauth_Model_Consumer $consumer * @param Mage_Oauth_Model_Token $token * @return stdClass * @throws Exception */ public function sendRegistrationRequest($loginData, $consumer, $token) { //at this point we have all the login data we need for styla to access our api $stylaApi = Mage::getSingleton('styla_connect/styla_api'); //make the api request to styla api $apiRequest = $stylaApi->getRequest(Styla_Connect_Model_Styla_Api::REQUEST_TYPE_REGISTER_MAGENTO_API); $apiRequest->setConnectionType(Zend_Http_Client::POST); $apiRequest->setParams(array('styla_email' => $loginData['email'], 'styla_password' => $loginData['password'], 'consumer_key' => $consumer->getKey(), 'consumer_secret' => $consumer->getSecret(), 'token_key' => $token->getToken(), 'token_secret' => $token->getSecret())); $apiResponse = $stylaApi->callService($apiRequest, false); if (!$apiResponse->isOk()) { throw new Exception("Couldn't connect to Styla API. Error result: " . $apiResponse->getHttpStatus() . ($apiResponse->getError() ? " - " . $apiResponse->getError() : "")); } //setup the api urls for this client /** @var array $connectionData */ $connectionData = $apiResponse->getResult(); /** * store the result data, so we don't have to call the api anymore in the future (on every change of the module's operating mode * the configuration gets overwritten, so otherwise we'd have to call the api every time we change it) */ $this->cacheConnectionData($connectionData, Mage::helper('styla_connect/config')->getMode()); return $connectionData; }