我在laravel 4应用程序中使用Sentry2包(http://docs.cartalyst.com/sentry-2/)。
我创建了一个扩展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.
答案 0 :(得分:3)
如果您想从Eloquent质量对齐创建对象,可以手动添加Hasher,如下所示:
$user->setHasher(new Cartalyst\Sentry\Hashing\NativeHasher);
或者永久性地,您可以向User对象添加“boot”方法,如下所示:
class User extends \Cartalyst\Sentry\Users\Eloquent\User implements UserInterface, RemindableInterface {
// ...
public static function boot()
{
self::$hasher = new Cartalyst\Sentry\Hashing\NativeHasher;
}
// ...
}
答案 1 :(得分:2)
我需要更新sentry包的配置文件:
'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,
)
);
答案 2 :(得分:0)
它告诉你的是你必须哈希并加密你的密码 所以
$adminUser = User::create(array(
'email' => 'admin@admin.com',
'password' => Hash::make('admin'),
'first_name' => 'Admin',
'last_name' => 'Admin',
'activated' => 1,
));
答案 3 :(得分:0)
我像你一样扩展了Sentry用户模型并返回了相同的错误,然后我在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();