Laravel-permission: User does not have the right permissions

Created on 4 Aug 2018  路  8Comments  路  Source: spatie/laravel-permission

hello

i try to access to my admin panel and i set middleware to have browse admin permission

so when i try with my admin account work great and when i try it with normal user account this error show up

"User does not have the right permissions."

but if i try to access admin without login it's redirect me to my root page (not admin panel page)

so i want to redirect any user don't have any browse admin permission to custom error page

please help me 馃憤

support

Most helpful comment

It could be because in your route middleware you are using 2 colons instead of one:
'permission::browse admin'

All 8 comments

I'm not sure I understand. I think you may need to add another middleware mention into your routing.
But without seeing your code it's hard to help.
I don't think this is a problem with this permissions package, but rather something missing in your app.

my routers file

Route::group(['middleware' => ['auth','permission::browse admin']], function () {
    Route::get('/admin', 'AdminController@index')->name('admin');
   }
);

my admincontroller

```

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use App\User;
use Auth;

class AdminController extends Controller
{

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

public function index()
{
    echo "Welcome To Admin Dashboard :)";
}


// public function perm()
// {
//  $role = Role::create(['name' => 'admin']);
//  $permission = Permission::create(['name' => 'browse admin']);
//  $permission->assignRole($role);
//  $user = User::find(1);

// $role = Role::where('name', 'admin')->get()->first();
// $user->assignRole($role);
// }
}
```

It could be because in your route middleware you are using 2 colons instead of one:
'permission::browse admin'

you are right i delete 1 colon but still nothing changed

You experience the redirect when not logged in because the auth middleware does a redirect when not authenticated.

The permission middleware does not do a redirect. It throws an UnauthorizedException: https://github.com/spatie/laravel-permission/blob/master/src/Middlewares/PermissionMiddleware.php#L27

You can add your own redirect by catching that exception in your app's exception handler.

thank you i try your way it's work

one last question

i want to throw error with redirect how i can do it?

You should be able to flash session data in your redirect:
https://laravel.com/docs/5.6/redirects#redirecting-with-flashed-session-data

thank you 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bhulsman picture bhulsman  路  3Comments

Dreambox13 picture Dreambox13  路  3Comments

ionesculiviucristian picture ionesculiviucristian  路  4Comments

feliperoan picture feliperoan  路  3Comments

neoreids picture neoreids  路  3Comments