Skip to content

Commit 5bb8a94

Browse files
Merge pull request #95 from brandonlim-hs/bugfix-unintended-users-relation
Fix unintended relation to users when a model uses HasPermissions trait
2 parents 2c6e328 + 2e07803 commit 5bb8a94

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

src/Models/Permission.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function roles(): BelongsToMany
108108
* A permission belongs to some users of the model associated with its guard.
109109
* @return BelongsToMany
110110
*/
111-
public function users()
111+
public function users(): BelongsToMany
112112
{
113113
return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name']));
114114
}

src/Models/Role.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Maklad\Permission\Models;
44

55
use Jenssegers\Mongodb\Eloquent\Model;
6+
use Jenssegers\Mongodb\Relations\BelongsToMany;
67
use Maklad\Permission\Contracts\RoleInterface;
78
use Maklad\Permission\Exceptions\GuardDoesNotMatch;
89
use Maklad\Permission\Exceptions\RoleAlreadyExists;
@@ -117,6 +118,15 @@ public static function findByName(string $name, $guardName = null): RoleInterfac
117118
return $role;
118119
}
119120

121+
/**
122+
* A role belongs to some users of the model associated with its guard.
123+
* @return BelongsToMany
124+
*/
125+
public function users(): BelongsToMany
126+
{
127+
return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name']));
128+
}
129+
120130
/**
121131
* Determine if the user may perform the given permission.
122132
*

src/Traits/HasPermissions.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ public function permissions(): BelongsToMany
4949
return $this->belongsToMany(config('permission.models.permission'));
5050
}
5151

52-
/**
53-
* A role belongs to some users of the model associated with its guard.
54-
*/
55-
public function users(): BelongsToMany
56-
{
57-
return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name']));
58-
}
59-
6052
/**
6153
* Grant the given permission(s) to a role.
6254
*

tests/HasPermissionsTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,10 @@ public function it_can_determine_that_the_user_has_all_of_the_permissions_via_ro
430430
$this->testUser->assignRole('testRole');
431431
$this->assertTrue($this->testUser->hasAllPermissions('edit-articles', 'edit-news'));
432432
}
433+
434+
/** @test */
435+
public function a_model_that_uses_hasPermissions_trait_should_not_have_users_method()
436+
{
437+
$this->assertFalse(method_exists($this->testUser, 'users'));
438+
}
433439
}

tests/HasRolesTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,10 @@ public function it_can_give_and_revoke_multiple_roles()
347347

348348
$this->assertFalse($this->testUser->hasRole('testRole2'));
349349
}
350+
351+
/** @test */
352+
public function a_model_that_uses_hasRoles_trait_should_not_have_users_method()
353+
{
354+
$this->assertFalse(method_exists($this->testUser, 'users'));
355+
}
350356
}

0 commit comments

Comments
 (0)