对于那些熟悉Kohana中的Auth模块的人,我无法登录用户。我可以创建一个用户很好,但显然哈希不匹配。我已经使用提供MySql架构来创建数据库,而我正在使用模块模型。
这是我创建用户代码:
public function user_create()
{
$user = ORM::factory('user');
$user->username = "user";
$this->auth = Auth::instance();
$user->email = "info@test.com";
$user->password = $this->auth->hash_password('admin');
$user->add(ORM::factory('role', 'login'));
$user->add(ORM::factory('role', 'admin'));
if ($user->save())
{
$this->template->title = "user saved";
$this->template->content = "user saved";
}
}
它创建一个带有哈希密码的用户,并为其提供正确的登录/管理员角色。数据库中的一切看起来都很好。这是我的登录代码。我跳过了检查用户是否已登录的开始部分。
$user = $this->input->post('username');
$password = $this->input->post('password');
if(!empty($user))
{
$find_user = ORM::factory('user')->where('username', $user)->find();
$username = $find_user->username;
$this->auth = Auth::instance();
if($this->auth->login($username, $password))
{
$error = "logged in";
}
else
{
$error = "not logged in at all";
}
}
$this->template->content = new View('admin/login_view');
$this->template->content->user_info = $username . " " . $password;
$this->template->title = "Login Admin";
$this->template->content->bind('error', $error);
它总是返回“根本没有登录”。我验证我输入了正确的用户名和密码,但它没有登录。我无法弄清楚原因。我正在使用内置的hash_password函数来创建密码,我已经按照文档,但我无法发现错误。有什么帮助吗?
答案 0 :(得分:12)
当您通过__set()方法设置密码时,kohana中的Auth模块会自动散列密码。因此,为了您存储密码,请执行以下操作:
public function user_create()
{
$user = ORM::factory('user');
$user->username = "user";
$this->auth = Auth::instance();
$user->email = "info@test.com";
$user->password = 'admin';
...
希望有所帮助。如果你想查看auth模块(models / auth_user.php),你可以看到它记录了密码:
public function __set($key, $value)
{
if ($key === 'password')
{
// Use Auth to hash the password
$value = Auth::instance()->hash_password($value);
}
parent::__set($key, $value);
}