Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel-5 adding hasRole method to Auth

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

Auth::hasRole()

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

Here is my routes file:

/* Administrator Routes */

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

Here is my middleware file:

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

    }
}
like image 712
V4n1ll4 Avatar asked Nov 30 '22 17:11

V4n1ll4


2 Answers

I've taken a different tack by using a trait in my User model.

<?php
namespace App\Traits;

use App\Role;
use App\User;

trait HasRoles{
    public function roles()
    {
        return $this->belongsToMany('App\Role');
    }

    public static function findByRole(Role $role)
    {
        return $role->users()->get();
    }

    public function hasRole(Role $role)
    {
        return $this->roles()->get()->contains($role);
    }
}
like image 31
mike Avatar answered Dec 23 '22 09:12

mike


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

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

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-----------------/

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

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

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

Your middleware would look like this:

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.

like image 57
jakehallas Avatar answered Dec 23 '22 09:12

jakehallas