Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5, Entrust - check roles not working

Tags:

php

laravel-5

I'm new in Laravel. I'm trying to use in Laravel 5 Zizaco/entrust (from laravel-5 branch). All working ok - attach rules, detach rules... but when I try check permissions I have problems.

First I try in routes.php, but in this place Entrust don't know who am I, hasRole and routeNeedsRole not working in routes.php.

In middleware hasRole is working but routeNeedsRole not. Trying use as second parameter string, array, same effect - abort(403) runs.

Because hasRole is working this problem looks very strange for me.

composer dump-autoload - used, not solve problem

in routes.php

Entrust::hasRole('superadmin');// => false
\Entrust::routeNeedsRole('admin/dashboard', ['superadmin'], abort(403, 'Forbidden'), false); // display 403 page

in middleware

\Entrust::hasRole('superadmin'); // => true
\Entrust::routeNeedsRole('admin/dashboard', ['superadmin'], abort(403, 'Forbidden'), false); // display 403 page

My model User.php

use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

use Authenticatable, CanResetPassword, EntrustUserTrait;

routes.php

Route::group([ 'prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => ['auth', 'admin']], function (){
    Route::get('dashboard', [ 'as' => 'dashboard', 'uses' => "DashBoardController@index" ]);
});

I have also Role and Permission models looks like in Readme file https://github.com/Zizaco/entrust/tree/laravel-5

// sorry for my english.

like image 782
Grzegorz Gąsak Avatar asked Mar 14 '15 10:03

Grzegorz Gąsak


People also ask

How to install entrust in Laravel 5?

Entrust is a succinct and flexible way to add Role-based Permissions to Laravel 5. If you are looking for the Laravel 4 version, take a look Branch 1.0. It contains the latest entrust version for Laravel 4. In order to install Laravel 5 Entrust, just add the following to your composer.json. Then run composer update:

How to check for roles and permissions in Laravel?

Now we can check for roles and permissions simply by doing: Both hasRole () and can () can receive an array of roles & permissions to check: By default, if any of the roles or permissions are present for a user then the method will return true.

What is the use of Laravel 5?

Laravel 5 provides authentication to us but that it simple to get user register, login, logout, and reset password and run quickly and easily. Laravel 5 give you simple authentication and it's fast and it's consider to all developer requirement.

How do I tell entrust where my permission and role models?

If your app uses a custom namespace then you'll need to tell entrust where your permission and role models are, you can do this by editing the config file in config/entrust.php.


1 Answers

Update: Laravel 5.1.11 and newer now come with built in Authorization. It is much more Laravel friendly and will always be well maintained. Use this when possible


You are using the middleware wrong. There is a lot of Laravel 4 stuff still in the docs for Entrust so you have to be selective as to what you use from there. The middleware shouldn't be setting routeNeedsRole. Actually routeNeedsRole doesn't really fit in L5 in my opinion. Here is how I would do it:

Create a new middleware with

php artisan make:middleware AuthAdmin

Now in the newly generated app/Http/Middleware/AuthAdmin.php

<?php namespace App\Http\Middleware;

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

class AuthAdmin {

  protected $auth;

  public function __construct(Guard $auth) {
    $this->auth = $auth;
  }

  public function handle($request, Closure $next) {
    if ($this->auth->guest()) {
      if ($request->ajax()) {
        return response('Unauthorized.', 401);
      } else {
        return redirect()->guest('auth/login');
      }
    } else if(! $request->user()->hasRole('superadmin')) {
      return abort(404); //Or redirect() or whatever you want
    }
    return $next($request);
  }

}

This will do the same thing as the auth middleware but if they are already logged in and don't have the 'superadmin' role they will get the 404.

Next we need to add the middleware to routemiddleware. Do this in app/Http/Kernal.php:

protected $routeMiddleware = [
  ...,
  'superadmin' => 'App\Http\Middleware\AuthAdmin',
];

This makes it possible to add the middleware to the controller. Now let's do that. In your controller we do this in the constructor:

public function __construct() {
  $this->middleware('superadmin');
}

This will add the middleware to the whole controller. You can be specific as to the routes if needed but for your case I would assume we need the whole controller protected.

Let me know if you need nay more help.

Note: It would be ideal to make AuthAdmin run the 'auth' middleware first instead of copying the code but I don't know how to do that from within the middleware and we don't want to have to do middleware => ['auth', 'superadmin'] instead of just 'superadmin'. If we didn't copy the 'auth' code over we would be trying to get ->hasRole() of null which would get an error.

like image 171
DutGRIFF Avatar answered Sep 23 '22 00:09

DutGRIFF