Laravel-5将hasRole方法添加到Auth [英] Laravel-5 adding hasRole method to Auth

查看:591
本文介绍了Laravel-5将hasRole方法添加到Auth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展Laravel-5.1 Auth中间件,以便可以向其中添加我自己的方法:

I'm trying to extend the Laravel-5.1 Auth middleware so that I can add my own method to it:

Auth::hasRole()

要将新方法hasRole添加到Auth,我需要做什么?

What do I need to do in order to add the new method hasRole to Auth?

这是我的路线文件:

/* Administrator Routes */

Route::group(['namespace' => 'Admin', 'middleware' => 'timesheets.admin:Administrator'], function()
{
    Route::get('home', 'AdminController@index');
});

这是我的中间件文件:

<?php

namespace App\Http\Middleware\Auth;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class AdminAuthenticate
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('login');
            }
        }

        if (auth()->check() && auth()->user()->hasRole($role)) {
            return $next($request);
        }

    }
}

推荐答案

您能否尝试将以下内容添加到您的用户模型:-

Could you try adding the following to your User model:-

public function hasRole($role)
{
    return User::where('role', $role)->get();
}

这应该首先检查您的用户表是否具有角色"字段,然后针对role字段检查参数$role.

This should firstly check to see if you User table has the field 'role' and then check your parameter $role against the role field.

您可以通过以下操作进行检查:

You can the check by doing the following:

if( Auth::user()->hasRole($role) )

您可能需要根据需要调整示例.需要帮助请叫我.

You may need to adjust the example to your needs. Let me know if you need anything else.

/------------ EDIT -----------------/

/------------EDIT-----------------/

如果您有两个单独的表,一个表保存用户信息,另一个表保存用户特权/角色,则可以向用户模型添加另一个功能:

If you have two seperate tables, one holding the user information and the other holding the users privileges/roles you could add another function to the User model:

public function userID()
{
    return $this->user_id; 
}

这将检查您是否具有用户ID字段,如果有,它将返回已认证用户的ID.

This will check for if you have a user ID field if so, it will return the id for the authenticated user.

然后将其添加到您的hasRoles方法:

Then add this to your hasRoles method:

    public function hasRoles($userID, $roles)
{
    return Your\User\Roles\Model::where('role', $role)->where('user_id', $user_id)->get();
}

您的中间件如下所示:

public function handle($request, Closure $next, $role)
{
    if ($this->auth->guest()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('login');
        }
    }

    $userID = Auth::user()->userID();

    if (auth()->check() && auth()->user()->hasRole($userID, $role)) {
        return $next($request);
    }

}

如果我正确理解您想要什么.我相信这应该为您工作.

If I understood correctly what you want. I believe this should work for you.

这篇关于Laravel-5将hasRole方法添加到Auth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆