Exemplo n.º 1
0
 /**
  * Invoke Latch lib for unpair admin account with Latch
  * 
  * @param Mage_Admin_Model_User $user
  * @return array
  */
 public function unpairAdmin($user = null)
 {
     $appId = $this->getApplicationId();
     $appSecret = $this->getSecretKey();
     $apiUrl = $this->getApiUrl();
     if (!empty($appId) && !empty($appSecret)) {
         require_once Mage::getBaseDir('lib') . '/Latch/latch.php';
         if ($apiUrl) {
             $api = new Latch($appId, $appSecret, $apiUrl);
         } else {
             $api = new Latch($appId, $appSecret);
         }
         $latchId = Mage::getModel('admin/user')->load($user->getId())->getData('latch_id');
         $apiResponse = $api->unpair($latchId);
         if ($latchId) {
             if ($apiResponse->getError() == NULL) {
                 $user->setData('latch_id', '');
                 $mustSave = Mage::getSingleton('core/session')->getAdminMustSave();
                 if ($mustSave) {
                     $user->save();
                 }
                 return array("status" => 1, "message" => $this->__("The account was unlinked with Latch successfully."));
             } else {
                 return array("status" => 0, "message" => $this->__("Couldn't unlink the account with Latch: ") . $this->__($apiResponse->getError()->getMessage()));
             }
         } else {
             return array("status" => 0, "message" => $this->__("There is no Latch Id to unlink."));
         }
     } else {
         return array("status" => 0, "message" => $this->__("The account wasn't unlinked with Latch. Please try again later."));
     }
 }
 static function wfPrefHook($user, &$preferences)
 {
     global $wgUser, $wgRequest, $wgOut;
     $user_id = "";
     $acc_id = "";
     $app_id = "";
     $secret = "";
     $error_msg = "";
     $pairResponse = null;
     # If app_id, secret, user_id and the account_id are already in the DB, we take them
     SpecialLatch::accDB_appsecret($app_id, $secret);
     SpecialLatch::accDB_useraccid($wgUser->getId(), $user_id, $acc_id);
     # We create a new Latch object from the Latch SDK
     $api = new Latch($app_id, $secret);
     # We print the Latch preferences
     SpecialLatch::drawUserPreferences($acc_id, $wgUser, $preferences);
     # If the Pair button is pressed, we try to pair the account
     if ($wgRequest->getCheck('latchTokBot')) {
         # CSRF protection
         if (!$wgUser->matchEditToken($wgRequest->getVal('hiddToken'))) {
             return;
         } else {
             $pair_token = $wgRequest->getText('latchTok');
             # Not empty or extrange characters
             if (empty($pair_token) || preg_match('/\\.([^\\.]*$)/', $pair_token)) {
                 throw new DBExpectedError(null, wfMsg('latch-error-pair'));
             } else {
                 $pairResponse = $api->pair($pair_token);
                 $responseData = $pairResponse->getData();
                 if (!empty($responseData)) {
                     $accountId = $responseData->{"accountId"};
                 }
                 # If everything is OK, we insert the data in the DB
                 if (!empty($accountId)) {
                     SpecialLatch::insDB_useraccid($wgUser, $accountId);
                 } elseif ($pairResponse->getError() == NULL) {
                     throw new DBExpectedError(null, wfMsg('default-error-pair'));
                 } else {
                     switch ($pairResponse->getError()->getCode()) {
                         case 205:
                             $error_msg = wfMsg('205-pair');
                             break;
                         case 206:
                             $error_msg = wfMsg('206-pair');
                             break;
                         case 401:
                             $error_msg = wfMsg('error-401');
                             break;
                         default:
                             $error_msg = wfMsg('default-error-pair');
                             break;
                     }
                     throw new DBExpectedError(null, $pairResponse->getError()->getCode() . " - " . $error_msg);
                 }
             }
         }
     }
     # If the Unpair button is pressed, we try to unpair the account
     if ($wgRequest->getCheck('latchUnpair')) {
         SpecialLatch::accDB_useraccid($wgUser->getId(), $user_id, $acc_id);
         # CSRF protection
         if (!$wgUser->matchEditToken($wgRequest->getVal('hiddToken'))) {
             return;
         } else {
             $pairResponse = $api->unpair($acc_id);
             # If Account ID is empty and no error fields are found, there are problems with the connection to the server
             if ($pairResponse->getError() == NULL) {
                 SpecialLatch::delDB_useraccid($wgUser);
             } else {
                 switch ($pairResponse->getError()->getCode()) {
                     case 201:
                         $error_msg = wfMsg('201-unpair');
                         break;
                     case 401:
                         $error_msg = wfMsg('error-401');
                         break;
                     default:
                         $error_msg = wfMsg('error-unpair');
                         break;
                 }
                 throw new DBExpectedError(null, $pairResponse->getError()->getCode() . " - " . $error_msg);
             }
         }
     }
     # We print the Latch preferences again to make sure that nothing strange happens
     SpecialLatch::drawUserPreferences($acc_id, $wgUser, $preferences);
     # Required return value of a hook function.
     return true;
 }