Skip to content

Commit ee0a50a

Browse files
authored
Fix hints, support int on scopePermission() (#1863) (#1908)
Co-authored-by: Erik Niebla <[email protected]> Co-authored-by: Erik Niebla <[email protected]>
1 parent 1c9304a commit ee0a50a

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

src/Traits/HasPermissions.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
trait HasPermissions
1818
{
19+
/** @var string */
1920
private $permissionClass;
2021

2122
public static function bootHasPermissions()
@@ -61,7 +62,7 @@ public function permissions(): BelongsToMany
6162
* Scope the model query to certain permissions only.
6263
*
6364
* @param \Illuminate\Database\Eloquent\Builder $query
64-
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
65+
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
6566
*
6667
* @return \Illuminate\Database\Eloquent\Builder
6768
*/
@@ -86,9 +87,10 @@ public function scopePermission(Builder $query, $permissions): Builder
8687
}
8788

8889
/**
89-
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
90+
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
9091
*
9192
* @return array
93+
* @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
9294
*/
9395
protected function convertToPermissionModels($permissions): array
9496
{
@@ -102,8 +104,9 @@ protected function convertToPermissionModels($permissions): array
102104
if ($permission instanceof Permission) {
103105
return $permission;
104106
}
107+
$method = is_string($permission) ? 'findByName' : 'findById';
105108

106-
return $this->getPermissionClass()->findByName($permission, $this->getDefaultGuardName());
109+
return $this->getPermissionClass()->{$method}($permission, $this->getDefaultGuardName());
107110
}, $permissions);
108111
}
109112

@@ -184,15 +187,6 @@ protected function hasWildcardPermission($permission, $guardName = null): bool
184187
return false;
185188
}
186189

187-
/**
188-
* @deprecated since 2.35.0
189-
* @alias of hasPermissionTo()
190-
*/
191-
public function hasUncachedPermissionTo($permission, $guardName = null): bool
192-
{
193-
return $this->hasPermissionTo($permission, $guardName);
194-
}
195-
196190
/**
197191
* An alias to hasPermissionTo(), but avoids throwing an exception.
198192
*
@@ -213,10 +207,9 @@ public function checkPermissionTo($permission, $guardName = null): bool
213207
/**
214208
* Determine if the model has any of the given permissions.
215209
*
216-
* @param array ...$permissions
210+
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection ...$permissions
217211
*
218212
* @return bool
219-
* @throws \Exception
220213
*/
221214
public function hasAnyPermission(...$permissions): bool
222215
{
@@ -234,7 +227,7 @@ public function hasAnyPermission(...$permissions): bool
234227
/**
235228
* Determine if the model has all of the given permissions.
236229
*
237-
* @param array ...$permissions
230+
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection ...$permissions
238231
*
239232
* @return bool
240233
* @throws \Exception
@@ -335,7 +328,7 @@ protected function getPermissionsRelation()
335328
/**
336329
* Grant the given permission(s) to a role.
337330
*
338-
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
331+
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
339332
*
340333
* @return $this
341334
*/
@@ -392,7 +385,7 @@ function ($object) use ($permissions, $model) {
392385
/**
393386
* Remove all current permissions and set the given ones.
394387
*
395-
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
388+
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
396389
*
397390
* @return $this
398391
*/
@@ -429,7 +422,7 @@ public function getPermissionNames(): Collection
429422
}
430423

431424
/**
432-
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
425+
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
433426
*
434427
* @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection
435428
*/
@@ -487,7 +480,7 @@ public function forgetCachedPermissions()
487480

488481
/**
489482
* Check if the model has All of the requested Direct permissions.
490-
* @param array ...$permissions
483+
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection ...$permissions
491484
* @return bool
492485
*/
493486
public function hasAllDirectPermissions(...$permissions): bool
@@ -505,7 +498,7 @@ public function hasAllDirectPermissions(...$permissions): bool
505498

506499
/**
507500
* Check if the model has Any of the requested Direct permissions.
508-
* @param array ...$permissions
501+
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection ...$permissions
509502
* @return bool
510503
*/
511504
public function hasAnyDirectPermission(...$permissions): bool

src/Traits/HasRoles.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ trait HasRoles
1313
{
1414
use HasPermissions;
1515

16+
/** @var string */
1617
private $roleClass;
1718

1819
public static function bootHasRoles()
@@ -86,9 +87,8 @@ public function scopeRole(Builder $query, $roles, $guard = null): Builder
8687
}
8788

8889
$method = is_numeric($role) ? 'findById' : 'findByName';
89-
$guard = $guard ?: $this->getDefaultGuardName();
9090

91-
return $this->getRoleClass()->{$method}($role, $guard);
91+
return $this->getRoleClass()->{$method}($role, $guard ?: $this->getDefaultGuardName());
9292
}, $roles);
9393

9494
return $query->whereHas('roles', function (Builder $subQuery) use ($roles) {
@@ -114,7 +114,7 @@ protected function getRolesRelation()
114114
/**
115115
* Assign the given role to the model.
116116
*
117-
* @param array|string|int|\Spatie\Permission\Contracts\Role ...$roles
117+
* @param array|string|int|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection ...$roles
118118
*
119119
* @return $this
120120
*/
@@ -189,7 +189,7 @@ public function removeRole($role)
189189
/**
190190
* Remove all current roles and set the given ones.
191191
*
192-
* @param array|\Spatie\Permission\Contracts\Role|string|int ...$roles
192+
* @param array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection|string|int ...$roles
193193
*
194194
* @return $this
195195
*/

tests/HasPermissionsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ public function it_can_scope_users_using_a_string()
6565
$this->assertEquals(1, $scopedUsers2->count());
6666
}
6767

68+
/** @test */
69+
public function it_can_scope_users_using_a_int()
70+
{
71+
$user1 = User::create(['email' => '[email protected]']);
72+
$user2 = User::create(['email' => '[email protected]']);
73+
$user1->givePermissionTo([1, 2]);
74+
$this->testUserRole->givePermissionTo(1);
75+
$user2->assignRole('testRole');
76+
77+
$scopedUsers1 = User::permission(1)->get();
78+
$scopedUsers2 = User::permission([2])->get();
79+
80+
$this->assertEquals(2, $scopedUsers1->count());
81+
$this->assertEquals(1, $scopedUsers2->count());
82+
}
83+
6884
/** @test */
6985
public function it_can_scope_users_using_an_array()
7086
{

0 commit comments

Comments
 (0)