Hi all
I am implementing an ACL along Jeff's explanations in https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/16. So I have setup a roles and a permissions table with two pivot-tables permission_role and role_user. In the boot method of the AuthServiceProvider I have defined the Gate as follows:
public function boot()
{
$this->registerPolicies();
/**
* boot is called also when migration is run, hence it will not find the table permissions and throw
* an exception.
*/
if(Schema::hasTable('permissions')){
foreach($this->getPermissions() as $permission){
Gate::define($permission->name, function($user) use ($permission){
return $user->hasRole($permission->roles);
});
}
}
}
protected function getPermissions(){
return Permission::with('roles')->get();
}
I need to do the check on the existence of the table 'permissions'. Otherwise the getPermission method throws an Exception during migration, since no 'permissions' table is found. I need to migrate manually incl. prepared seeders. The acl-relevant migrations are stored in a subfolder of the migrations folder to keep them outside of the regular migrations. The DatabaseMigrations trait does not work in this feauture test.
function a_school_owner_can_create_edit_view_and_delete_a_school()
{
$user = factory(User::class)->create();
$user->assignRole('school-owner');
Auth::loginUsingId($user->id);
$this->assertTrue(Auth::user()->can('create-school'));
$this->assertTrue(Auth::user()->can('view-school'));
$this->assertTrue(Auth::user()->can('edit-school'));
$this->assertTrue(Auth::user()->can('delete-school'));
Auth::logout();
}
My question: the $user in the callback function of the Gate definition is null all the time, despite my Auth::loginUsingId($user->id), which actually returns the proper user. I am a bit confused on why that could be. Any help much appreciated. Thanks in advance.