Sentry2 user model extension

我只是一个虾纸丫 提交于 2020-01-15 23:08:15

问题


I'm using Sentry2 package in my laravel 4 application (http://docs.cartalyst.com/sentry-2/).

I create a new User model that extends Sentry2 User Model:

<?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;
    }

}

when I execute the follow code I have a exception.

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

Error:

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

回答1:


If you want to create your object from Eloquent mass alignment you can add the Hasher manually Like that :

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

Or to be permanent you can add a "boot" method to your User object like this :

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

    // ...

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

    // ...

}



回答2:


I need to update the config file of sentry package:

'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',

),

and use Sentry::getUserProvider()->create() method

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



回答3:


what it tell you is you have to hash and salt your password so

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

));




回答4:


I extended the Sentry User Model like you and the same error is returned, then i find an idea in https://github.com/cartalyst/sentry/issues/163 and then tried passing a new instance of NativeHasher; i'm not sure if this is the correct way, but in the first test the user was saved correctly:

$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();


来源:https://stackoverflow.com/questions/16655070/sentry2-user-model-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!