Search code examples
phplaravelpermissionsroles

Laravel 8 gates not working with Auth::guard


Hey I am developing a project where I have roles and permissions for users and I am trying to protect my routes with the help of middleware by defining Gates but it's showing me 403| Not Authorized. I can't understand what the actual problem is?

Into Category Controller

public function addcategory(AdminsRole $adminsroles){

   return view('add-category');

}

Into Routes

Route::get('/add-category', [CategoryController::class, 'addcategory'])->middleware('can:add-category')->name('addcategory');

Into AuthServiceProvider.php

$admin = Auth::guard('admin');

Gate::define('add-category', function ($admin, AdminsRole $adminsroles) {

    if($admin->user()->u_type != 'superadmin'){

        $adminRolescount = $adminsroles::where([
            ['admin_id', '=', $admin->user()->id],
            ['module', '=', 'categories'],
            ['add_access', '=', '1'],
        ])->count();
        return $adminRolescount;

    }else{

        return $adminRolescount = 1;

    } 

});

Solution

  • I think what you're looking for can be simplified. The code in your controller and routes file does not need to be adjusted. I would however change your gate definition to the following:

    Gate::define('add-category', function ($user = null) {
        
        // Fetch user from custom guard
        $user = Auth::guard('admin')->user();
    
        // Check if a valid user instance was passed
        if (is_null($user)) {
            return false;
        }
    
        // Allow super admins to add categories regardless of AdminsRole existence
        if ($user->u_type === 'superadmin') {
            return true;
        }
    
        // Check if current user has a matching AdminsRole row with add_access permission
        return AdminsRole::where([
            ['admin_id', '=', $user->id],
            ['module', '=', 'categories'],
            ['add_access', '=', '1'],
        ])->exists();
    
    });
    

    Note that a gate always receives a user instance as their first parameter if there is a logged in user, you needn't supply this yourself. Additionally, you can query for the AdminsRole existence directly via the model, using the id of the user instance that is being checked and automatically supplied to the gate.