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

#2863 API endpoint: account->Change password

RomanL 6 лет назад
Родитель
Сommit
306ae3e411

+ 18 - 1
inc/classes/BxDolAccount.php

@@ -233,7 +233,24 @@ class BxDolAccount extends BxDolFactory implements iBxDolSingleton
         }
         return false;
     }
-    
+    /**
+     * Change account password
+     * @param  string  $sPassword - new password
+     * @param  int  $iAccountId  - optional account id
+     * @return true on success or false on error
+     */
+    public function updatePassword($sPassword, $iAccountId = false)
+    {
+        $sSalt = genRndSalt();
+        $sPasswordHash = encryptUserPwd($sPassword, $sSalt);
+        $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
+        if((int)BxDolAccountQuery::getInstance()->updatePassword($sPasswordHash, $sSalt, $iId) > 0) {
+            bx_alert('account', 'edited', $iId, BxDolAccount::getInstance()->id(), array('action' => 'reset_password'));
+            $this->doAudit($iId, '_sys_audit_action_account_reset_password');
+            return true;
+        }
+        return false;
+    }
     /**
      * Set account phone
      * @param  string  $sPhone - phone number

+ 1 - 9
modules/boonex/accounts/classes/BxAccntGridAdministration.php

@@ -286,14 +286,8 @@ class BxAccntGridAdministration extends BxBaseModProfileGridAdministration
         if(!$oAccount)
             return echoJson(array());
 
-        $sPwd = genRndPwd();
-        $sSalt = genRndSalt();
-        $sPasswordHash = encryptUserPwd($sPwd, $sSalt);
-
         $aRes = array();
-        if((int)BxDolAccountQuery::getInstance()->updatePassword($sPasswordHash, $sSalt, $iId) > 0) {
-            bx_alert('account', 'edited', $iId, BxDolAccount::getInstance()->id(), array('action' => 'reset_password'));
-
+        if (BxDolAccount::getInstance()->updatePassword(genRndPwd(), $iId)){
             $sPopupId = $this->_oModule->_oConfig->getHtmlIds('password_popup');
             $sPopupTitle = _t('_bx_accounts_form_display_account_settings_password_popup');
             $sPopupContent = $this->_oModule->_oTemplate->parseHtmlByName('reset_password.html', array(
@@ -304,8 +298,6 @@ class BxAccntGridAdministration extends BxBaseModProfileGridAdministration
             ));
 
             $aRes = array('popup' => BxTemplStudioFunctions::getInstance()->popupBox($sPopupId, $sPopupTitle, $sPopupContent));
-            
-            $oAccount->doAudit($iId, '_sys_audit_action_account_reset_password');
         }
         else 
             $aRes = array('msg' => _t('_bx_accnt_grid_action_err_perform'));

+ 59 - 0
modules/boonex/api/classes/BxApiModule.php

@@ -21,6 +21,7 @@ class BxApiModule extends BxDolModule
         $a = parent::serviceGetSafeServices();
         return array_merge($a, array (
             'DeletePage' => '',
+            'ChangeAccountPassword' => '',
         ));
     }
 
@@ -191,6 +192,64 @@ class BxApiModule extends BxDolModule
 
         return array('code' => 200);
     }
+    
+    /**
+     * @page private_api API Private
+     * @section private_api_api_change_account_password /m/oauth2/com/change_account_password
+     * 
+     * Change account password
+     * 
+     * **Scopes:** 
+     * `api`
+     *
+     * **HTTP Method:** 
+     * `POST`
+     *
+     * **Request params:**
+     * - `password` - new password
+     *
+     * **Request header:**
+     * @code
+     * Authorization: Bearer 9802c4a34e1535d8c3b721604ee0e7fb04116c49
+     * @endcode
+     *
+     * **Response (success):**
+     * @code
+     * {  
+     *     "code": 200,
+     * }
+     * @endcode
+     *
+     * **Response (error):**
+     * @code
+     * {  
+     *    "error":"short error description here",
+     *    "error_description":"long error description here"
+     * }
+     * @endcode
+     */
+    public function serviceChangeAccountPassword()
+    {        
+        $sPassword = bx_get('password');
+        
+        if ($sPassword == '') {
+            return array(
+                'code' => 204,
+                'error' => 'No Content',
+                'desc' => 'Password can\'t be empty',
+            );
+        }
+        
+        $oAccount = BxDolAccount::getInstance();
+        if (!$oAccount->updatePassword($sPassword)){
+            return array(
+                'code' => 500,
+                'error' => 'Internal Error',
+            );
+        }
+
+        return array('code' => 200);
+    }
 }
 
 /** @} */