-
-
Save theStrangeAdventurer/adc940944cedad531736d1679aaf12b6 to your computer and use it in GitHub Desktop.
БИТРИКС: класс компонента для смены пароля пользователя
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) die(); | |
use Bitrix\Main\Localization\Loc; | |
use Bitrix\Main\Application, Bitrix\Main\Context, Bitrix\Main\Request, Bitrix\Main\Server; | |
class ChangeForgotPassword extends CBitrixComponent | |
{ | |
const SEND_EVENT_TYPE = "YOUR_MAIL_EVENT_TYPE"; | |
const POST_TEMPLATE_ID = "YOUR_MAIL_EVENT_ID"; | |
protected $regexp = [ | |
'PHONE' => '~^\d[\d\(\)\ -]{4,14}\d$~', | |
'EMAIL' => '~^[\w-\.]+@[\w-]+\.[a-z]{2,3}$~i' | |
]; | |
protected $code = null; | |
protected $user = null; | |
public function onPrepareComponentParams($arParams) | |
{ | |
$this->arResult['ERRORS'] = []; | |
$this->fillPostData(); | |
$this->validateInputData(); | |
if (empty($this->arResult['ERRORS']) && ($this->user)) { | |
$this->code = $this->generateCode(); | |
$this->sendTempPasswd(); | |
} | |
return $arParams; | |
} | |
protected function sendTempPasswd() | |
{ | |
$result = null; | |
if ($this->arResult['POST']['FORGOT_TYPE'] == 'EMAIL') { | |
$result = $this->sendEmailCode(); | |
} else { | |
$result = $this->sendSmsCode(); | |
} | |
return $result; | |
} | |
protected function sendEmailCode() | |
{ | |
$arEventFields = [ | |
"MESSAGE" => 'bla bla', | |
"NAME" => 'test', | |
"LAST_NAME" => 'test last name', | |
"TEMP_PSWD" => $this->code | |
]; | |
\CEvent::Send( | |
self::SEND_EVENT_TYPE, | |
SITE_ID, | |
$arEventFields, | |
"N", | |
self::POST_TEMPLATE_ID | |
); | |
} | |
protected function sendSmsCode() { | |
// Нужны смс щлюзы | |
} | |
protected function generateCode($length = 6) | |
{ | |
$characters = '0123456789abcdefghijklmnopqrstuvwxyz'; | |
$charactersLength = strlen($characters); | |
$randomString = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$randomString .= $characters[rand(0, $charactersLength - 1)]; | |
} | |
return $randomString; | |
} | |
protected function getUserByEmail($email) | |
{ | |
$filter = [ | |
"EMAIL" => $email, | |
]; | |
$arParameters = [ | |
"FIELDS" => [ | |
"ID", | |
"NAME", | |
"LAST_NAME", | |
"LOGIN", | |
"PASSWORD", | |
"EMAIL" | |
] | |
]; | |
$cdbResult = \CUser::GetList($by = "ID", $order = "desc", $filter, $arParameters); | |
if ($cdbResult->SelectedRowsCount() !== 1) { | |
$this->user = false; | |
} | |
$this->user = $cdbResult->Fetch(); | |
} | |
protected function getUserByPhone($phone) | |
{ | |
$filter = [ | |
"PERSONAL_PHONE" => $phone, | |
]; | |
$arParameters = [ | |
"FIELDS" => [ | |
"ID", | |
"NAME", | |
"LAST_NAME", | |
"LOGIN", | |
"PASSWORD", | |
"EMAIL" | |
] | |
]; | |
$cdbResult = \CUser::GetList($by = "ID", $order = "desc", $filter, $arParameters); | |
if ($cdbResult->SelectedRowsCount() !== 1) { | |
$this->user = false; | |
} | |
$this->user = $cdbResult->Fetch(); | |
} | |
private function changeUserPassword() | |
{ | |
$user = new CUser(); | |
$result = $user->Update( | |
$this->user['ID'], | |
[ | |
'PASSWORD' => $this->requestParams['FORM']['PASSWORD'], | |
'CONFIRM_PASSWORD' => $this->requestParams['FORM']['CONFIRM'] | |
] | |
); | |
if (!$result) { | |
$this->errors[] = Loc::getMessage('CHANGING_PASSWORD_ERROR'); | |
} else { | |
ChangePassSecutiryKeysTable::delete(intval($this->key['ID'])); | |
} | |
return empty($this->errors); | |
} | |
protected function validateInputData() | |
{ | |
if ($this->arResult['POST']['FORGOT_TYPE'] === 'EMAIL') { | |
if (!$this->validate($this->regexp['EMAIL'], $this->arResult['POST']['USER_EMAIL'])) $this->arResult['ERRORS'][] = Loc::getMessage('INVALID_EMAIL'); | |
else $this->getUserByEmail($this->arResult['POST']['USER_EMAIL']); | |
} elseif ($this->arResult['POST']['FORGOT_TYPE'] === 'PHONE') { | |
if (!$this->validate($this->regexp['PHONE'], $this->arResult['POST']['USER_PHONE'])) $this->arResult['ERRORS'][] = Loc::getMessage('INVALID_PHONE'); | |
else $this->getUserByEmail($this->arResult['POST']['USER_PHONE']); | |
} | |
} | |
protected function validate($pattern, $fieldValue) | |
{ | |
if (preg_match($pattern, $fieldValue)) { | |
return true; | |
} | |
return false; | |
} | |
protected function fillPostData() | |
{ | |
$request = Context::getCurrent()->getRequest(); | |
if ($request->isPost()) { | |
foreach ($request->getPostList() as $key => $value) { | |
$this->arResult['POST'][htmlspecialcharsbx($key)] = htmlspecialcharsbx($value); | |
} | |
} | |
$this->arResult['POST']['FORGOT_TYPE'] = ($this->arResult['POST']['FORGOT_TYPE'] == 'EMAIL') ? 'EMAIL' : 'PHONE'; | |
} | |
public function executeComponent() | |
{ | |
$this->IncludeComponentTemplate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment