You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.5 KiB

6 years ago
  1. <?php
  2. namespace console\controllers;
  3. use Yii;
  4. use yii\console\Controller;
  5. use yii\helpers\Console;
  6. use yii\db\Query;
  7. use yii\helpers\ArrayHelper;
  8. use common\models\User;
  9. //use common\models\ars\AdminUser;
  10. /**
  11. * Description of RbacsetController
  12. *
  13. * @author blobt
  14. */
  15. class InitController extends Controller {
  16. public function actionIndex() {
  17. echo "index\n";
  18. }
  19. public function actionCreateAdmin($password = NULL) {
  20. $auth = Yii::$app->authManager;
  21. while ($password == NULL) {
  22. $password = $this->prompt("\n\n请输入admin用户密码(最少6位任意字符):");
  23. if (strlen($password) < 6) {
  24. $password = NULL;
  25. continue;
  26. }
  27. break;
  28. }
  29. $this->createAdmin($password);
  30. }
  31. /**
  32. * 数据迁移
  33. * @param sring $migrationPath 数据迁移代码目录
  34. */
  35. private function migrate($migrationPath = "@app/migrations") {
  36. $migrate = Yii::createObject("yii\console\controllers\MigrateController", ["action", $this]);
  37. $migrate->interactive = false;
  38. $migrate->migrationPath = $migrationPath;
  39. $migrate->runAction("up", []);
  40. }
  41. /**
  42. * TODO没有完善
  43. * 清空数据库
  44. */
  45. public function clearDb() {
  46. Yii::$app->db->createCommand("SET FOREIGN_KEY_CHECKS = 0;")->execute();
  47. $dbname = explode('=', explode(';', Yii::$app->db->dsn)[1])[1];
  48. $sql = "SELECT CONCAT('drop table ',table_name,';') FROM information_schema.`TABLES` WHERE table_schema='{$dbname}';";
  49. $sqls = Yii::$app->db->createCommand($sql)->queryColumn();
  50. foreach ($sqls as $dropSql) {
  51. Yii::$app->db->createCommand($dropSql)->execute();
  52. }
  53. }
  54. /**
  55. * 创建管理员
  56. * @param string $password 管理员密码
  57. */
  58. private function createAdmin($password) {
  59. $auth = Yii::$app->authManager;
  60. $model = User::findOne("username = 'admin");
  61. if (empty($model)) {
  62. $model = new User();
  63. }
  64. $model->username = "admin";
  65. $model->email = "admin@kcshop.store";
  66. $model->setPassword($password);
  67. $model->generateAuthKey();
  68. $model->status = User::STATUS_ACTIVE;
  69. $model->created_at = $model->updated_at = time();
  70. if ($model->save()) {
  71. //$oRole = $auth->getRole("超级管理员");
  72. //if ($auth->assign($oRole, $model->id)) {
  73. $this->stdout("Create admin success!\n", Console::FG_GREEN);
  74. //}
  75. }
  76. }
  77. }