Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V4] Fix hints, support int on scopePermission() #1863

Merged
merged 1 commit into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 13 additions & 20 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

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

public static function bootHasPermissions()
Expand Down Expand Up @@ -55,7 +56,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 @@ -80,9 +81,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 @@ -96,8 +98,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 @@ -178,15 +181,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 @@ -207,10 +201,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 @@ -228,7 +221,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 @@ -314,7 +307,7 @@ public function getAllPermissions(): Collection
/**
* 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 @@ -365,7 +358,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 @@ -400,7 +393,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 @@ -458,7 +451,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 @@ -476,7 +469,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 @@ -12,6 +12,7 @@ trait HasRoles
{
use HasPermissions;

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

public static function bootHasRoles()
Expand Down Expand Up @@ -73,9 +74,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 @@ -86,7 +86,7 @@ public function scopeRole(Builder $query, $roles, $guard = null): Builder
/**
* 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 @@ -153,7 +153,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