Yii2:如何手动添加用户
Yii2: How to add users manually
可以从数据库手动添加用户吗?我得到了一个用这个框架做的项目,但客户不记得管理员的访问代码,我已经尝试手动创建用户,但我不知道如何加密密码。
手动添加
是的,您也可以从数据库中手动添加用户,您只需要使用以下行生成的加密密码
echo Yii::$app->security->generatePasswordHash('Your_password');
现在您可以将上面的行复制粘贴到现有的视图文件中,然后复制输出字符串并在用户 table password
字段中另存为密码。
使用迁移
或 create a migration 并通过该迁移添加管理员用户以快速修复您可以将以下迁移复制粘贴到您的 migrations
文件夹中,它应该位于 basic-app
的根目录中或 console/migrations
表示 advance-app
.
注意:更改列 and/or table 名称,如果它们与您的情况不同,我使用了 user
和 profile
table 名称
<?php
// @codingStandardsIgnoreStart
use yii\db\Migration;
/**
* Class m180322_183353_add_admin_user
*/
class m180322_183353_add_admin_user extends Migration
{
// @codingStandardsIgnoreEnd
/**
* Table name
*
* @var string
*/
private $_user = "{{%user}}";
/**
* @var string
*/
private $_profile = "{{%profile}}";
/**
* Runs for the migate/up command
*
* @return null
*/
public function safeUp()
{
$time = time();
$password_hash = Yii::$app->getSecurity()->generatePasswordHash('pass12345');
$auth_key = Yii::$app->security->generateRandomString();
$table = $this->_user;
$sql = <<<SQL
INSERT INTO {$table}
(`username`, `email`,`password_hash`, `auth_key`, `created_at`, `updated_at`)
VALUES
('admin', 'admin@yoursite.com', '$password_hash', '$auth_key', {$time}, {$time})
SQL;
Yii::$app->db->createCommand($sql)->execute();
$id = Yii::$app->db->getLastInsertID();
//add profile entry for admin
$this->insert(
$this->_profile,
[
'user_id' => $id,
'name' => 'Administrator',
'public_email' => 'admin@yoursite.com'
]
);
}
/**
* Runs for the migate/down command
*
* @return null
*/
public function safeDown()
{
$table = $this->_user;
$sql = <<<SQL
SELECT id from {$table}
where username='admin'
SQL;
$id = Yii::$app->db->createCommand($sql)->execute();
$this->delete($this->_user, ['username' => 'admin']);
$this->delete($this->_profile, ['user_id' => $id]);
}
}
现在通过终端进入项目根目录并运行执行以下命令
./yii migrate
它将向您显示如下内容
Yii Migration Tool (based on Yii v2.0.22)
Total 1 new migration to be applied:
m180322_183353_add_admin_user
Apply the above migration? (yes|no) [no]:
输入 yes 并回车,如果一切顺利,它会在终端上显示如下输出
Apply the above migration? (yes|no) [no]:yes
*** applying m180322_183353_add_admin_user
> insert into {{%profile}} ... done (time: 0.006s)
*** applied m180322_183353_add_admin_user (time: 0.865s)
1 migration was applied.
Migrated up successfully.
现在您可以使用以下凭据登录
username : admin
password: pass12345
如果你以后想删除用户,请在终端中输入以下内容,然后删除迁移 运行s
./yii migrate/down 1
考虑到您在此之后没有创建任何其他迁移,或者创建一个单独的迁移来删除用户。
On PHP 5.5+ Yii 2 使用 password_hash()
生成密码哈希。如果你想重置某人的密码,你可以从控制台生成新的哈希值:
php -r 'echo password_hash("my password", PASSWORD_DEFAULT), "\n";'
并在更新查询中使用生成的值。
可以从数据库手动添加用户吗?我得到了一个用这个框架做的项目,但客户不记得管理员的访问代码,我已经尝试手动创建用户,但我不知道如何加密密码。
手动添加
是的,您也可以从数据库中手动添加用户,您只需要使用以下行生成的加密密码
echo Yii::$app->security->generatePasswordHash('Your_password');
现在您可以将上面的行复制粘贴到现有的视图文件中,然后复制输出字符串并在用户 table password
字段中另存为密码。
使用迁移
或 create a migration 并通过该迁移添加管理员用户以快速修复您可以将以下迁移复制粘贴到您的 migrations
文件夹中,它应该位于 basic-app
的根目录中或 console/migrations
表示 advance-app
.
注意:更改列 and/or table 名称,如果它们与您的情况不同,我使用了 user
和 profile
table 名称
<?php
// @codingStandardsIgnoreStart
use yii\db\Migration;
/**
* Class m180322_183353_add_admin_user
*/
class m180322_183353_add_admin_user extends Migration
{
// @codingStandardsIgnoreEnd
/**
* Table name
*
* @var string
*/
private $_user = "{{%user}}";
/**
* @var string
*/
private $_profile = "{{%profile}}";
/**
* Runs for the migate/up command
*
* @return null
*/
public function safeUp()
{
$time = time();
$password_hash = Yii::$app->getSecurity()->generatePasswordHash('pass12345');
$auth_key = Yii::$app->security->generateRandomString();
$table = $this->_user;
$sql = <<<SQL
INSERT INTO {$table}
(`username`, `email`,`password_hash`, `auth_key`, `created_at`, `updated_at`)
VALUES
('admin', 'admin@yoursite.com', '$password_hash', '$auth_key', {$time}, {$time})
SQL;
Yii::$app->db->createCommand($sql)->execute();
$id = Yii::$app->db->getLastInsertID();
//add profile entry for admin
$this->insert(
$this->_profile,
[
'user_id' => $id,
'name' => 'Administrator',
'public_email' => 'admin@yoursite.com'
]
);
}
/**
* Runs for the migate/down command
*
* @return null
*/
public function safeDown()
{
$table = $this->_user;
$sql = <<<SQL
SELECT id from {$table}
where username='admin'
SQL;
$id = Yii::$app->db->createCommand($sql)->execute();
$this->delete($this->_user, ['username' => 'admin']);
$this->delete($this->_profile, ['user_id' => $id]);
}
}
现在通过终端进入项目根目录并运行执行以下命令
./yii migrate
它将向您显示如下内容
Yii Migration Tool (based on Yii v2.0.22)
Total 1 new migration to be applied:
m180322_183353_add_admin_user
Apply the above migration? (yes|no) [no]:
输入 yes 并回车,如果一切顺利,它会在终端上显示如下输出
Apply the above migration? (yes|no) [no]:yes
*** applying m180322_183353_add_admin_user
> insert into {{%profile}} ... done (time: 0.006s)
*** applied m180322_183353_add_admin_user (time: 0.865s)
1 migration was applied.
Migrated up successfully.
现在您可以使用以下凭据登录
username : admin
password: pass12345
如果你以后想删除用户,请在终端中输入以下内容,然后删除迁移 运行s
./yii migrate/down 1
考虑到您在此之后没有创建任何其他迁移,或者创建一个单独的迁移来删除用户。
On PHP 5.5+ Yii 2 使用 password_hash()
生成密码哈希。如果你想重置某人的密码,你可以从控制台生成新的哈希值:
php -r 'echo password_hash("my password", PASSWORD_DEFAULT), "\n";'
并在更新查询中使用生成的值。