Php 角色上具有雄辩驱动程序的laravel身份验证

Php 角色上具有雄辩驱动程序的laravel身份验证,php,authentication,laravel,eloquent,Php,Authentication,Laravel,Eloquent,我正在尝试验证我的Laravel应用程序中的用户 我遇到以下问题: 在auth.php中使用driverdatabase:我可以使用auth::trunt()登录,并且auth::check正在工作,但是我无法验证登录的用户是否具有特定角色 在auth.php中使用drivereloquent:我可以使用auth::trunt()登录,但auth::check不起作用。但是,我可以检查登录用户的角色 编辑(问题):如何修复此问题,以便仅使用其中一个驱动程序,就可以执行完整的身份验证和角色检查

我正在尝试验证我的Laravel应用程序中的用户

我遇到以下问题:

  • 在auth.php中使用driverdatabase:我可以使用auth::trunt()登录,并且auth::check正在工作,但是我无法验证登录的用户是否具有特定角色
  • 在auth.php中使用drivereloquent:我可以使用auth::trunt()登录,但auth::check不起作用。但是,我可以检查登录用户的角色
编辑(问题):如何修复此问题,以便仅使用其中一个驱动程序,就可以执行完整的身份验证和角色检查

迁移表:

Schema::create('users', function ($table) {
        $table->increments('id');
        $table->integer('group_id')->unsigned();
        $table->string('name', 64);
        $table->string('email', 64)->unique();
        $table->string('username', 64)->unique();
        $table->string('phone', 13);
        $table->string('address', 64);
        $table->boolean('isresponsible');
        $table->string('password', 64);
        $table->rememberToken()->nullable();
    });
Schema::create('roles', function ($table) {
        $table->increments('id');
        $table->string('name');
    });

Schema::create('users_roles', function ($table) {
            $table->integer('user_id')->unsigned();
            $table->integer('role_id')->unsigned();
        }
    );
Schema::table('users_roles', function($table){
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('role_id')->references('id')->on('roles');
    });
模型类用户

<?php
use Illuminate\Auth\UserTrait;`
use Illuminate\Auth\UserInterface;`
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {


use UserTrait, RemindableTrait;

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

public static $rules = ['name' => 'required', 'group_id' => 'required', 'email' => 'required', 'phone' => 'required'];
protected $fillable = ['name', 'group_id', 'email', 'phone', 'address', 'isresponsible', 'password'];

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

public function group()
{
    return $this->belongsTo('Group');
}

public function userroles(){
    return $this->hasMany('Userrole');
}

public function roles()
{
    return $this->belongsToMany('Role', 'users_roles');
}

public function hasRole($check)
{
    dd($this->roles->toArray());
    return in_array($check, array_fetch($this->roles->toArray(), 'name'));
}

public function setBasicPassword($id){
    $user = User::find($id);
    $user->password = Hash::make('changeme');
    $user->save();
}

public function isValid()
{
    $validation = Validator::make($this->attributes, static::$rules);
    if ($validation->passes()) return true;
    $this->messages = $validation->messages();
    return false;
}


/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    // TODO: Implement getReminderEmail() method.
}

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

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

/**
 * Get the token value for the "remember me" session.
 *
 * @return string
 */
public function getRememberToken()
{
    return $this->remember_token;
}

public function setRememberToken($value)
{
    $this->remember_token = $value;
}

public function getRememberTokenName()
{
    return 'remember_token';
}
}
HomeController身份验证功能

 public function authenticate(){
    $rules = array(
        'email'    => 'required|email',
        'password' => 'required|alphaNum|min:3'
    );
    $validator = Validator::make(Input::all(), $rules);
    if ($validator->fails()) {
        return Redirect::to('login')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    } else {
        $userdata = array(
            'email' => Input::get('email'),
            'password' => Input::get('password')
        );
        if (Auth::attempt($userdata, true)) {
            return Redirect::action('HomeController@index');

        } else {
            return Redirect::action('HomeController@login')->withInput();
        }
    }
}
使用数据库驱动程序
-auth:trument()和auth::check正在工作

$this->beforeFilter('admin', ['only' => ['index']]); //filter in controller
//filter in filters;php
Route::filter('admin', function()
{
if(!Auth::check()) return Redirect::action('HomeController@index');
if(!Auth::user()->hasRole('admin')) return View::make('errors.401');
});
“调用未定义的方法\Auth\GenericUser::hasRole()”失败”

编辑数据库驱动程序返回一个GenericUser对象,我需要自己的用户对象。我不知道在哪里可以改变这个

解决方法:我不想使用这个,丑陋的代码和过滤器(或视图)不需要这样做

Route::filter('admin', function()
{
    if(!Auth::check()) return Redirect::action('HomeController@index');
    $user = User::find((Auth::user()->id));
    if(!$user->hasRole('admin')){ return View::make('errors.401');}
});
使用雄辩的驱动程序

  • auth::trument()成功
  • auth::check()失败
  • 过滤器上没有错误

问题在于
getAuthIdentifier()
的实现。此方法实际上应该返回表的主键,而不是用于登录的用户名

所以你的应该是这样的:

public function getAuthIdentifier(){
    return $this->id;
}
或者实际上,我建议您进一步清理您的模型,因为所有的
getSomeAuthStuff
方法都是在这两个特性中实现的

使用作为基础并添加所有自定义代码(角色、方法、规则等)

背景信息 从
getAuthIdentifier()
返回的值将存储在会话中。
之后使用
check()
时,将在
UserProvider
上调用
retrieveById
。而EloquentUserProvider执行以下操作:

public function retrieveById($identifier)
{
    return $this->createModel()->newQuery()->find($identifier);
}

它使用
find()
,通过它的主键(通常是
id
)搜索模型。

您是否只尝试了
Auth::trunt($userdata)
而没有记住选项?是的,它给出了相同的结果des
Auth::id()
工作?(使用Eloquent时)返回用户名/电子邮件地址aaaaa!!现在写一个答案:)
public function retrieveById($identifier)
{
    return $this->createModel()->newQuery()->find($identifier);
}