Skip to content

Commit

Permalink
Merge pull request #2402 from erikn69/last
Browse files Browse the repository at this point in the history
[V6] Php 7.4 typed properties
  • Loading branch information
drbyte authored Apr 13, 2023
2 parents 33b016a + cd37572 commit 38f2049
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 65 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
parameters:
ignoreErrors:
-
# PHPStan can't understand what's going on in context of Role class using Builder `when`
message: "#^Call to an undefined method Spatie\\\\Permission\\\\Models\\\\Role::getRoleClass\\(\\).$#"
count: 1
path: src\Traits\HasPermissions.php
11 changes: 7 additions & 4 deletions src/Commands/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ public function handle()
$roles = $roleClass::whereGuardName($guard)
->with('permissions')
->when(config('permission.teams'), fn ($q) => $q->orderBy($team_key))
->orderBy('name')->get()->mapWithKeys(fn ($role) => [$role->name.'_'.($role->$team_key ?: '') => ['permissions' => $role->permissions->pluck('id'), $team_key => $role->$team_key]]
);
->orderBy('name')->get()->mapWithKeys(fn ($role) => [
$role->name.'_'.($role->$team_key ?: '') => ['permissions' => $role->permissions->pluck('id'), $team_key => $role->$team_key],
]);

$permissions = $permissionClass::whereGuardName($guard)->orderBy('name')->pluck('name', 'id');

$body = $permissions->map(fn ($permission, $id) => $roles->map(fn (array $role_data) => $role_data['permissions']->contains($id) ? '' : ' ·'
$body = $permissions->map(fn ($permission, $id) => $roles->map(
fn (array $role_data) => $role_data['permissions']->contains($id) ? '' : ' ·'
)->prepend($permission)
);

if (config('permission.teams')) {
$teams = $roles->groupBy($team_key)->values()->map(fn ($group, $id) => new TableCell('Team ID: '.($id ?: 'NULL'), ['colspan' => $group->count()])
$teams = $roles->groupBy($team_key)->values()->map(
fn ($group, $id) => new TableCell('Team ID: '.($id ?: 'NULL'), ['colspan' => $group->count()])
);
}

Expand Down
4 changes: 1 addition & 3 deletions src/Commands/UpgradeForTeams.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ protected function getExistingMigrationsWarning(array $existingMigrations)
$base = "Setup teams migration already exists.\nFollowing file was found: ";
}

return $base.array_reduce($existingMigrations, function ($carry, $fileName) {
return $carry."\n - ".$fileName;
});
return $base.array_reduce($existingMigrations, fn ($carry, $fileName) => $carry."\n - ".$fileName);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public static function getNames($model): Collection
protected static function getConfigAuthGuards(string $class): Collection
{
return collect(config('auth.guards'))
->map(fn ($guard) => isset($guard['provider']) ? config("auth.providers.{$guard['provider']}.model") : null
)
->map(fn ($guard) => isset($guard['provider']) ? config("auth.providers.{$guard['provider']}.model") : null)
->filter(fn ($model) => $class === $model)
->keys();
}
Expand Down
53 changes: 21 additions & 32 deletions src/PermissionRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,38 @@

class PermissionRegistrar
{
/** @var Repository */
protected $cache;
protected Repository $cache;

/** @var CacheManager */
protected $cacheManager;
protected CacheManager $cacheManager;

/** @var string */
protected $permissionClass;
protected string $permissionClass;

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

/** @var Collection|array|null */
protected $permissions;

/** @var string */
public $pivotRole;
public string $pivotRole;

/** @var string */
public $pivotPermission;
public string $pivotPermission;

/** @var \DateInterval|int */
public $cacheExpirationTime;

/** @var bool */
public $teams;
public bool $teams;

/** @var string */
public $teamsKey;
public string $teamsKey;

/** @var int|string */
protected $teamId = null;

/** @var string */
public $cacheKey;
public string $cacheKey;

/** @var array */
private $cachedRoles = [];
private array $cachedRoles = [];

/** @var array */
private $alias = [];
private array $alias = [];

/** @var array */
private $except = [];
private array $except = [];

/**
* PermissionRegistrar constructor.
Expand Down Expand Up @@ -180,9 +168,9 @@ private function loadPermissions(): void
return;
}

$this->permissions = $this->cache->remember($this->cacheKey, $this->cacheExpirationTime, function () {
return $this->getSerializedPermissionsForCache();
});
$this->permissions = $this->cache->remember(
$this->cacheKey, $this->cacheExpirationTime, fn () => $this->getSerializedPermissionsForCache()
);

// fallback for old cache method, must be removed on next mayor version
if (! isset($this->permissions['alias'])) {
Expand Down Expand Up @@ -345,11 +333,12 @@ private function getHydratedPermissionCollection(): Collection
$permissionClass = $this->getPermissionClass();
$permissionInstance = new $permissionClass();

return Collection::make(
array_map(fn ($item) => $permissionInstance
->newFromBuilder($this->aliasedArray(array_diff_key($item, ['r' => 0])))
->setRelation('roles', $this->getHydratedRoleCollection($item['r'] ?? [])), $this->permissions['permissions'])
);
return Collection::make(array_map(
fn ($item) => $permissionInstance
->newFromBuilder($this->aliasedArray(array_diff_key($item, ['r' => 0])))
->setRelation('roles', $this->getHydratedRoleCollection($item['r'] ?? [])),
$this->permissions['permissions']
));
}

private function getHydratedRoleCollection(array $roles): Collection
Expand Down
26 changes: 13 additions & 13 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@

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

/** @var string|null */
private $wildcardClass;
private ?string $wildcardClass = null;

public static function bootHasPermissions()
{
Expand Down Expand Up @@ -101,22 +99,24 @@ public function scopePermission(Builder $query, $permissions): Builder
{
$permissions = $this->convertToPermissionModels($permissions);

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

return $query->where(function (Builder $query) use ($permissions, $rolesWithPermissions) {
$query->whereHas('permissions', function (Builder $subQuery) use ($permissions) {
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));
});
if (count($rolesWithPermissions) > 0 && ! is_a($this, Role::class)) {
$query->orWhereHas('roles', function (Builder $subQuery) use ($rolesWithPermissions) {
})
->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));
});
}
});
})
)
);
}

/**
Expand Down
21 changes: 10 additions & 11 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ trait HasRoles
{
use HasPermissions;

/** @var string */
private $roleClass;
private ?string $roleClass = null;

public static function bootHasRoles()
{
Expand Down Expand Up @@ -60,11 +59,10 @@ public function roles(): BelongsToMany
return $relation;
}

$teamField = config('permission.table_names.roles').'.'.app(PermissionRegistrar::class)->teamsKey;

return $relation->wherePivot(app(PermissionRegistrar::class)->teamsKey, getPermissionsTeamId())
->where(function ($q) {
$teamField = config('permission.table_names.roles').'.'.app(PermissionRegistrar::class)->teamsKey;
$q->whereNull($teamField)->orWhere($teamField, getPermissionsTeamId());
});
->where(fn ($q) => $q->whereNull($teamField)->orWhere($teamField, getPermissionsTeamId()));
}

/**
Expand All @@ -89,11 +87,12 @@ public function scopeRole(Builder $query, $roles, $guard = null): Builder
return $this->getRoleClass()::{$method}($role, $guard ?: $this->getDefaultGuardName());
}, Arr::wrap($roles));

return $query->whereHas('roles', function (Builder $subQuery) use ($roles) {
$roleClass = $this->getRoleClass();
$key = (new $roleClass())->getKeyName();
$subQuery->whereIn(config('permission.table_names.roles').".$key", \array_column($roles, $key));
});
$roleClass = $this->getRoleClass();
$key = (new $roleClass())->getKeyName();

return $query->whereHas('roles', fn (Builder $subQuery) => $subQuery
->whereIn(config('permission.table_names.roles').".$key", \array_column($roles, $key))
);
}

/**
Expand Down

0 comments on commit 38f2049

Please sign in to comment.