romanbican / roles

Powerful package for handling roles and permissions in Laravel 5
MIT License
1.15k stars 296 forks source link

`hasRole` always returns false!!! #164

Closed Mahmoudz closed 8 years ago

Mahmoudz commented 8 years ago

I'm using version 2.1.7

I attached the role on the user but when I check if he has the role I always get false!! That's my test code:

$listUsersPermission = Permission::create([
    'name' => 'List users',
    'slug' => 'list.users',
    'description' => 'List all registered Users',
]);

$adminRole = Role::create([
    'name' => 'Admin',
    'slug' => 'admin',
    'description' => 'Admin access',
    'level' => 1,
]);

$adminRole->attachPermission($listUsersPermission);

$user = $this->getLoggedInTestingUser(); // getting a User instance

$user->attachRole($adminRole);

dd($user->hasRole('admin')); // false!!!!!!!!
Isfirs commented 8 years ago

Can you provide more information? What Laravel version are you using?

How did you set up your environment? Did you add the Trait? What is the output if you do dd(Permission::all(), Role::all()) ? Please show a SELECT* FROM role_user; from your database.

Mahmoudz commented 8 years ago

I'm using Laravel v5.1.34

Yes I setup the environment.

Yes I added the Trait Bican\Roles\Traits\HasRoleAndPermission

\DB::select('SELECT * FROM permission_user')

array(

)),

\DB::select('SELECT * FROM role_user')

array(
     'id' => '1',
     'role_id' => '1',
     'user_id' => '1',
     'created_at' => '2016-04-20 18:26:33',
     'updated_at' => '2016-04-20 18:26:33',
  )),

Permission::all()

{  
   "id":"1",
   "name":"List users",
   "slug":"list.users",
   "description":"List all registered Users",
   "model":null,
   "created_at":"2016-04-20 18:28:08",
   "updated_at":"2016-04-20 18:28:08"
}

Role::all()

{  
   "id":"1",
   "name":"Admin",
   "slug":"admin",
   "description":"Admin access",
   "level":"1",
   "created_at":"2016-04-20 18:30:42",
   "updated_at":"2016-04-20 18:30:42"
}

I'm surprised why role_user has the data stored!! and still getting false whenever I run this dd($user->hasRole('admin'));

Isfirs commented 8 years ago

I have problems getting what you are doing.

Can you give me link to your project? I will clone it, set up and check local.

Make sure to set up seeders and migrations.

Mahmoudz commented 8 years ago

Hi @Isfirs thanks for your reply. It's too late! I already switched to Entrust, I think this package is prettier but I couldn't wait anymore.. I am sure I've done everything correctly! it's weird why it refused to work!! anyway I'll close this issue now. Thanks again.