Skip to content

Commit 280a946

Browse files
Added bulk password reset for users
1 parent 34597e6 commit 280a946

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

src/controllers/UserController.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,6 @@ public function actionAdminResetPassword($id)
204204
Yii::$app->session->setFlash('error', Module::t('There was an error during the password reset'));
205205
return $this->redirect(Url::previous('adm_user_index') ?? ['adm-user/index']);
206206
}
207-
208-
echo $password;die();
209207
}
210208

211209
/**
@@ -506,12 +504,26 @@ public function actionUsersUpdate()
506504
unset($model);
507505
}
508506
Yii::$app->session->setFlash('success', Module::t('Roles added'));
509-
} else {
507+
} else if ($action == 'Update Status') {
510508
foreach ($users as $user) {
511509
$user->status = $status;
512510
$user->save();
513511
}
514512
Yii::$app->session->setFlash('success', Module::t('Statuses changed'));
513+
} else {
514+
$cntSuccess = 0;
515+
$cntError = 0;
516+
foreach ($users as $user) {
517+
$password = User::genPassword();
518+
519+
if ($user->setPassword($password) && $user->save() && $user->sendPasswordEmail($password, 'reset')) {
520+
$cntSuccess++;
521+
} else {
522+
$cntError++;
523+
}
524+
}
525+
526+
Yii::$app->session->setFlash('info', Module::t('Successfully reset passwords').': <b>'.$cntSuccess .'</b><br> '.Module::t('Password reset errors').': '.$cntError);
515527
}
516528

517529

src/messages/ru/user.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
'Delete' => 'Удалить',
8484
'Send' => 'Отправить',
8585
'Close' => 'Закрыть',
86+
'Cancel' => 'Отмена',
8687
'Action' => 'Действие',
8788
'Select Status' => 'Выберите статус',
8889
'Change Status' => 'Изменить статус',
@@ -91,6 +92,10 @@
9192
'User created' => 'Пользователь создан',
9293
'A new password has been set and sent to' => 'Новый пароль установлен и выслан на',
9394
'There was an error during the password reset' => 'Произошла ошибка при сбросе пароля',
95+
'Are you sure you want to reset the passwords for the selected users?' => 'Вы уверены, что хотите сбросить пароли для выбранных пользователей?',
96+
'Processing, please wait..' => 'Идет обработка, пожалуйста, подождите...',
97+
'Successfully reset passwords' => 'Успешно сброшено паролей',
98+
'Password reset errors' => 'Ошибок сброса пароля',
9499

95100
'Select Role' => 'Выберите роль',
96101
'Roles added' => 'Роли добавлены',

src/views/user/index.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@
2828

2929
$toggleUrl = Url::to(['/user/user/toggle-column-visibility']);
3030
$language = \Yii::$app->language;
31+
$waitMessage = Module::t('Processing, please wait..');
3132

3233
$script = <<< JS
3334
$('#users-update-status').on('click', function() {
3435
$('#status-modal').modal('show');
3536
});
36-
$('#users-add-role').on('click', function() {
37-
$('#role-modal').modal('show');
37+
$('#submit-reset-password').on('click', function() {
38+
$('#reset-password-modal .modal-body').text('$waitMessage');
3839
});
40+
3941
$('.toggleColumn').on('click', function() {
4042
let column = $(this).data('column');
4143
let visibility = $(this).data('visibility');
@@ -238,13 +240,26 @@ function updateSelectedCount() {
238240
<div style="display:flex">
239241
<?= Html::a(Module::t('Create User'), ['create'], ['class' => 'btn btn-success']) ?>
240242
<div id="selected-block">
241-
<p id="selected-count">Выбрано 0 пользователей</p>
243+
<p id="selected-count"></p>
242244
<?= $classButtonDropdown::widget([
243245
'label' => Module::t('Action'),
244246
'dropdown' => [
245247
'items' => [
246248
'<a href="#" id="users-update-status" class="dropdown-item">'.Module::t('Change Status').'</a>',
247-
'<a href="#" id="users-add-role" class="dropdown-item">'.Module::t('Add Role').'</a>',
249+
Html::a(Module::t('Add Role'), '#', [
250+
'class' => 'dropdown-item',
251+
'data' => [
252+
'bs-toggle' => 'modal',
253+
'bs-target' => '#role-modal',
254+
],
255+
]),
256+
Html::a(Module::t('Reset password'), '#', [
257+
'class' => 'dropdown-item',
258+
'data' => [
259+
'bs-toggle' => 'modal',
260+
'bs-target' => '#reset-password-modal',
261+
],
262+
]),
248263
],
249264
],
250265
]);?>
@@ -371,18 +386,19 @@ function updateSelectedCount() {
371386
<?php $classModal::begin([
372387
'id' => 'status-modal',
373388
($bootstrapVersion==3 ? 'header' : 'title') => '<h2>'.Module::t('Select Status').'</h2>',
374-
'footer' => '<button type="button" class="btn btn-default" data-dismiss="modal">' . Module::t('Close') . '</button>' .
389+
'footer' => '<button type="button" class="btn btn-default" data-dismiss="modal" data-bs-dismiss="modal">' . Module::t('Close') . '</button>' .
375390
Html::submitButton(Module::t('Update Status'), ['name' => 'form-action', 'value' => 'Update Status','class' => 'btn btn-primary'])
376391
]) ?>
377392

378393
<?= Html::dropDownList('status', null, User::getStatusList(), ['class' => 'form-control form-select']) ?>
379394

380395
<?php $classModal::end() ?>
381396

397+
<!-- Modal form for role selection -->
382398
<?php $classModal::begin([
383399
'id' => 'role-modal',
384400
($bootstrapVersion==3 ? 'header' : 'title') => '<h2>'.Module::t('Add Role').'</h2>',
385-
'footer' => '<button type="button" class="btn btn-default" data-dismiss="modal">' . Module::t('Close') . '</button>' .
401+
'footer' => '<button type="button" class="btn btn-default" data-dismiss="modal" data-bs-dismiss="modal">' . Module::t('Close') . '</button>' .
386402
Html::submitButton(Module::t('Add Role'), ['name' => 'form-action', 'value' => 'Add Role', 'class' => 'btn btn-primary'])
387403
]) ?>
388404
<div class="form-group">
@@ -399,6 +415,21 @@ function updateSelectedCount() {
399415
</div>
400416
<?php $classModal::end() ?>
401417

418+
<!-- Modal form for confirming password reset for users -->
419+
<?php $classModal::begin([
420+
'id' => 'reset-password-modal',
421+
($bootstrapVersion==3 ? 'header' : 'title') => '<h2>'.Module::t('Reset password').'</h2>',
422+
'footer' => ''
423+
424+
]) ?>
425+
426+
<p style="text-align:center"><?= Module::t('Are you sure you want to reset the passwords for the selected users?') ?></p>
427+
<div style="display:flex;justify-content: center;gap:4px;">
428+
<?= Html::submitButton(Module::t('Reset password'), ['name' => 'form-action', 'value' => 'Reset Password','class' => 'btn btn-danger']) ?>
429+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" data-dismiss="modal"><?= Module::t('Cancel') ?></button>
430+
</div>
431+
<?php $classModal::end() ?>
432+
402433
<?= Html::endForm() ?>
403434

404435

0 commit comments

Comments
 (0)