Răsfoiți Sursa

Ticket #1876 - point 1

AntonLV 7 ani în urmă
părinte
comite
3fbbc6a266

+ 1 - 0
modules/boonex/english/data/langs/system/en.xml

@@ -957,6 +957,7 @@
 	<string name="_adm_msg_modules_success_install"><![CDATA[The package was installed successfully]]></string>
 	<string name="_adm_msg_modules_success_uninstall"><![CDATA[The package was uninstalled successfully]]></string>
 	<string name="_adm_msg_oauth_need_authorize"><![CDATA[You need to to login into your <b>UNA</b> account to get data. Click <a href="{0}">here</a> to login.]]></string>
+        <string name="_adm_msg_oauth_non_owner_logged"><![CDATA[WARNING: You are logged in to an <a href="{0}">UNA.IO</a> account which is different from the account that generated UNA Key & Secret for your domain.]]></string>
 	<string name="_adm_nav_btn_current_icon_image_delete"><![CDATA[Delete]]></string>
 	<string name="_adm_nav_btn_items_add"><![CDATA[Add]]></string>
 	<string name="_adm_nav_btn_items_cancel"><![CDATA[Cancel]]></string>

+ 7 - 2
modules/boonex/oauth2/classes/BxOAuthAPI.php

@@ -75,8 +75,13 @@ class BxOAuthAPI extends BxDol
             $this->errorOutput('404', 'not_found', 'Profile was not found');
             return;
         }
-    
-        $this->output($this->_prepareProfileArray($oProfile, false));
+
+        $aClient = $this->_oDb->getClientsBy(array('type' => 'client_id', 'client_id' => $aToken['client_id']));
+
+        $aProfile = $this->_prepareProfileArray($oProfile, false);
+        $aProfile['owner'] = (int)$aProfile['id'] == (int)$aClient['user_id'];
+
+        $this->output($aProfile);
     }
 
     /**

+ 1 - 0
modules/boonex/russian/data/langs/system/ru.xml

@@ -955,6 +955,7 @@
 	<string name="_adm_msg_modules_success_install"><![CDATA[Пакет удачно установлен]]></string>
 	<string name="_adm_msg_modules_success_uninstall"><![CDATA[Пакет удачно отменён]]></string>
 	<string name="_adm_msg_oauth_need_authorize"><![CDATA[Вам нужно зайти в свой <b>UNA</b> аккаунт, чтобы запросить данные. Нажмите <a href="{0}">здесь</a> для входа.]]></string>
+        <string name="_adm_msg_oauth_non_owner_logged"><![CDATA[Заметьте. Вы вошли в систему, используя UNA аккаунт, который не является владельцем пары Key&Secret.]]></string>
 	<string name="_adm_nav_btn_current_icon_image_delete"><![CDATA[Удалить]]></string>
 	<string name="_adm_nav_btn_items_add"><![CDATA[Добавить]]></string>
 	<string name="_adm_nav_btn_items_cancel"><![CDATA[Отменить]]></string>

+ 26 - 14
studio/classes/BxDolStudioOAuth.php

@@ -11,12 +11,18 @@ bx_import('BxDolStudioInstallerUtils');
 
 class BxDolStudioOAuth extends BxDolFactory
 {
+    protected static $sSessionKeyToken = 'sys_oauth_token';
+    protected static $sSessionKeySecret = 'sys_oauth_secret';
+    protected static $sSessionKeyAuthorized = 'sys_oauth_authorized';
+    protected static $sSessionKeyAuthorizedUser = 'sys_oauth_authorized_user';
+    protected static $sSessionKeyAuthorizedOwner = 'sys_oauth_authorized_owner';
+
     protected $oSession;
 
-	protected $sErrorCode;
+    protected $sErrorCode;
     protected $sErrorMessage;
 
-	protected $sKey;
+    protected $sKey;
     protected $sSecret;
     protected $sDataRetrieveMethod;
 
@@ -32,18 +38,23 @@ class BxDolStudioOAuth extends BxDolFactory
 
     static function isAuthorizedClient()
     {
-        return (int)BxDolSession::getInstance()->getValue('sys_oauth_authorized') == 1;
+        return (int)BxDolSession::getInstance()->getValue(self::$sSessionKeyAuthorized) == 1;
     }
 
     static function getAuthorizedClient()
     {
-        return (int)BxDolSession::getInstance()->getValue('sys_oauth_authorized_user');
+        return (int)BxDolSession::getInstance()->getValue(self::$sSessionKeyAuthorizedUser);
+    }
+
+    static function isAuthorizedOwner()
+    {
+        return (int)BxDolSession::getInstance()->getValue(self::$sSessionKeyAuthorizedOwner) == 1;
     }
 
     public function loadItems($aParams = array())
     {
         if(empty($this->sKey) || empty($this->sSecret))
-            return _t('_adm_err_oauth_empty_key_secret');
+            return MsgBox(_t('_adm_err_oauth_empty_key_secret'));
 
         $mixedResult = $this->authorize();
         if($mixedResult !== true)
@@ -51,9 +62,9 @@ class BxDolStudioOAuth extends BxDolFactory
 
         $aItems = $this->fetch($aParams);
         if(is_null($aItems))
-            return _t('_adm_err_oauth_cannot_read_answer');
+            return MsgBox(_t('_adm_err_oauth_cannot_read_answer'));
         else if(empty($aItems))
-            return _t('_Empty');
+            return MsgBox(_t('_Empty'));
 
         return $aItems;
     }
@@ -61,7 +72,7 @@ class BxDolStudioOAuth extends BxDolFactory
     public function doAuthorize()
     {
         if(empty($this->sKey) || empty($this->sSecret))
-            return _t('_adm_err_oauth_empty_key_secret');
+            return MsgBox(_t('_adm_err_oauth_empty_key_secret'));
 
         $mixedResult = $this->authorize();
         if($mixedResult !== true)
@@ -96,12 +107,13 @@ class BxDolStudioOAuth extends BxDolFactory
     	return self::getAuthorizedClient();
     }
 
-	protected function unsetAuthorizedUser()
-	{
-		$this->oSession->unsetValue('sys_oauth_token');
-        $this->oSession->unsetValue('sys_oauth_authorized');
-		$this->oSession->unsetValue('sys_oauth_authorized_user');
-	}
+    protected function unsetAuthorizedUser()
+    {
+        $this->oSession->unsetValue(self::$sSessionKeyToken);
+        $this->oSession->unsetValue(self::$sSessionKeyAuthorized);
+        $this->oSession->unsetValue(self::$sSessionKeyAuthorizedUser);
+        $this->oSession->unsetValue(self::$sSessionKeyAuthorizedOwner);
+    }
 
     protected function isServerError($aResult)
     {

+ 15 - 15
studio/classes/BxDolStudioOAuthLib.php

@@ -46,9 +46,9 @@ class BxDolStudioOAuthLib extends BxDolStudioOAuthOAuth1 implements iBxDolSingle
 
         try {
             $bToken = bx_get('oauth_token') !== false;
-            $mixedSecret = $this->oSession->getValue('sys_oauth_secret');
+            $mixedSecret = $this->oSession->getValue(self::$sSessionKeySecret);
             if(!$bToken && $mixedSecret !== false) {
-                $this->oSession->unsetValue('sys_oauth_secret');
+                $this->oSession->unsetValue(self::$sSessionKeySecret);
                 $mixedSecret = false;
             }
 
@@ -79,7 +79,7 @@ class BxDolStudioOAuthLib extends BxDolStudioOAuthOAuth1 implements iBxDolSingle
 		if($this->isServerError($aRequestToken))
 			throw new OAuthException();
 
-		$this->oSession->setValue('sys_oauth_secret', $aRequestToken['oauth_token_secret']);
+		$this->oSession->setValue(self::$sSessionKeySecret, $aRequestToken['oauth_token_secret']);
 
 		$sUrl = bx_append_url_params(BX_DOL_OAUTH_URL_AUTHORIZE, array('oauth_token' => $aRequestToken['oauth_token'], 'sid' => bx_site_hash()));
 		return array(
@@ -90,20 +90,20 @@ class BxDolStudioOAuthLib extends BxDolStudioOAuthOAuth1 implements iBxDolSingle
 
     protected function getAccessToken($sToken, $mixedSecret, $sVerifier, $iUser,  $oConsumer)
     {
-		$oConsumer->setToken($sToken, $mixedSecret);
-		$aAccessToken = $oConsumer->getAccessToken(bx_append_url_params(BX_DOL_OAUTH_URL_ACCESS_TOKEN, array('oauth_verifier' => $sVerifier)));
-		if(empty($aAccessToken))
-			return _t('_adm_err_oauth_cannot_get_token');
+        $oConsumer->setToken($sToken, $mixedSecret);
+        $aAccessToken = $oConsumer->getAccessToken(bx_append_url_params(BX_DOL_OAUTH_URL_ACCESS_TOKEN, array('oauth_verifier' => $sVerifier)));
+        if(empty($aAccessToken))
+            return _t('_adm_err_oauth_cannot_get_token');
 
-		if($this->isServerError($aAccessToken))
-			throw new OAuthException();
+        if($this->isServerError($aAccessToken))
+            throw new OAuthException();
 
-		$this->oSession->setValue('sys_oauth_token', $aAccessToken['oauth_token']);
-		$this->oSession->setValue('sys_oauth_secret', $aAccessToken['oauth_token_secret']);
-		$this->oSession->setValue('sys_oauth_authorized', 1);
-		$this->oSession->setValue('sys_oauth_authorized_user', $iUser);
+        $this->oSession->setValue(self::$sSessionKeyToken, $aAccessToken['oauth_token']);
+        $this->oSession->setValue(self::$sSessionKeySecret, $aAccessToken['oauth_token_secret']);
+        $this->oSession->setValue(self::$sSessionKeyAuthorized, 1);
+        $this->oSession->setValue(self::$sSessionKeyAuthorizedUser, $iUser);
 
-		return true;
+        return true;
     }
     
     protected function fetch($aParams = array())
@@ -114,7 +114,7 @@ class BxDolStudioOAuthLib extends BxDolStudioOAuthOAuth1 implements iBxDolSingle
 		$oConsumer = $this->getServiceObject(false);
 
         try {
-            $oConsumer->setToken($this->oSession->getValue('sys_oauth_token'), $this->oSession->getValue('sys_oauth_secret'));
+            $oConsumer->setToken($this->oSession->getValue(self::$sSessionKeyToken), $this->oSession->getValue(self::$sSessionKeySecret));
             $oConsumer->fetch(BX_DOL_OAUTH_URL_FETCH_DATA, $aParams, $this->sDataRetrieveMethod);
 
             //echo $oConsumer->getLastResponse(); exit;	//--- Uncomment to debug

+ 5 - 5
studio/classes/BxDolStudioOAuthOAuth1.php

@@ -21,12 +21,12 @@ class BxDolStudioOAuthOAuth1 extends BxDolStudioOAuth
     	parent::__construct();
     }
 
-	protected function unsetAuthorizedUser()
-	{
-		$this->oSession->unsetValue('sys_oauth_secret');
+    protected function unsetAuthorizedUser()
+    {
+        $this->oSession->unsetValue(self::$sSessionKeySecret);
 
-		parent::unsetAuthorizedUser();
-	}
+        parent::unsetAuthorizedUser();
+    }
 }
 
 /** @} */

+ 11 - 10
studio/classes/BxDolStudioOAuthOAuth2.php

@@ -13,9 +13,9 @@ class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
     protected $sSessionKeyCsrfToken;
     protected $sSessionKeyCsrfTokenTime;
 
-	protected $sApiUrl;
-	protected $sScope;
-	protected $sPageHandle;
+    protected $sApiUrl;
+    protected $sScope;
+    protected $sPageHandle;
 
     protected function __construct()
     {
@@ -94,7 +94,7 @@ class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
             'grant_type'    => 'authorization_code',
             'code' => $sCode,
             'redirect_uri'  => $this->sPageHandle,
-    		'scope' => $this->sScope,
+            'scope' => $this->sScope,
         ), $this->sDataRetrieveMethod);
 
         if (!$sResponse || ($aResponse = json_decode($sResponse, true)) === NULL || !isset($aResponse['access_token']) || isset($aResponse['error']))
@@ -112,11 +112,12 @@ class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
         if (!$sResponse || ($aResponse = json_decode($sResponse, true)) === NULL || !$aResponse || isset($aResponse['error']))
             return isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
 
-		$this->oSession->setValue('sys_oauth_token', $sAccessToken);
-		$this->oSession->setValue('sys_oauth_authorized', 1);
-		$this->oSession->setValue('sys_oauth_authorized_user', $aResponse['id']);
+        $this->oSession->setValue(self::$sSessionKeyToken, $sAccessToken);
+        $this->oSession->setValue(self::$sSessionKeyAuthorized, 1);
+        $this->oSession->setValue(self::$sSessionKeyAuthorizedUser, (int)$aResponse['id']);
+        $this->oSession->setValue(self::$sSessionKeyAuthorizedOwner, (bool)$aResponse['owner'] ? 1 : 0);
 
-		return true;
+        return true;
     }
 
     protected function fetch($aParams = array())
@@ -131,8 +132,8 @@ class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
         }
 
         $sHttpCode = null;
-		$sResponse = bx_file_get_contents($this->sApiUrl . 'api/market', $aParams, 'get', array(
-            'Authorization: Bearer ' . $this->oSession->getValue('sys_oauth_token'),
+        $sResponse = bx_file_get_contents($this->sApiUrl . 'api/market', $aParams, 'get', array(
+            'Authorization: Bearer ' . $this->oSession->getValue(self::$sSessionKeyToken),
         ), $sHttpCode, array(), $iTimeout);
 
         //echo $sResponse; exit;		//--- Uncomment to debug

+ 7 - 7
studio/classes/BxDolStudioOAuthPlugin.php

@@ -61,9 +61,9 @@ class BxDolStudioOAuthPlugin extends BxDolStudioOAuthOAuth1 implements iBxDolSin
 
         try {
             $bToken = bx_get('oauth_token') !== false;
-            $mixedSecret = $this->oSession->getValue('sys_oauth_secret');
+            $mixedSecret = $this->oSession->getValue(self::$sSessionKeySecret);
             if(!$bToken && $mixedSecret !== false) {
-                $this->oSession->unsetValue('sys_oauth_secret');
+                $this->oSession->unsetValue(self::$sSessionKeySecret);
                 $mixedSecret = false;
             }
 
@@ -103,7 +103,7 @@ class BxDolStudioOAuthPlugin extends BxDolStudioOAuthOAuth1 implements iBxDolSin
 		if(empty($oToken))
 			return $sError;
 
-		$this->oSession->setValue('sys_oauth_secret', $oToken->getRequestTokenSecret());
+		$this->oSession->setValue(self::$sSessionKeySecret, $oToken->getRequestTokenSecret());
 
 		$oUrl = $oService->getAuthorizationUri(array('oauth_token' => $oToken->getRequestToken()));
 		$sUrl = bx_append_url_params($oUrl, array('sid' => bx_site_hash()));
@@ -137,10 +137,10 @@ class BxDolStudioOAuthPlugin extends BxDolStudioOAuthOAuth1 implements iBxDolSin
 		if(empty($oAccessToken))
 			return $sError;
 
-		$this->oSession->setValue('sys_oauth_token', $oAccessToken->getAccessToken());
-		$this->oSession->setValue('sys_oauth_secret', $oAccessToken->getAccessTokenSecret());
-		$this->oSession->setValue('sys_oauth_authorized', 1);
-		$this->oSession->setValue('sys_oauth_authorized_user', $iUser);
+		$this->oSession->setValue(self::$sSessionKeyToken, $oAccessToken->getAccessToken());
+		$this->oSession->setValue(self::$sSessionKeySecret, $oAccessToken->getAccessTokenSecret());
+		$this->oSession->setValue(self::$sSessionKeyAuthorized, 1);
+		$this->oSession->setValue(self::$sSessionKeyAuthorizedUser, $iUser);
 
 		return true;
     }

+ 5 - 1
studio/classes/BxDolStudioStore.php

@@ -23,6 +23,8 @@ class BxDolStudioStore extends BxTemplStudioPage
     protected $sStoreDataUrlPublic;
     protected $bAuthAccessUpdates;
 
+    protected $sSessionKeyNonOwnerNotified;
+
     function __construct($sPage = "")
     {
         parent::__construct('store');
@@ -34,6 +36,8 @@ class BxDolStudioStore extends BxTemplStudioPage
         $this->sStoreDataUrlPublic = BxDolStudioInstallerUtils::getInstance()->getStoreDataUrl();
         $this->bAuthAccessUpdates = false;
 
+        $this->sSessionKeyNonOwnerNotified = 'str_non_owner_notified';
+
         $this->sPage = BX_DOL_STUDIO_STR_TYPE_DEFAULT;
         if(is_string($sPage) && !empty($sPage))
             $this->sPage = $sPage;
@@ -363,7 +367,7 @@ class BxDolStudioStore extends BxTemplStudioPage
             $mixedParams = array('page' => $mixedParams);
 
         return bx_append_url_params($this->sBaseUrl, $mixedParams);
-    } 
+    }
 
     private function checkoutCart($iVendor)
     {

+ 29 - 5
studio/template/scripts/BxBaseStudioPage.php

@@ -311,16 +311,40 @@ class BxBaseStudioPage extends BxDolStudioPage
     }
 
     protected function getJsResult($sMessage, $bTranslate = true, $bRedirect = false, $sRedirect = '', $sOnResult = '')
+    {
+        return $this->getJsResultBy(array(
+            'message' => $sMessage,
+            'translate' => $bTranslate,
+            'redirect' => $bRedirect === true && !empty($sRedirect) ? $sRedirect : $bRedirect,
+            'eval' => $sOnResult
+        ));
+    }
+
+    protected function getJsResultBy($aParams)
     {
         $aResult = array();
-        $aResult['message'] = $bTranslate ? _t($sMessage) : $sMessage;
-        if($bRedirect)
-            $aResult['redirect'] = $sRedirect != '' ? $sRedirect : BX_DOL_URL_STUDIO;
 
-        if(!empty($sOnResult))
-            $aResult['eval'] = $sOnResult;
+        if(!empty($aParams['message'])) {
+            $aResult['message'] = $aParams['message'];
+
+            if(!isset($aParams['translate']) || !empty($aParams['translate'])) {
+                $aTrtParams = array($aResult['message']);
+                if(!empty($aParams['translate']) && is_array($aParams['translate']))
+                    $aTrtParams = array_merge($aTrtParams, $aParams['translate']);
+
+                $aResult['message'] = call_user_func_array ('_t', $aTrtParams);
+            }
+        }
+
+        if(isset($aParams['redirect']) && $aParams['redirect'] !== false)
+            $aResult['redirect'] = is_string($aParams['redirect']) ? $aParams['redirect'] : BX_DOL_URL_STUDIO;
+
+        if(!empty($aParams['eval']))
+            $aResult['eval'] = $aParams['eval'];
 
         $sResult = "window.parent.processJsonData(" . json_encode($aResult) . ");";
+        if(isset($aParams['on_page_load']) && $aParams['on_page_load'] === true)
+            $sResult = "$(document).ready(function() {" . $sResult . "});";
 
         return BxDolStudioTemplate::getInstance()->_wrapInTagJsCode($sResult);
     }

+ 35 - 16
studio/template/scripts/BxBaseStudioStore.php

@@ -100,30 +100,32 @@ class BxBaseStudioStore extends BxDolStudioStore
 
         $mixedResult = $this->authorizeClient();
         if($mixedResult === true) {
-	        $aProducts = $this->loadGoodies();
+            $aProducts = $this->loadGoodies();
 
-	        $sContent = "";
-	        $sContent .= $this->getBlocksLine(array(
+            $sContent = "";
+            $sContent .= $this->displayNonOwnerNotification();
+
+            $sContent .= $this->getBlocksLine(array(
                 array(
-    	            'caption' => '_adm_block_cpt_categories',
-                	'actions' => array(),
+                    'caption' => '_adm_block_cpt_categories',
+                        'actions' => array(),
                     'items' => $this->getCategoriesList(false)
                 ),
                 array(
-    	            'caption' => '_adm_block_cpt_tags',
-                	'actions' => array(),
+                    'caption' => '_adm_block_cpt_tags',
+                        'actions' => array(),
                     'items' => $this->getTagsList(false)
                 )
-            ));        
+            ));
 
-	        foreach($aProducts as $aBlock) {
-	            $aBlock['items'] = $oTemplate->parseHtmlByName('str_products.html', array(
-	                'list' => $this->displayProducts($aBlock['items']),
-	                'paginate' => ''
-	            ));
+            foreach($aProducts as $aBlock) {
+                $aBlock['items'] = $oTemplate->parseHtmlByName('str_products.html', array(
+                    'list' => $this->displayProducts($aBlock['items']),
+                    'paginate' => ''
+                ));
 
-	            $sContent .= $this->getBlockCode($aBlock);
-	        }
+                $sContent .= $this->getBlockCode($aBlock);
+            }
         }
         else
         	$sContent = $this->getBlockCode(array(
@@ -1030,6 +1032,23 @@ class BxBaseStudioStore extends BxDolStudioStore
         return BxDolStudioTemplate::getInstance()->parseHtmlByName('str_lbl_tags.html', $aTags);
     }
 
+    protected function displayNonOwnerNotification()
+    {
+        if(BxDolStudioOAuth::isAuthorizedOwner())
+            return;
+
+        $oSession = BxDolSession::getInstance();
+        if($oSession->getValue($this->sSessionKeyNonOwnerNotified) === true)
+            return;
+
+        $oSession->setValue($this->sSessionKeyNonOwnerNotified, true);
+        return $this->getJsResultBy(array(
+            'message' => '_adm_msg_oauth_non_owner_logged',
+            'translate' => array(BX_DOL_MARKET_URL_ROOT),
+            'on_page_load' => true
+        ));
+    }
+
     private function getDownloadedModules($bNamesOnly = true)
     {
 		$aModules = BxDolStudioInstallerUtils::getInstance()->getModules(false);
@@ -1037,7 +1056,7 @@ class BxBaseStudioStore extends BxDolStudioStore
         return $bNamesOnly ? array_keys($aModules) : $aModules;
     }
 
-	private function getVoteStars($aItem)
+    private function getVoteStars($aItem)
     {
         $aTmplVarsStars = array();
         for($i = (int)$aItem['rate_min']; $i <= (int)$aItem['rate_max']; $i++)