public function install($model)
 {
     $class = "m151209_185057_migration";
     require Yii::getAlias("@hass/install/migrations/" . $class . ".php");
     $migration = new $class();
     try {
         //yii2 迁移是在命令行下操作的。。会输出很多垃圾信息
         ob_start();
         if ($migration->up() == false) {
             ob_end_clean();
             $model->addError("username", "数据库迁移失败");
             return false;
         }
     } catch (\Exception $e) {
         ob_end_clean();
         $model->addError("username", "数据表已经存在,或者其他错误!");
         return false;
     }
     ob_end_clean();
     $data = \Yii::$app->getCache()->get("install-site-form");
     foreach ($data as $name => $value) {
         $config = new Config();
         $config->name = preg_replace_callback('/([a-z]*)([A-Z].*)/', function ($matches) {
             return $matches[1] . "." . strtolower($matches[2]);
         }, $name);
         $config->value = $value;
         $config->save();
     }
     $data = \Yii::$app->getCache()->get("install-admin-form");
     $user = new User();
     $user->setScenario("create");
     $user->email = $data['email'];
     $user->username = $data['username'];
     $user->password = $data['password'];
     $user->create();
     $connection = \Yii::$app->getDb();
     $connection->createCommand()->insert('{{%auth_assignment}}', ['item_name' => 'admin', 'user_id' => $user->id, "created_at" => time()])->execute();
     Module::getInstance()->setCookieValidationKey();
     Module::getInstance()->setInstalled();
     \Yii::$app->getCache()->flush();
     return true;
 }
Пример #2
0
 public function createAdminUser()
 {
     $data = \Yii::$app->getCache()->get(AdminForm::CACHE_KEY);
     $user = new User();
     $user->setScenario("create");
     $user->email = $data["email"];
     $user->username = $data["username"];
     $user->password = $data["password"];
     if ($user->create() == false) {
         return $user->formatErrors();
     }
     //添加管理员权限
     $connection = \Yii::$app->getDb();
     $connection->createCommand()->insert('{{%auth_assignment}}', ['item_name' => 'admin', 'user_id' => $user->id, "created_at" => time()])->execute();
     return null;
 }