Skip to content

Commit e5d8f88

Browse files
added the ability for the administrator to reset user passwords
1 parent b54dee7 commit e5d8f88

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

src/controllers/UserController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,23 @@ public function actionResetPassword($token)
191191
]);
192192
}
193193

194+
public function actionAdminResetPassword($id)
195+
{
196+
$user = $this->findModel($id);
197+
198+
$password = User::genPassword();
199+
200+
if ($user->setPassword($password) && $user->save() && $user->sendPasswordEmail($password, 'reset')) {
201+
Yii::$app->session->setFlash('success', Module::t('A new password has been set and sent to') .' '. $user->email);
202+
return $this->redirect(Url::previous('user_index') ?? ['index']);
203+
} else {
204+
Yii::$app->session->setFlash('error', Module::t('There was an error during the password reset'));
205+
return $this->redirect(Url::previous('adm_user_index') ?? ['adm-user/index']);
206+
}
207+
208+
echo $password;die();
209+
}
210+
194211
/**
195212
* Change email
196213
*

src/mail/password-reset-html.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@
1111
<p><?= Module::t('Your data for access to')?> <?= Html::a(Html::encode($site_link), $site_link) ?>:</p>
1212
<p><?= Module::t('Login') ?>: <?= Html::encode($user->username) ?></p>
1313
<p><?= Module::t('Password')?>: <?= Html::encode($password) ?></p>
14-
<p><?= Module::t('We recommend changing your password') ?></p>
1514
</div>

src/messages/ru/user.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,18 @@
7979
'Create User' => 'Создать пользователя',
8080
'Save' => 'Сохранить',
8181
'Update' => 'Изменить',
82+
'Edit' => 'Редактировать',
83+
'Delete' => 'Удалить',
8284
'Send' => 'Отправить',
8385
'Close' => 'Закрыть',
86+
'Action' => 'Действие',
8487
'Select Status' => 'Выберите статус',
8588
'Change Status' => 'Изменить статус',
8689
'Update Status' => 'Обновить статус',
8790
'Statuses changed' => 'Статусы изменены',
8891
'User created' => 'Пользователь создан',
92+
'A new password has been set and sent to' => 'Новый пароль установлен и выслан на',
93+
'There was an error during the password reset' => 'Произошла ошибка при сбросе пароля',
8994

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

src/views/user/index.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
use ZakharovAndrew\user\Module;
99
use ZakharovAndrew\user\models\UserRoles;
1010
use ZakharovAndrew\user\models\Roles;
11+
use yii\bootstrap\ButtonDropdown;
1112

1213
use ZakharovAndrew\user\assets\UserAssets;
1314
UserAssets::register($this);
1415

1516
$bootstrapVersion = Yii::$app->getModule('user')->bootstrapVersion;
1617
$classModal = "\\yii\bootstrap".($bootstrapVersion==3 ? '' : $bootstrapVersion)."\\Modal";
18+
$classButtonDropdown = "\\yii\bootstrap".($bootstrapVersion==3 ? '' : $bootstrapVersion)."\\ButtonDropdown";
1719

1820
/** @var yii\web\View $this */
1921
/** @var ZakharovAndrew\user\models\UserSearch $searchModel */
@@ -279,11 +281,29 @@
279281

280282
}
281283
],
284+
[
285+
286+
'format' => 'raw',
287+
'value' => function ($model) use ($classButtonDropdown) {
282288

283-
//
284-
//'updated_at',
289+
290+
return $classButtonDropdown::widget([
291+
'label' => Module::t('Action'),
292+
'dropdown' => [
293+
'items' => [
294+
['label' => Module::t('Profile'), 'url' => Url::toRoute(['profile', 'id' => $model->id])],
295+
['label' => Module::t('Edit'), 'url' => Url::toRoute(['edit-profile', 'id' => $model->id])],
296+
['label' => Module::t('Delete'), 'url' => Url::toRoute(['delete', 'id' => $model->id])],
297+
'<div class="dropdown-divider"></div>',
298+
['label' => Module::t('Appreciation'), 'url' => Url::toRoute(['/user/thanks/view', 'id' => $model->id])],
299+
['label' => Module::t('Reset password'), 'url' => Url::toRoute(['admin-reset-password', 'id' => $model->id])],
300+
],
301+
],
302+
]);
303+
}
304+
],
285305

286-
[
306+
/*[
287307
'class' => ActionColumn::className(),
288308
'urlCreator' => function ($action, $model, $key, $index, $column) {
289309
if ($action == 'view') {
@@ -294,7 +314,7 @@
294314
}
295315
return Url::toRoute([$action, 'id' => $model->id]);
296316
}
297-
],
317+
],*/
298318
],
299319
]); ?>
300320

0 commit comments

Comments
 (0)