Просмотр исходного кода

#2818 Switch to Profile service method

Alex Trofimov 6 лет назад
Родитель
Сommit
480f9b4130
2 измененных файлов с 65 добавлено и 21 удалено
  1. 12 21
      template/scripts/BxBasePageAccount.php
  2. 53 0
      template/scripts/BxBaseServiceAccount.php

+ 12 - 21
template/scripts/BxBasePageAccount.php

@@ -53,38 +53,29 @@ class BxBasePageAccount extends BxTemplPage
 
         // switch profile context
         if ($iSwitchToProfileId = (int)bx_get('switch_to_profile')) {
+            $sInformerMsg = '';
             $oProfile = BxDolProfile::getInstance($iSwitchToProfileId);
-            $sInformerMsg = '';            
 
             if ($oProfile && BxDolService::call($oProfile->getModule(), 'act_as_profile')) {
-                $iViewerAccountId = getLoggedId();
-                $iSwitchToAccountId = $oProfile->getAccountId();
-                
-                $iViewerProfileId = bx_get_logged_profile_id();
-                $aCheck = checkActionModule($iViewerProfileId, 'switch to any profile', 'system', false);
-                $bAllowSwitchToAnyProfile = $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
-                
-                $bCanSwitch = ($iSwitchToAccountId == $iViewerAccountId || $bAllowSwitchToAnyProfile);
-                bx_alert('account', 'check_switch_context', $iSwitchToAccountId, $iViewerProfileId, array('switch_to_profile' => $iSwitchToProfileId, 'viewer_account' => $iViewerAccountId, 'override_result' => &$bCanSwitch));
 
-                if ($bCanSwitch) {
-                    $oAccount = BxDolAccount::getInstance();
-                    if ($oAccount->updateProfileContext($iSwitchToProfileId)) {
-                        checkActionModule($iViewerProfileId, 'switch to any profile', 'system', true);
-                        $sInformerMsg = _t('_sys_txt_account_profile_context_changed_success', $oProfile->getDisplayName());
-                        if ((int)bx_get('redirect_back') && isset($_SERVER['HTTP_REFERER']) && 0 === mb_stripos($_SERVER['HTTP_REFERER'], BX_DOL_URL_ROOT)) {
-                            header("Location:" . $_SERVER['HTTP_REFERER']);
-                            exit;
-                        }
+                $mixedRes = bx_srv('system','switch_profile',[$oProfile->id()], 'TemplServiceAccount');
+                if (true === $mixedRes) {
+                    $sInformerMsg = _t('_sys_txt_account_profile_context_changed_success', $oProfile->getDisplayName());
+                    if ((int)bx_get('redirect_back') && isset($_SERVER['HTTP_REFERER']) && 0 === mb_stripos($_SERVER['HTTP_REFERER'], BX_DOL_URL_ROOT)) {
+                        header("Location:" . $_SERVER['HTTP_REFERER']);
+                        exit;
                     }
                 }
+                else {
+                    $sInformerMsg = $mixedRes;
+                }
 
                 $oInformer = BxDolInformer::getInstance($this->_oTemplate);
                 if ($oInformer)
-                    $oInformer->add('sys-account-profile-context-change-result', $sInformerMsg ? $sInformerMsg : _t('_error occured'), $sInformerMsg ? BX_INFORMER_INFO : BX_INFORMER_ERROR);
+                    $oInformer->add('sys-account-profile-context-change-result', $sInformerMsg ? $sInformerMsg : _t('_error occured'), true === $mixedRes ? BX_INFORMER_INFO : BX_INFORMER_ERROR);
+
             }
         }
-
     }
 
     protected function _isVisiblePage ($a)

+ 53 - 0
template/scripts/BxBaseServiceAccount.php

@@ -445,6 +445,59 @@ class BxBaseServiceAccount extends BxDol
         return '<div class="bx-def-padding-sec-bottom">' . $sResultMsg . '</div>' . $sForm . ($bShowErrorEmptyBoth ? '<div class="bx-form-warn">' . _t("_sys_form_forgot_password_phone_and_email_empty_error") . '</div>' : '');
     }
 
+    /**
+     * @page service Service Calls
+     * @section bx_system_general System Services 
+     * @subsection bx_system_general-account Account
+     * @subsubsection bx_system_general-switch_profile switch_profile
+     * 
+     * @code bx_srv('system', 'switch_profile', [123], 'TemplServiceAccount'); @endcode
+     * @code {{~system:switch_profile:TemplServiceAccount:[123]~}} @endcode
+     * 
+     * Switch current user profile. 
+     * User can switch between profiles if multiple profiles are created.
+     * Also it's possible to switch to Organization profile if user is admin of Organization, 
+     * or other module which can `actAsProfile`.
+     * Admin can switch to any prifile by default
+     *
+     * @param $iSwitchToProfileId profile ID to switch to
+     * @return true on success or error message otherwise, so make sue to make strict (===) comparision
+     * 
+     * @see BxBaseServiceAccount::serviceSwitchProfile
+     */
+    /** 
+     * @ref bx_system_general-switch_profile "switch_profile"
+     */
+    public function serviceSwitchProfile($iSwitchToProfileId)
+    {
+        $oProfile = BxDolProfile::getInstance($iSwitchToProfileId);
+        if (!$oProfile) {
+            return _t('_sys_txt_error_occured');
+        }
+
+        $iViewerAccountId = getLoggedId();
+        $iSwitchToAccountId = $oProfile->getAccountId();
+        
+        $iViewerProfileId = bx_get_logged_profile_id();
+        $aCheck = checkActionModule($iViewerProfileId, 'switch to any profile', 'system', false);
+        $bAllowSwitchToAnyProfile = $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
+
+        $bCanSwitch = ($iSwitchToAccountId == $iViewerAccountId || $bAllowSwitchToAnyProfile);
+        bx_alert('account', 'check_switch_context', $iSwitchToAccountId, $iViewerProfileId, array('switch_to_profile' => $iSwitchToProfileId, 'viewer_account' => $iViewerAccountId, 'override_result' => &$bCanSwitch));
+
+        if (!$bCanSwitch) {
+            return isset($aCheck[CHECK_ACTION_MESSAGE]) ? $aCheck[CHECK_ACTION_MESSAGE] : _t('_sys_txt_error_occured'); 
+        } 
+
+        $oAccount = BxDolAccount::getInstance();
+        if (!$oAccount->updateProfileContext($iSwitchToProfileId)) {
+            return _t('_sys_txt_error_occured');
+        }
+
+        checkActionModule($iViewerProfileId, 'switch to any profile', 'system', true);
+        return true;
+    }
+
     /**
      * Reset password procedure
      */