PasswordController.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace console\controllers;
  3. use Yii;
  4. use yii\helpers\Console;
  5. use yii\console\Controller;
  6. use common\models\member\Member;
  7. use common\helpers\StringHelper;
  8. /**
  9. * 密码初始化
  10. *
  11. * Class PasswordController
  12. * @package console\controllers
  13. */
  14. class PasswordController extends Controller
  15. {
  16. /**
  17. * 初始化
  18. *
  19. * @throws \yii\base\Exception
  20. */
  21. public function actionInit()
  22. {
  23. if ($model = Member::findOne(1)) {
  24. $password_hash = StringHelper::random(10);
  25. $model->username = StringHelper::random(5);
  26. $model->password_hash = Yii::$app->security->generatePasswordHash($password_hash);
  27. if (Member::findOne(['username' => $model->username])) {
  28. return $this->actionInit();
  29. }
  30. if ($model->save()) {
  31. Console::output('username: ' . $model->username);
  32. Console::output('password: ' . $password_hash);
  33. exit();
  34. }
  35. Console::stdout('Password initialization failed');
  36. exit();
  37. }
  38. Console::stdout('Cannot find administrator');
  39. exit();
  40. }
  41. }
粤ICP备19079148号