Skip to content

Commit

Permalink
code reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
angeljqv committed Apr 14, 2023
1 parent 38f2049 commit 0a4e6bf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 36 deletions.
6 changes: 2 additions & 4 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ protected function registerCommands()

protected function registerModelBindings()
{
$this->app->bind(PermissionContract::class, fn ($app) => $app->make($app->config['permission.models.permission'])
);
$this->app->bind(RoleContract::class, fn ($app) => $app->make($app->config['permission.models.role'])
);
$this->app->bind(PermissionContract::class, fn ($app) => $app->make($app->config['permission.models.permission']));
$this->app->bind(RoleContract::class, fn ($app) => $app->make($app->config['permission.models.role']));
}

public static function bladeMethodWrapper($method, $role, $guard = null)
Expand Down
43 changes: 22 additions & 21 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,23 @@ public function scopePermission(Builder $query, $permissions): Builder
{
$permissions = $this->convertToPermissionModels($permissions);

$permissionClass = $this->getPermissionClass();
$permissionKey = (new $permissionClass())->getKeyName();
$roleClass = $this->getRoleClass();
$roleKey = (new $roleClass())->getKeyName();

$rolesWithPermissions = is_a($this, Role::class) ? [] : array_unique(
array_reduce($permissions, fn ($result, $permission) => array_merge($result, $permission->roles->all()), [])
);

return $query->where(fn (Builder $query) => $query
->whereHas('permissions', function (Builder $subQuery) use ($permissions) {
$permissionClass = $this->getPermissionClass();
$key = (new $permissionClass())->getKeyName();
$subQuery->whereIn(config('permission.table_names.permissions').".$key", \array_column($permissions, $key));
})
->when(count($rolesWithPermissions) > 0, fn ($subQuery) => $subQuery
->orWhereHas('roles', function (Builder $subQuery) use ($rolesWithPermissions) {
$roleClass = $this->getRoleClass();
$key = (new $roleClass())->getKeyName();
$subQuery->whereIn(config('permission.table_names.roles').".$key", \array_column($rolesWithPermissions, $key));
})
->whereHas('permissions', fn (Builder $subQuery) => $subQuery
->whereIn(config('permission.table_names.permissions').".$permissionKey", \array_column($permissions, $permissionKey))
)
->when(count($rolesWithPermissions), fn ($whenQuery) => $whenQuery
->orWhereHas('roles', fn (Builder $subQuery) => $subQuery
->whereIn(config('permission.table_names.roles').".$roleKey", \array_column($rolesWithPermissions, $roleKey))
)
)
);
}
Expand All @@ -139,7 +140,7 @@ protected function convertToPermissionModels($permissions): array
$permission = $permission->value;
}

$method = is_string($permission) && ! PermissionRegistrar::isUid($permission) ? 'findByName' : 'findById';
$method = is_int($permission) || PermissionRegistrar::isUid($permission) ? 'findById' : 'findByName';

return $this->getPermissionClass()::{$method}($permission, $this->getDefaultGuardName());
}, Arr::wrap($permissions));
Expand All @@ -159,15 +160,15 @@ public function filterPermission($permission, $guardName = null)
$permission = $permission->value;
}

if (is_string($permission) && ! PermissionRegistrar::isUid($permission)) {
$permission = $this->getPermissionClass()::findByName(
if (is_int($permission) || PermissionRegistrar::isUid($permission)) {
$permission = $this->getPermissionClass()::findById(
$permission,
$guardName ?? $this->getDefaultGuardName()
);
}

if (is_int($permission) || is_string($permission)) {
$permission = $this->getPermissionClass()::findById(
if (is_string($permission)) {
$permission = $this->getPermissionClass()::findByName(
$permission,
$guardName ?? $this->getDefaultGuardName()
);
Expand Down Expand Up @@ -209,6 +210,10 @@ protected function hasWildcardPermission($permission, $guardName = null): bool
{
$guardName = $guardName ?? $this->getDefaultGuardName();

if ($permission instanceof \BackedEnum) {
$permission = $permission->value;
}

if (is_int($permission) || PermissionRegistrar::isUid($permission)) {
$permission = $this->getPermissionClass()::findById($permission, $guardName);
}
Expand All @@ -217,10 +222,6 @@ protected function hasWildcardPermission($permission, $guardName = null): bool
$permission = $permission->name;
}

if ($permission instanceof \BackedEnum) {
$permission = $permission->value;
}

if (! is_string($permission)) {
throw WildcardPermissionInvalidArgument::create();
}
Expand Down Expand Up @@ -461,7 +462,7 @@ protected function getStoredPermission($permissions)
$permissions = $permissions->value;
}

if (is_numeric($permissions) || PermissionRegistrar::isUid($permissions)) {
if (is_int($permissions) || PermissionRegistrar::isUid($permissions)) {
return $this->getPermissionClass()::findById($permissions, $this->getDefaultGuardName());
}

Expand Down
18 changes: 9 additions & 9 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function scopeRole(Builder $query, $roles, $guard = null): Builder
return $role;
}

$method = is_numeric($role) || PermissionRegistrar::isUid($role) ? 'findById' : 'findByName';
$method = is_int($role) || PermissionRegistrar::isUid($role) ? 'findById' : 'findByName';

return $this->getRoleClass()::{$method}($role, $guard ?: $this->getDefaultGuardName());
}, Arr::wrap($roles));
Expand Down Expand Up @@ -209,13 +209,7 @@ public function hasRole($roles, string $guard = null): bool
$roles = $roles->value;
}

if (is_string($roles) && ! PermissionRegistrar::isUid($roles)) {
return $guard
? $this->roles->where('guard_name', $guard)->contains('name', $roles)
: $this->roles->contains('name', $roles);
}

if (is_int($roles) || is_string($roles)) {
if (is_int($roles) || PermissionRegistrar::isUid($roles)) {
$roleClass = $this->getRoleClass();
$key = (new $roleClass())->getKeyName();

Expand All @@ -224,6 +218,12 @@ public function hasRole($roles, string $guard = null): bool
: $this->roles->contains($key, $roles);
}

if (is_string($roles)) {
return $guard
? $this->roles->where('guard_name', $guard)->contains('name', $roles)
: $this->roles->contains('name', $roles);
}

if ($roles instanceof Role) {
return $this->roles->contains($roles->getKeyName(), $roles->getKey());
}
Expand Down Expand Up @@ -347,7 +347,7 @@ protected function getStoredRole($role): Role
$role = $role->value;
}

if (is_numeric($role) || PermissionRegistrar::isUid($role)) {
if (is_int($role) || PermissionRegistrar::isUid($role)) {
return $this->getRoleClass()::findById($role, $this->getDefaultGuardName());
}

Expand Down
4 changes: 2 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
function getModelForGuard(string $guard)
{
return collect(config('auth.guards'))
->map(fn ($guard) => isset($guard['provider']) ? config("auth.providers.{$guard['provider']}.model") : null
)->get($guard);
->map(fn ($guard) => isset($guard['provider']) ? config("auth.providers.{$guard['provider']}.model") : null)
->get($guard);
}
}

Expand Down

0 comments on commit 0a4e6bf

Please sign in to comment.