2013-05-20 2 views
1

Я использую пакет Sentry2 в своем приложении 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'  => '[email protected]', 
    'password' => "admin", 
    'first_name' => 'Admin', 
    'last_name' => 'Admin', 
    'activated' => 1, 
)); 

Ошибка:

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

ответ

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'  => '[email protected]', 
      'password' => "admin", 
      'first_name' => 'Admin', 
      'last_name' => 'Admin', 
      'activated' => 1, 
     ) 
    ); 
0

что сказать вам, у вас есть хэш и соли пароль так

$adminUser = User::create(array(
    'email'  => '[email protected]', 
    'password' => Hash::make('admin'), 
    'first_name' => 'Admin', 
    'last_name' => 'Admin', 
    'activated' => 1, 

));

+0

проблема не в этом. Hash: make, а не решить проблему. –

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 protected]"; 
$user->save(); 
+0

Вы пытались использовать «Sentry :: getUserProvider() -> create (....»? – Leabdalla

+0

Да, вы все в порядке с Sentry :: getUserProvider() -> create ... ., я использовал это в своем репозитории. – Bradley

3

Если вы хотите создать свой объект от выравнивания массового красноречивого вы можете добавить 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; 
    } 

    // ... 

} 
Смежные вопросы