1

我在我的 laravel 4 应用程序(http://docs.cartalyst.com/sentry-2/)中使用了 Sentry2 包。

我创建了一个扩展 Sentry2 用户模型的新用户模型:

<?php namespace App\Models;

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends \Cartalyst\Sentry\Users\Eloquent\User implements UserInterface, RemindableInterface {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }

}

当我执行以下代码时,我有一个异常。

$adminUser = User::create(array(
    'email'       => 'admin@admin.com',
    'password'    => "admin",
    'first_name'  => 'Admin',
    'last_name'   => 'Admin',
    'activated'   => 1,
));

错误:

 [RuntimeException]
 A hasher has not been provided for the user.
4

4 回答 4

3

如果你想从 Eloquent 质量对齐创建你的对象,你可以手动添加 Hasher 像这样:

$user->setHasher(new Cartalyst\Sentry\Hashing\NativeHasher);

或者是永久的,你可以像这样向你的用户对象添加一个“引导”方法:

class User extends \Cartalyst\Sentry\Users\Eloquent\User implements UserInterface, RemindableInterface {

    // ...

    public static function boot()
    {
        self::$hasher = new Cartalyst\Sentry\Hashing\NativeHasher;
    }

    // ...

}
于 2014-01-29T17:56:06.603 回答
2

我需要更新哨兵包的配置文件:

'users' => array(

    /*
    |--------------------------------------------------------------------------
    | Model
    |--------------------------------------------------------------------------
    |
    | When using the "eloquent" driver, we need to know which
    | Eloquent models should be used throughout Sentry.
    |
    */

    'model' => '\App\Models\User',

    /*
    |--------------------------------------------------------------------------
    | Login Attribute
    |--------------------------------------------------------------------------
    |
    | If you're the "eloquent" driver and extending the base Eloquent model,
    | we allow you to globally override the login attribute without even
    | subclassing the model, simply by specifying the attribute below.
    |
    */

    'login_attribute' => 'email',

),

和使用Sentry::getUserProvider()->create()方法

    $adminUser = Sentry::getUserProvider()->create(
        array(
            'email'       => 'admin@admin.com',
            'password'    => "admin",
            'first_name'  => 'Admin',
            'last_name'   => 'Admin',
            'activated'   => 1,
        )
    );
于 2013-05-22T23:46:16.853 回答
0

我像你一样扩展了哨兵用户模型并返回了相同的错误,然后我在https://github.com/cartalyst/sentry/issues/163中找到了一个想法,然后尝试传递一个新的 NativeHasher 实例;我不确定这是否是正确的方法,但在第一次测试中,用户被正确保存:

$user = new User;
$user->setHasher(new Cartalyst\Sentry\Hashing\NativeHasher);
$user->first_name = "Name";
$user->last_name = "Last";
$user->password = 'admin';
$user->email = "email@gmail.com";
$user->save();
于 2013-07-28T03:55:43.100 回答
0

它告诉你的是你必须对你的密码进行哈希和加盐,所以

$adminUser = User::create(array(
    'email'       => 'admin@admin.com',
    'password'    => Hash::make('admin'),
    'first_name'  => 'Admin',
    'last_name'   => 'Admin',
    'activated'   => 1,

));

于 2013-05-20T17:56:04.913 回答