Skip to content

Commit

Permalink
Merge pull request #2219 from erikn69/erikn69-patch-fix
Browse files Browse the repository at this point in the history
Fix undefined index guard_name
  • Loading branch information
drbyte committed Oct 19, 2022
2 parents 1cb0a5c + e4493da commit 8125446
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function roles(): BelongsToMany
public function users(): BelongsToMany
{
return $this->morphedByMany(
getModelForGuard($this->attributes['guard_name']),
getModelForGuard($this->attributes['guard_name'] ?? config('auth.defaults.guard')),
'model',
config('permission.table_names.model_has_permissions'),
PermissionRegistrar::$pivotPermission,
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function permissions(): BelongsToMany
public function users(): BelongsToMany
{
return $this->morphedByMany(
getModelForGuard($this->attributes['guard_name']),
getModelForGuard($this->attributes['guard_name'] ?? config('auth.defaults.guard')),
'model',
config('permission.table_names.model_has_roles'),
PermissionRegistrar::$pivotRole,
Expand Down
14 changes: 14 additions & 0 deletions tests/PermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@

class PermissionTest extends TestCase
{
/** @test */
public function it_get_user_models_using_with()
{
$this->testUser->givePermissionTo($this->testUserPermission);

$permission = app(Permission::class)::with('users')
->where($this->testUserPermission->getKeyName(), $this->testUserPermission->getKey())
->first();

$this->assertEquals($permission->getKey(), $this->testUserPermission->getKey());
$this->assertCount(1, $permission->users);
$this->assertEquals($permission->users[0]->id, $this->testUser->id);
}

/** @test */
public function it_throws_an_exception_when_the_permission_already_exists()
{
Expand Down
13 changes: 13 additions & 0 deletions tests/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ public function setUp(): void
Permission::create(['name' => 'wrong-guard-permission', 'guard_name' => 'admin']);
}

/** @test */
public function it_get_user_models_using_with()
{
$this->testUser->assignRole($this->testUserRole);

$role = app(Role::class)::with('users')
->where($this->testUserRole->getKeyName(), $this->testUserRole->getKey())->first();

$this->assertEquals($role->getKey(), $this->testUserRole->getKey());
$this->assertCount(1, $role->users);
$this->assertEquals($role->users[0]->id, $this->testUser->id);
}

/** @test */
public function it_has_user_models_of_the_right_class()
{
Expand Down

0 comments on commit 8125446

Please sign in to comment.