关于将 Sentinel 与 Laravel 和 $fillable 数组一起使用的查询

Query regarding using Sentinel with Laravel and the $fillable array

我在 Laravel 中使用 Cartalyst Sentinel 进行用户身份验证。 我已经创建了我的表单来添加新用户。 由于某些奇怪的原因,除非我将密码字段放入用户 class 模型的 $fillable 数组中,否则密码不会通过 Sentinel::register() 传递。 这是一个潜在的安全问题。 我该如何解决这个问题?使用 Sentinel 创建新用户时一定有一些我遗漏的东西(而且 Sentinel 文档对有用的信息非常少)。

只是简要介绍一下我在代码方面所做的工作。我的数组中填满了创建用户所需的字段。该数组被传递到 Sentinel::register()。一切似乎都进行得很顺利,但是当我去查看数据库时,密码字段是空白的。

$newUser = array(
    '_token' => Input::get('_token'),
    'email' => Input::get('email'),
    'password' => Input::get('password'),
    'first_name' => Input::get('first_name'),
    'middle_name' => Input::get('middle_name'),
    'last_name' => Input::get('last_name'));

$user = Sentinel::register($newUser);

附带说明:不幸的是我无法切换身份验证系统。我需要使用 Sentinel。

如果您不想让新用户成为可填写的用户,则需要手动设置密码选项 属性。

$newUser = array(
'_token' => Input::get('_token'),
'email' => Input::get('email'),
'first_name' => Input::get('first_name'),
'middle_name' => Input::get('middle_name'),
'last_name' => Input::get('last_name'));

$user = Sentinel::register($newUser);

$user->password = Input::get('password');

$user->save();

您可能也需要在设置密码之前对密码进行哈希处理,对吗?类似于 $user->password = \Hash::make(Input::get('password'));。除非 Sentinel 自动执行此操作。

只是另一种与 Jeff 的回答几乎相同的方法。 这应该基于 Sentinel 代码工作,但我没有使用过 Sentinel。部署前测试。

$newUser = array(
    '_token' => Input::get('_token'),
    'email' => Input::get('email'),
    'password' => Input::get('password'),
    'first_name' => Input::get('first_name'),
    'middle_name' => Input::get('middle_name'),
    'last_name' => Input::get('last_name')
);

Sentinel::register($newUser, function($user) use ($newUser) {
    try {
        return $user->password = \Hash::make($newUser['password']);
    } catch(RuntimeException $e) {
        return false;
    }
});

回调在 fill 方法之后运行,因此它应该绕过 $fillable 限制,如果您的设计需要,您可以从 fillable 中删除密码。
如果在回调中返回 false,则不会创建用户。