Skip to content

Commit b54dee7

Browse files
improved the password generation function
1 parent 91c0dec commit b54dee7

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

src/mail/password-reset-html.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
use ZakharovAndrew\user\Module;
5+
6+
$site_link = Yii::$app->urlManager->createAbsoluteUrl(['site/index']);
7+
?>
8+
9+
<div class="password-set">
10+
<h2><?= Module::t('Hello')?>, <?= Html::encode($user->getName()) ?></h2>
11+
<p><?= Module::t('Your data for access to')?> <?= Html::a(Html::encode($site_link), $site_link) ?>:</p>
12+
<p><?= Module::t('Login') ?>: <?= Html::encode($user->username) ?></p>
13+
<p><?= Module::t('Password')?>: <?= Html::encode($password) ?></p>
14+
<p><?= Module::t('We recommend changing your password') ?></p>
15+
</div>

src/models/User.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,6 @@ public function generateTelegramCode()
341341
{
342342
$this->telegram_code = md5(time().$this->username);
343343
}
344-
345-
346344

347345
/**
348346
* Find a user by email
@@ -407,20 +405,30 @@ public static function getUsersByStatus($status_id)
407405
}
408406

409407
/**
410-
* Generating a new password
411-
* @param int $length - new password length
412-
* @return string new password
408+
* Generates a random password.
409+
*
410+
* @param int $length The desired length of the password. Default is 10.
411+
* @param bool $use_special Whether to include special characters in the password. Default is false.
412+
* @return string The generated password.
413413
*/
414-
public static function genPassword($length = 10)
414+
public static function genPassword($length = 10, $use_special = false)
415415
{
416-
$chars = "qazxswedcvfrtgbnhyujmkiolp1234567890QAZXSWEDCVFRTGBNHYUJMKIOLP!@#$%&*?";
417-
$length = intval($length);
418-
$size = strlen($chars) - 1;
419-
$password = "";
420-
while($length--) {
421-
$password .= $chars[rand(0, $size)];
422-
}
416+
$lowercase = range('a', 'z');
417+
$uppercase = range('A', 'Z');
418+
$digits = range(0, 9);
423419

420+
// If special characters are to be used, create an array of special characters
421+
$special = $use_special ? ['!', '@', '#', '$', '%', '^', '&', '*'] : [];
422+
423+
// Merge all character arrays into one array for password generation
424+
$chars = array_merge($lowercase, $uppercase, $digits, $special);
425+
$password = '';
426+
427+
// Loop to generate each character of the password
428+
for ($i = 0; $i < intval($length); $i++) {
429+
$password .= $chars[random_int(0, count($chars) - 1)];
430+
}
431+
424432
return $password;
425433
}
426434

0 commit comments

Comments
 (0)