Skip to content

Commit

Permalink
Fix hints, support int on scopePermission() (#1863) (#1908)
Browse files Browse the repository at this point in the history
Co-authored-by: Erik Niebla <ep_niebla@hotmail.com>

Co-authored-by: Erik Niebla <ep_niebla@hotmail.com>
  • Loading branch information
erikn69 and Erik Niebla committed Nov 4, 2021
1 parent 1c9304a commit ee0a50a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
33 changes: 13 additions & 20 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

trait HasPermissions
{
/** @var string */
private $permissionClass;

public static function bootHasPermissions()
Expand Down Expand Up @@ -61,7 +62,7 @@ public function permissions(): BelongsToMany
* Scope the model query to certain permissions only.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
*
* @return \Illuminate\Database\Eloquent\Builder
*/
Expand All @@ -86,9 +87,10 @@ public function scopePermission(Builder $query, $permissions): Builder
}

/**
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
*
* @return array
* @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist
*/
protected function convertToPermissionModels($permissions): array
{
Expand All @@ -102,8 +104,9 @@ protected function convertToPermissionModels($permissions): array
if ($permission instanceof Permission) {
return $permission;
}
$method = is_string($permission) ? 'findByName' : 'findById';

return $this->getPermissionClass()->findByName($permission, $this->getDefaultGuardName());
return $this->getPermissionClass()->{$method}($permission, $this->getDefaultGuardName());
}, $permissions);
}

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

/**
* @deprecated since 2.35.0
* @alias of hasPermissionTo()
*/
public function hasUncachedPermissionTo($permission, $guardName = null): bool
{
return $this->hasPermissionTo($permission, $guardName);
}

/**
* An alias to hasPermissionTo(), but avoids throwing an exception.
*
Expand All @@ -213,10 +207,9 @@ public function checkPermissionTo($permission, $guardName = null): bool
/**
* Determine if the model has any of the given permissions.
*
* @param array ...$permissions
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection ...$permissions
*
* @return bool
* @throws \Exception
*/
public function hasAnyPermission(...$permissions): bool
{
Expand All @@ -234,7 +227,7 @@ public function hasAnyPermission(...$permissions): bool
/**
* Determine if the model has all of the given permissions.
*
* @param array ...$permissions
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection ...$permissions
*
* @return bool
* @throws \Exception
Expand Down Expand Up @@ -335,7 +328,7 @@ protected function getPermissionsRelation()
/**
* Grant the given permission(s) to a role.
*
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
*
* @return $this
*/
Expand Down Expand Up @@ -392,7 +385,7 @@ function ($object) use ($permissions, $model) {
/**
* Remove all current permissions and set the given ones.
*
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
*
* @return $this
*/
Expand Down Expand Up @@ -429,7 +422,7 @@ public function getPermissionNames(): Collection
}

/**
* @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
*
* @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection
*/
Expand Down Expand Up @@ -487,7 +480,7 @@ public function forgetCachedPermissions()

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

/**
* Check if the model has Any of the requested Direct permissions.
* @param array ...$permissions
* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection ...$permissions
* @return bool
*/
public function hasAnyDirectPermission(...$permissions): bool
Expand Down
8 changes: 4 additions & 4 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ trait HasRoles
{
use HasPermissions;

/** @var string */
private $roleClass;

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

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

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

return $query->whereHas('roles', function (Builder $subQuery) use ($roles) {
Expand All @@ -114,7 +114,7 @@ protected function getRolesRelation()
/**
* Assign the given role to the model.
*
* @param array|string|int|\Spatie\Permission\Contracts\Role ...$roles
* @param array|string|int|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection ...$roles
*
* @return $this
*/
Expand Down Expand Up @@ -189,7 +189,7 @@ public function removeRole($role)
/**
* Remove all current roles and set the given ones.
*
* @param array|\Spatie\Permission\Contracts\Role|string|int ...$roles
* @param array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection|string|int ...$roles
*
* @return $this
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/HasPermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ public function it_can_scope_users_using_a_string()
$this->assertEquals(1, $scopedUsers2->count());
}

/** @test */
public function it_can_scope_users_using_a_int()
{
$user1 = User::create(['email' => 'user1@test.com']);
$user2 = User::create(['email' => 'user2@test.com']);
$user1->givePermissionTo([1, 2]);
$this->testUserRole->givePermissionTo(1);
$user2->assignRole('testRole');

$scopedUsers1 = User::permission(1)->get();
$scopedUsers2 = User::permission([2])->get();

$this->assertEquals(2, $scopedUsers1->count());
$this->assertEquals(1, $scopedUsers2->count());
}

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

0 comments on commit ee0a50a

Please sign in to comment.