public static function getUserInfo($model) { if ($model->author_id) { $userClass = \Yii::$app->getUser()->identityClass; $user = $userClass::findIdentity($model->author_id); return [$user->getAvatar(70, 70), $user->username]; } return [User::getDefaultAvatar(64, 64), $model->username]; }
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; }
?> <?php $form = ActiveForm::begin(["options" => ["class" => "comment-form"]]); ?> <div class="media" <?php echo $model->parent_id ?: "id='comment-form'"; ?> > <div class="media-left"> <a href="#"> <?php if (\Yii::$app->getUser()->isGuest) { ?> <img class="media-object" style ="width: 54px; height: 54px;" src="<?php echo \hass\user\models\User::getDefaultAvatar(64, 64); ?> " > <?php } else { ?> <img class="media-object" style ="width: 54px; height: 54px;" src="<?php echo \Yii::$app->getUser()->identity->getAvatar(70, 70); ?> " > <?php } ?> </a> </div> <div class="media-body">
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; }