From f0213d0c2a16d39c09c77b26fe35bdd376bf529a Mon Sep 17 00:00:00 2001 From: uwla Date: Fri, 6 Sep 2024 10:50:48 -0300 Subject: [PATCH] refactor: code style --- src/AclServiceProvider.php | 4 +- src/Contracts/HasPermissionContract.php | 2 +- src/Contracts/HasRoleContract.php | 2 +- src/Models/Permission.php | 137 ++++++++++--------- src/Models/PermissionModel.php | 2 +- src/Models/Role.php | 6 +- src/Models/RoleModel.php | 2 +- src/Traits/CustomAclModels.php | 22 ++-- src/Traits/HasPermission.php | 168 ++++++++++++------------ src/Traits/HasRole.php | 79 ++++++----- src/Traits/Identifiable.php | 7 +- src/Traits/Permissionable.php | 98 +++++++------- src/Traits/PermissionableHasRole.php | 18 ++- src/Traits/ResourcePolicy.php | 18 +-- tests/Feature/HasRoleTest.php | 26 ++-- tests/app/Models/HasCustomAclModels.php | 4 +- 16 files changed, 304 insertions(+), 291 deletions(-) diff --git a/src/AclServiceProvider.php b/src/AclServiceProvider.php index 0983d06..3699482 100644 --- a/src/AclServiceProvider.php +++ b/src/AclServiceProvider.php @@ -6,7 +6,7 @@ class AclServiceProvider extends ServiceProvider { - public function boot() + public function boot(): void { // publishes migrations $src = __DIR__ . '/' . '../database/migrations/create_acl_tables.php'; @@ -15,4 +15,4 @@ public function boot() ); $this->publishes([$src => $dest], 'migrations'); } -} +} \ No newline at end of file diff --git a/src/Contracts/HasPermissionContract.php b/src/Contracts/HasPermissionContract.php index a52b3d8..f381903 100644 --- a/src/Contracts/HasPermissionContract.php +++ b/src/Contracts/HasPermissionContract.php @@ -4,5 +4,5 @@ interface HasPermissionContract { - // + // HasPermissionContract } \ No newline at end of file diff --git a/src/Contracts/HasRoleContract.php b/src/Contracts/HasRoleContract.php index a7c2bf8..cd43477 100644 --- a/src/Contracts/HasRoleContract.php +++ b/src/Contracts/HasRoleContract.php @@ -4,5 +4,5 @@ interface HasRoleContract { - // + // HasRoleContract } \ No newline at end of file diff --git a/src/Models/Permission.php b/src/Models/Permission.php index aace115..a4479b5 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -2,7 +2,7 @@ namespace Uwla\Lacl\Models; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\DbCollection; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use InvalidArgumentException; @@ -10,68 +10,76 @@ class Permission extends Model { - use HasFactory, Permissionable; + use HasFactory; + use Permissionable; /** - * Get the instances of the given model which have this permission - * - * @param string $model_class - * @param string $id_column - * @return \Illuminate\Database\Eloquent\Collection - */ - public function getModels($model_class, $id_column) + * Get the instances of the given model which have this permission + * + * @param string $model_class + * @param string $id_column + * @return \Illuminate\Database\Eloquent\Collection + */ + public function getModels($model_class, $id_column): DbCollection { $ids = PermissionModel::where([ 'permission_id' => $this->id, 'model' => $model_class, ])->pluck('model_id'); - $models = $model_class::whereIn($id_column, $ids)->get(); - return $models; + return $model_class::whereIn($id_column, $ids)->get(); } /** - * Get the roles that have this permission - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public function getRoles() + * Get the roles that have this permission + * + * @return \Illuminate\Database\Eloquent\Collection + */ + public function getRoles(): DbCollection { return $this->getModels($this::Role(), 'id'); } /** - * Get the name of the roles that have this permission - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public function getRoleNames() + * Get the name of the roles. + * + * @return array The names. + */ + public function getRoleNames(): array { return $this->getRoles()->pluck('name'); } /** - * Get permissions by their name - * - * @param array|string $names The names of the permissions - * @param mixed $modelType The class name of the model (optional) - * @param mixed $models The models or their ids (optional) - * @return \Illuminate\Database\Eloquent\Collection - */ - public static function getByName($names, $modelType=null, $models=null) + * Get permissions by their name + * + * @param array|string $names The names of the permissions + * @param mixed $modelType The class name of the model (optional) + * @param mixed $models The models or their ids (optional) + * @return \Illuminate\Database\Eloquent\Collection + */ + public static function getByName($names, $modelType = null, $models = null): DbCollection { - if (is_string($names)) + if (is_string($names)) { $names = [$names]; - if (! is_array($names)) - throw new InvalidArgumentException('First arg must be string array'); + } + if (! is_array($names)) { + throw new InvalidArgumentException( + 'First arg must be string array', + ); + } $n = count($names); - if ($n == 0) - throw new InvalidArgumentException('No permission provided'); + if ($n == 0) { + throw new InvalidArgumentException( + 'No permission provided', + ); + } - if ($modelType != null) + if ($modelType != null) { $query = static::where('model', $modelType); - else + } else { $query = static::query(); + } if ($models == null) { // we are dealing with permissions for a resource group @@ -79,28 +87,25 @@ public static function getByName($names, $modelType=null, $models=null) } else { // we are dealing with permission for specific resources - if (! is_countable($models)) - { + if (! is_countable($models)) { throw new InvalidArgumentException( - 'Second arguments must be array or Collection.'); + 'Second arguments must be array or Collection.' + ); } - if (count($models) != $n) - { + if (count($models) != $n) { throw new InvalidArgumentException( - 'number of permissions and models must match'); + 'number of permissions and models must match' + ); } - if ($models instanceof Collection) - { + if ($models instanceof DbCollection) { $models = $models->pluck('id'); } // each resource is identified by its model_id - $query->where(function($q) use ($names, $models, $n) - { - for ($i = 0; $i < $n; $i+=1) - { + $query->where(function ($q) use ($names, $models, $n) { + for ($i = 0; $i < $n; $i += 1) { $q->orWhere([ ['name', $names[$i]], ['model_id', $models[$i]], @@ -113,36 +118,38 @@ public static function getByName($names, $modelType=null, $models=null) } /** - * Create one permission by the provided name - * - * @param array $names - * @return \Illuminate\Database\Eloquent\Collection - */ - public static function createOne($name) + * Create one permission by the provided name + * + * @param array $name + * @return \Illuminate\Database\Eloquent\Collection + */ + public static function createOne($name): DbCollection { return static::create(['name' => $name]); } /** - * Create permissions by the provided names - * - * @param array $names - * @return \Illuminate\Database\Eloquent\Collection - */ - public static function createMany($names) + * Create permissions by the provided names + * + * @param array $names + * @return \Illuminate\Database\Eloquent\Collection + */ + public static function createMany($names): DbCollection { - if (! is_array($names)) + if (! is_array($names)) { throw new InvalidArgumentException('Expected string array'); - if (count($names) == 0) - return new Collection(); + } + if (count($names) == 0) { + return new DbCollection(); + } // create permissions $permissionsToCreate = []; foreach ($names as $name); - $permissionsToCreate[] = ['name' => $name]; + $permissionsToCreate[] = ['name' => $name]; static::insert($permissionsToCreate); // bulk insertion // return them return static::whereIn('names', $names)->get(); } -} \ No newline at end of file +} diff --git a/src/Models/PermissionModel.php b/src/Models/PermissionModel.php index 609a845..e89ad6d 100644 --- a/src/Models/PermissionModel.php +++ b/src/Models/PermissionModel.php @@ -15,4 +15,4 @@ class PermissionModel extends Model * @var string */ protected $table = 'permissions_models'; -} +} \ No newline at end of file diff --git a/src/Models/Role.php b/src/Models/Role.php index fb69642..949dcfa 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -2,12 +2,12 @@ namespace Uwla\Lacl\Models; -use Uwla\Lacl\Contracts\HasPermissionContract; -use Uwla\Lacl\Traits\PermissionableHasRole; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Uwla\Lacl\Contracts\HasPermissionContract; +use Uwla\Lacl\Traits\PermissionableHasRole; class Role extends Model implements HasPermissionContract { use HasFactory, PermissionableHasRole; -} +} \ No newline at end of file diff --git a/src/Models/RoleModel.php b/src/Models/RoleModel.php index c1acee8..f18fd3d 100644 --- a/src/Models/RoleModel.php +++ b/src/Models/RoleModel.php @@ -15,4 +15,4 @@ class RoleModel extends Model * @var string */ protected $table = 'roles_models'; -} +} \ No newline at end of file diff --git a/src/Traits/CustomAclModels.php b/src/Traits/CustomAclModels.php index 2272c9f..3f56165 100644 --- a/src/Traits/CustomAclModels.php +++ b/src/Traits/CustomAclModels.php @@ -8,22 +8,22 @@ Trait CustomAclModels { /** - * Get the class of Role - * - * @return string - */ - protected static function Role() + * Get the class of Role + * + * @return string + */ + protected static function Role(): string { return Role::class; } /** - * Get the class of Permission - * - * @return string - */ - protected static function Permission() + * Get the class of Permission + * + * @return string + */ + protected static function Permission(): string { return Permission::class; } -} +} \ No newline at end of file diff --git a/src/Traits/HasPermission.php b/src/Traits/HasPermission.php index a23f205..f28347c 100644 --- a/src/Traits/HasPermission.php +++ b/src/Traits/HasPermission.php @@ -18,26 +18,26 @@ { use Identifiable, CustomAclModels; - /* + /** * Get the id of this model * - * @return mixed + * @return string|int */ protected function getSelfRoleId() { return $this->id; } - /* + /** * Get an eloquent collection of Permission. * The parameters permissions & ids can be an array of strings or eloquent models. * - * @param mixed $permissions The permissions to be normalized - * @param string $resource The class name of the resource model - * @param mixed $ids The ids of the resources + * @param array|Collection $permissions The permissions to be normalized + * @param string $resource The class name of the resource model + * @param array $ids The ids of the resources * @return \Illuminate\Database\Eloquent\Collection */ - private static function normalizePermissions($permissions, $resource = null, $ids = null) + private static function normalizePermissions($permissions, $resource = null, $ids = null): Collection { $normalized = $permissions; if (is_array($permissions)) @@ -51,13 +51,13 @@ private static function normalizePermissions($permissions, $resource = null, $id return $normalized; } - /* + /** * Get an eloquent collection of the given models. * - * @param mixed $models + * @param array|Collection $models * @return \Illuminate\Database\Eloquent\Collection */ - private static function normalizeModels($models) + private static function normalizeModels($models): Collection { if (is_array($models)) $models = collect($models); @@ -68,53 +68,53 @@ private static function normalizeModels($models) return $models; } - /* + /** * Get the ids of the given permissions * - * @param mixed $permissions The permissions to be normalized - * @param string $resource The class name of the resource model - * @param mixed $ids The ids of the resources + * @param array|Collection $permissions The permissions to be normalized + * @param string $resource The class name of the resource model + * @param array $ids The ids of the resources * @return \Illuminate\Support\Collection */ - private function getPermissionIds($permissions, $resource = null, $ids = null) + private function getPermissionIds($permissions, $resource = null, $ids = null): Collection { return $this::normalizePermissions($permissions, $resource, $ids)->pluck('id'); } - /* + /** * Guess the name of the permission called upon dynamic method. * * @param string $remainingMethodName The method name after removing the prefix * @return string */ - protected function guessPermissionName($remainingMethodName) + protected function guessPermissionName($remainingMethodName): string { // by the default, just lower case the first letter of it return Str::lcfirst($remainingMethodName); } - /* - * add single permission + /** + * Add single permission * - * @param mixed $permissions The permission or their names - * @param mixed $resource The model class - * @param mixed $id The model id + * @param string|Permission $permissions The permission or their names + * @param string $resource The model class + * @param string|int $id The model id * @return void */ - public function addPermission($permission, $resource = null, $id = null) + public function addPermission($permission, $resource = null, $id = null): void { $this->addPermissions([$permission], $resource, [$id]); } - /* - * add many permissions + /** + * Add many permissions * - * @param mixed $permissions The permission or their names - * @param mixed $resource The model class - * @param mixed $ids The model ids + * @param array|Collection $permissions The permission or their names + * @param string $resource The model class + * @param string|int $ids The model ids * @return void */ - public function addPermissions($permissions, $resource = null, $ids = null) + public function addPermissions($permissions, $resource = null, $ids = null): void { $permissions = static::normalizePermissions($permissions, $resource, $ids); $toAdd = []; @@ -130,28 +130,28 @@ public function addPermissions($permissions, $resource = null, $ids = null) PermissionModel::insert($toAdd); } - /* + /** * revoke a permission associated with this role * - * @param mixed $permissions The permission or their names - * @param mixed $resource The model class - * @param mixed $id The model id + * @param string|Permission $permissioni The permission or its names + * @param string $resource The model class + * @param string|int $id The model id * @return void */ - public function delPermission($permission, $resource = null, $id = null) + public function delPermission($permission, $resource = null, $id = null): void { $this->delPermissions([$permission], $resource, [$id]); } - /* + /** * revoke the given permissions associated with this role * - * @param mixed $permissions The permission or their names - * @param mixed $resource The model class - * @param mixed $ids The model ids + * @param array|Collection $permissions The permission or their names + * @param string $resource The model class + * @param array $ids The model ids * @return void */ - public function delPermissions($permissions, $resource = null, $ids = null) + public function delPermissions($permissions, $resource = null, $ids = null): void { // get ids of the permissions $ids = $this->getPermissionIds($permissions, $resource, $ids); @@ -166,7 +166,7 @@ public function delPermissions($permissions, $resource = null, $ids = null) ->delete(); } - /* + /** * revoke all permissions associated with this role * * @param mixed $permissions @@ -180,7 +180,7 @@ public function delAllPermissions() ])->delete(); } - /* + /** * set the permissions associated with this role * * @param mixed $permissions @@ -192,7 +192,7 @@ public function setPermissions($permissions) $this->addPermissions($permissions); } - /* + /** * get the permission ids associated with this object * * @return \Illuminate\Support\Collection @@ -224,7 +224,7 @@ private function getThisPermissionsIds() return $query->pluck('permission_id'); } - /* + /** * get all permissions associated with this object * * @return \Illuminate\Database\Eloquent\Collection @@ -235,7 +235,7 @@ public function getPermissions() return static::Permission()::whereIn('id', $ids)->get(); } - /* + /** * Get the models this role or user has permission to access. * * @return \Illuminate\Database\Eloquent\Collection @@ -271,7 +271,7 @@ public function getModels($class, $permissionNames = [], $addPrefix = true) return $models; } - /* + /** * Get the permissions associated with this model only, not with its roles. * * @return \Illuminate\Database\Eloquent\Collection<\Uwla\Lacl\Models\Permission> @@ -285,7 +285,7 @@ public function getSelfPermissions() return static::Permission()::whereIn('id', $ids)->get(); } - /* + /** * get the name of the permissions associated with this object * * @return \Illuminate\Support\Collection @@ -295,7 +295,7 @@ public function getPermissionNames() return $this->getPermissions()->pluck('name'); } - /* + /** * check whether this object has the given permission * * @param mixed $permission @@ -308,7 +308,7 @@ public function hasPermission($permission, $resource = null, $id = null) return $this->hasPermissions([$permission], $resource, [$id]); } - /* + /** * Executed when the object is called upon an undefined method. * We overwrote it to provide better interface to manage permissions. * @@ -341,7 +341,7 @@ public function __call($name, $arguments) return parent::__call($name, $arguments); } - /* + /** * check whether this object has the given permissions * * @param mixed $permission @@ -356,29 +356,29 @@ public function hasPermissions($permissions, $resource = null, $ids = null) return $m == $n; } - /* + /** * check whether this object has any of the given permissions * * @param mixed $permission * @param string $resource - * @param mixed $ids + * @param array $ids * @return bool */ - public function hasAnyPermission($permissions, $resource = null, $ids = null) + public function hasAnyPermission($permissions, $resource = null, $ids = null): bool { $m = $this->hasHowManyPermissions($permissions, $resource, $ids); return $m > 0; } - /* + /** * get how many of the given permissions this object has * - * @param mixed $permission - * @param string $resource - * @param mixed $ids + * @param array|Collection $permissions + * @param string $resource + * @param array $ids * @return int */ - private function hasHowManyPermissions($permissions, $resource, $ids) + private function hasHowManyPermissions($permissions, $resource, $ids): int { // the ids of the permissions $permission_ids = $this->getPermissionIds($permissions, $resource, $ids)->toArray(); @@ -390,36 +390,36 @@ private function hasHowManyPermissions($permissions, $resource, $ids) return count(array_intersect($permission_ids, $this_permission_ids)); } - /* + /** * get how many permissions this object has * * @return int */ - public function countPermissions() + public function countPermissions(): int { return $this->getThisPermissionsIds()->count(); } - /* - * add single permission to many models + /** + * Add single permission to many models * - * @param mixed $permission + * @param string|Permission $permission * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ - public static function addPermissionToMany($permission, $models) + public static function addPermissionToMany($permission, $models): void { static::addPermissionsToMany([$permission], $models); } - /* - * add many permissions to many models + /** + * Add many permissions to many models * - * @param mixed $permission + * @param array|Collection $permissions * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ - public static function addPermissionsToMany($permissions, $models) + public static function addPermissionsToMany($permissions, $models): void { $permissions = static::normalizePermissions($permissions); $models = static::normalizeModels($models); @@ -445,26 +445,26 @@ public static function addPermissionsToMany($permissions, $models) PermissionModel::insert($toCreate); } - /* - * delete a single permission from many models + /** + * Delete a single permission from many models * - * @param mixed $permission + * @param string|Permission $permission * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ - public static function delPermissionFromMany($permission, $models) + public static function delPermissionFromMany($permission, $models): void { static::delPermissionsFromMany([$permission], $models); } - /* - * delete many permissions from many models + /** + * Delete many permissions from many models * - * @param mixed $permission - * @param mixed $models + * @param array|Collection $permissions + * @param array|Collection $models * @return void */ - public static function delPermissionsFromMany($permissions, $models) + public static function delPermissionsFromMany($permissions, $models): void { $permissions = static::normalizePermissions($permissions); $models = static::normalizeModels($models); @@ -478,7 +478,7 @@ public static function delPermissionsFromMany($permissions, $models) ->delete(); } - /* + /** * Get the name of the id column of this model class * * @return string @@ -488,13 +488,13 @@ public static function getIdColumn() return 'id'; } - /* + /** * Get the given roles with their permissions * - * @param mixed $models + * @param array|Collection $models * @return \Illuminate\Database\Eloquent\Collection */ - public static function withPermissions($models) + public static function withPermissions($models): Collection { // normalize models $roles = static::normalizeModels($models); @@ -548,17 +548,17 @@ public static function withPermissions($models) return $roles; } - /* + /** * Get the given roles with their permission names * - * @param mixed $models + * @param array|Collection $models * @return \Illuminate\Database\Eloquent\Collection */ - public static function withPermissionNames($models) + public static function withPermissionNames($models): Collection { $models = static::withPermissions($models); foreach ($models as $m) $m->permissions = $m->permissions->pluck('name'); return $models; } -} +} \ No newline at end of file diff --git a/src/Traits/HasRole.php b/src/Traits/HasRole.php index 92c7daa..51a6629 100644 --- a/src/Traits/HasRole.php +++ b/src/Traits/HasRole.php @@ -3,6 +3,7 @@ namespace Uwla\Lacl\Traits; use Exception; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; use Uwla\Lacl\Models\Role; use Uwla\Lacl\Models\RoleModel; @@ -16,7 +17,7 @@ trait HasRole * * @return \Illuminate\Database\Eloquent\Builder */ - private function getBaseQuery() + private function getBaseQuery(): Builder { return RoleModel::where([ 'model' => $this::class, @@ -29,7 +30,7 @@ private function getBaseQuery() * * @return \Illuminate\Database\Eloquent\Collection */ - public function getRoles() + public function getRoles(): Collection { $role_ids = $this->getBaseQuery()->pluck('role_id'); return static::Role()::whereIn('id', $role_ids)->get(); @@ -40,7 +41,7 @@ public function getRoles() * * @return \Illuminate\Support\Collection */ - public function getRoleNames() + public function getRoleNames(): Collection { return $this->getRoles()->pluck('name'); } @@ -48,10 +49,10 @@ public function getRoleNames() /* * add single role * - * @param mixed $role + * @param string|Role $role * @return void */ - public function addRole($role) + public function addRole($role): void { $this->addRoles([$role]); } @@ -59,17 +60,14 @@ public function addRole($role) /* * add many roles * - * @param mixed $roles + * @param array|Collection $roles * @return void */ - public function addRoles($roles) + public function addRoles($roles): void { $roles = static::normalizeRoles($roles); - - // $model = $this::class; $model_id = $this->getModelId(); - $toAdd = []; foreach ($roles as $role) { $toAdd[] = [ @@ -88,7 +86,7 @@ public function addRoles($roles) * @param Role|string $role * @return void */ - public function delRole($role) + public function delRole($role): void { $this->delRoles([$role]); } @@ -96,10 +94,10 @@ public function delRole($role) /* * delete the given roles * - * @param mixed $roles + * @param array|Collection $roles * @return void */ - public function delRoles($roles) + public function delRoles($roles): void { $roles = static::normalizeRoles($roles); $ids = $roles->pluck('id'); @@ -111,7 +109,7 @@ public function delRoles($roles) * * @return void */ - public function delAllRoles() + public function delAllRoles(): void { $this->getBaseQuery()->delete(); } @@ -119,10 +117,10 @@ public function delAllRoles() /* * set a single role associated with this model * - * @param mixed $role + * @param name|Collection $role * @return void */ - public function setRole($role) + public function setRole($role): void { $this->setRoles([$role]); } @@ -130,10 +128,10 @@ public function setRole($role) /* * set the role associated with this model * - * @param mixed $roles + * @param array|Collection $roles * @return void */ - public function setRoles($roles) + public function setRoles($roles): void { // delete current user roles $this->delAllRoles(); @@ -147,7 +145,7 @@ public function setRoles($roles) * * @return int */ - public function countRoles() + public function countRoles(): int { return $this->getBaseQuery()->count(); } @@ -158,7 +156,7 @@ public function countRoles() * @param Role|string $role * @return bool */ - public function hasRole($role) + public function hasRole($role): bool { if (gettype($role) == 'string') $role = static::Role()::where('name', $role)->first(); @@ -170,10 +168,10 @@ public function hasRole($role) /* * check whether this model has the given roles * - * @param mixed $role + * @param array|Collection $roles * @return bool */ - public function hasRoles($roles) + public function hasRoles($roles): bool { return $this->hasHowManyRoles($roles) == count($roles); } @@ -181,10 +179,10 @@ public function hasRoles($roles) /* * check whether this model has any of the given roles * - * @param mixed $roles + * @param array|Collection $roles * @return bool */ - public function hasAnyRole($roles) + public function hasAnyRole($roles): bool { return $this->hasHowManyRoles($roles) > 0; } @@ -192,11 +190,11 @@ public function hasAnyRole($roles) /* * add single role to many models * - * @param mixed $role + * @param array|Collection $role * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ - public static function addRoleToMany($role, $models) + public static function addRoleToMany($role, $models): void { static::addRolesToMany([$role], $models); } @@ -204,11 +202,11 @@ public static function addRoleToMany($role, $models) /* * add many roles to many models * - * @param mixed $roles + * @param array|Collection $roles * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ - public static function addRolesToMany($roles, $models) + public static function addRolesToMany($roles, $models): void { $roles = static::normalizeRoles($roles); @@ -232,11 +230,11 @@ public static function addRolesToMany($roles, $models) /* * delete a single role from many models * - * @param mixed $role + * @param string|Role $role * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ - public static function delRoleFromMany($role, $models) + public static function delRoleFromMany($role, $models): void { static::delRolesFromMany([$role], $models); } @@ -244,11 +242,11 @@ public static function delRoleFromMany($role, $models) /* * delete many roles from many models * - * @param mixed $role + * @param array|Collection $roles * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ - public static function delRolesFromMany($roles, $models) + public static function delRolesFromMany($roles, $models): void { $roles = static::normalizeRoles($roles); $rids = $roles->pluck('id'); @@ -263,10 +261,10 @@ public static function delRolesFromMany($roles, $models) /* * Get the given models along with their roles * - * @param mixed $models + * @param array|Collection $models * @return \Illuminate\Database\Eloquent\Collection */ - public static function withRoles($models) + public static function withRoles($models): Collection { // normalize models $models = static::normalizeModels($models); @@ -315,17 +313,16 @@ public static function withRoles($models) $m->roles->add($r); } - // return the model return $models; } /* * Get the given users with their roles names * - * @param mixed $models + * @param array|Collection $models * @return \Illuminate\Database\Eloquent\Collection */ - public static function withRoleNames($models) + public static function withRoleNames($models): Collection { $users = static::withRoles($models); foreach ($users as $u) @@ -336,10 +333,10 @@ public static function withRoleNames($models) /* * Get how many of the given roles this model has * - * @param mixed $roles + * @param name|Collection $roles * @return int */ - private function hasHowManyRoles($roles) + private function hasHowManyRoles($roles): int { $roles = static::normalizeRoles($roles); $ids = $roles->pluck('id'); @@ -350,10 +347,10 @@ private function hasHowManyRoles($roles) /* * Normalize $roles into an Eloquent Collection of Role * - * @param mixed $roles + * @param array|Collection $roles * @return \Illuminate\Database\Eloquent\Collection */ - private static function normalizeRoles($roles) + private static function normalizeRoles($roles): Collection { if (is_array($roles)) $roles = collect($roles); diff --git a/src/Traits/Identifiable.php b/src/Traits/Identifiable.php index 483cd91..8209f97 100644 --- a/src/Traits/Identifiable.php +++ b/src/Traits/Identifiable.php @@ -4,8 +4,13 @@ Trait Identifiable { + /** + * Get the model id value. + * + * @return string|int The id of the instance. + */ public function getModelId() { return $this->id; } -} +} \ No newline at end of file diff --git a/src/Traits/Permissionable.php b/src/Traits/Permissionable.php index 113d656..a985d5e 100644 --- a/src/Traits/Permissionable.php +++ b/src/Traits/Permissionable.php @@ -2,19 +2,21 @@ namespace Uwla\Lacl\Traits; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\DbCollection; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; -Trait Permissionable +trait Permissionable { - use Identifiable, CustomAclModels; + use Identifiable; + use CustomAclModels; /** * Delete all permissions associated with this model instance. * * @return void */ - public function deleteThisModelPermissions() + public function deleteThisModelPermissions(): void { static::Permission()::where([ 'model' => $this::class, @@ -27,7 +29,7 @@ public function deleteThisModelPermissions() * * @return void */ - public static function deleteAllModelPermissions() + public static function deleteAllModelPermissions(): void { static::Permission()::where('model', static::class)->delete(); } @@ -37,7 +39,7 @@ public static function deleteAllModelPermissions() * * @return void */ - public static function deleteGenericModelPermissions() + public static function deleteGenericModelPermissions(): void { static::Permission()::where([ 'model' => static::class, @@ -50,7 +52,7 @@ public static function deleteGenericModelPermissions() * * @return string */ - protected static function getPermissionPrefix() + protected static function getPermissionPrefix(): string { // @see https://stackoverflow.com/questions/4636166/only-variables-should-be-passed-by-reference $tmp = explode('\\', static::class); @@ -60,26 +62,27 @@ protected static function getPermissionPrefix() /** * Prefix the given strings with this model permission prefix * - * @param array $permissionName + * @param array $permissionNames * @return array */ - protected static function getPrefixed($strings) + protected static function getPrefixed($permissionNames): array { $prefix = static::getPermissionPrefix(); $result = []; - foreach ($strings as $string) - $result[] = $prefix . '.' . $string; + foreach ($permissionNames as $name) { + $result[] = $prefix . '.' . $name; + } return $result; } /** * Create a permission associated with this model given the permission name. * - * @param string $permissionName - * @param mixed $model_id - * @return \Uwla\Lacl\Models\Permission|mixed + * @param string $permissionName + * @param string|int $model_id + * @return \Uwla\Lacl\Models\Permission|Model */ - protected static function createPermission($permissionName, $model_id=null) + protected static function createPermission($permissionName, $model_id = null): Model { return static::Permission()::firstOrCreate([ 'model' => static::class, @@ -91,11 +94,11 @@ protected static function createPermission($permissionName, $model_id=null) /** * Get the permission associated with this model given the permission name. * - * @param string $permissionName - * @param mixed $model_id - * @return \Uwla\Lacl\Models\Permission|mixed + * @param string $permissionName + * @param string|int $model_id + * @return \Uwla\Lacl\Models\Permission|Model */ - protected static function getPermission($permissionName, $model_id=null) + protected static function getPermission($permissionName, $model_id = null): Model { return static::Permission()::where([ 'model' => static::class, @@ -107,11 +110,11 @@ protected static function getPermission($permissionName, $model_id=null) /** * Delete the permission associated with this model given the permission name. * - * @param string $permissionName - * @param mixed $model_id + * @param string $permissionName + * @param string|int $model_id * @return void */ - protected static function deletePermission($permissionName, $model_id=null) + protected static function deletePermission($permissionName, $model_id = null): void { static::getPermission($permissionName, $model_id)->delete(); } @@ -120,11 +123,11 @@ protected static function deletePermission($permissionName, $model_id=null) * Grant the permission to the user or role. * * @param HasPermission $model - * @param string $permissionName - * @param mixed $model_id + * @param string $permissionName + * @param string|int $model_id * @return void */ - protected static function grantPermission($model, $permissionName, $model_id=null) + protected static function grantPermission($model, $permissionName, $model_id = null): void { $permission = static::getPermission($permissionName, $model_id); $model->addPermission($permission); @@ -134,11 +137,11 @@ protected static function grantPermission($model, $permissionName, $model_id=nul * Revoke the permission from the user or role. * * @param HasPermission $model - * @param string $permissionName - * @param mixed $model_id + * @param string $permissionName + * @param string|int $model_id * @return void */ - protected static function revokePermission($model, $permissionName, $model_id=null) + protected static function revokePermission($model, $permissionName, $model_id = null): void { $permission = static::getPermission($permissionName, $model_id); $model->delPermission($permission); @@ -150,16 +153,15 @@ protected static function revokePermission($model, $permissionName, $model_id=nu * Create the permission associated with this model. * * @param array $names - * @param mixed $model_id - * @return \Illuminate\Database\Eloquent\Collection + * @param string|int $model_id + * @return \Illuminate\Database\Eloquent\DbCollection */ - protected static function createManyPermissions($names, $model_id=null): Collection + protected static function createManyPermissions($names, $model_id = null): DbCollection { $permission_names = static::getPrefixed($names); $toCreate = []; - foreach ($permission_names as $name) - { + foreach ($permission_names as $name) { $toCreate[] = [ 'name' => $name, 'model' => static::class, @@ -176,9 +178,9 @@ protected static function createManyPermissions($names, $model_id=null): Collect * * @param array $names * @param mixed $model_id - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\DbCollection */ - protected static function getManyPermissions($names, $model_id=null): Collection + protected static function getManyPermissions($names, $model_id = null): DbCollection { $names = static::getPrefixed($names); return static::Permission()::query() @@ -192,10 +194,10 @@ protected static function getManyPermissions($names, $model_id=null): Collection * Delete the permissions associated with this model. * * @param array $names - * @param mixed $model_id + * @param string|int $model_id * @return void */ - protected static function deleteManyPermissions($names, $model_id=null) + protected static function deleteManyPermissions($names, $model_id = null): void { $names = static::getPrefixed($names); static::Permission()::query() @@ -213,7 +215,7 @@ protected static function deleteManyPermissions($names, $model_id=null) * @param mixed $model_id * @return void */ - protected static function grantManyPermissions($model, $names, $model_id=null) + protected static function grantManyPermissions($model, $names, $model_id = null): void { $permissions = static::getManyPermissions($names, $model_id); $model->addPermissions($permissions); @@ -224,10 +226,10 @@ protected static function grantManyPermissions($model, $names, $model_id=null) * * @param HasPermission $model * @param array $names - * @param mixed $model_id + * @param string|id $model_id * @return void */ - protected static function revokeManyPermissions($model, $names, $model_id=null) + protected static function revokeManyPermissions($model, $names, $model_id = null): void { $permissions = static::getManyPermissions($names, $model_id); $model->delPermissions($permissions); @@ -245,8 +247,7 @@ public function __call($name, $arguments) { $pattern = '/^(get|create|delete|grant|attach|revoke)([A-Za-z]+)Permissions?$/'; $matches = []; - if (preg_match($pattern, $name, $matches)) - { + if (preg_match($pattern, $name, $matches)) { // Here use Str::replace to ensure backward compatibility with // previous interface in order to avoid introducing breaking changes. // The `attach` methods were replace by `grant`. @@ -254,8 +255,7 @@ public function __call($name, $arguments) $method = $operation . 'Permission'; $permission_name = Str::lcfirst($matches[2]); - if ($permission_name == 'crud') - { + if ($permission_name == 'crud') { $permission_name = ['view', 'update', 'delete']; $method = $operation . 'ManyPermissions'; } @@ -268,7 +268,7 @@ public function __call($name, $arguments) return parent::__call($name, $arguments); } - /** + /** * Gets triggered when an unknown method is called upon the this object. * We use it to provide syntax sugar for calling some methods. * @@ -280,8 +280,7 @@ public static function __callStatic($name, $arguments) { $pattern = '/^(get|create|delete|grant|attach|revoke)([a-zA-Z]+)Permissions?$/'; $matches = []; - if (preg_match($pattern, $name, $matches)) - { + if (preg_match($pattern, $name, $matches)) { // Here use Str::replace to ensure backward compatibility with // previous interface in order to avoid introducing breaking changes. // The `attach` methods were replace by `grant`. @@ -289,8 +288,7 @@ public static function __callStatic($name, $arguments) $method = $operation . 'Permission'; $permission_name = Str::lcfirst($matches[2]); - if ($permission_name == 'crud') - { + if ($permission_name == 'crud') { $permission_name = ['create', 'viewAny', 'updateAny', 'deleteAny']; $method = $operation . 'ManyPermissions'; } @@ -301,4 +299,4 @@ public static function __callStatic($name, $arguments) return parent::__callStatic($name, $arguments); } -} +} \ No newline at end of file diff --git a/src/Traits/PermissionableHasRole.php b/src/Traits/PermissionableHasRole.php index 7ea6aa2..46e4c13 100644 --- a/src/Traits/PermissionableHasRole.php +++ b/src/Traits/PermissionableHasRole.php @@ -8,11 +8,17 @@ { use Permissionable, HasRole; - // override __call from Permissionable and HasPermission - // in order to avoid conflict + /** + * Call magic method. + * Override __call from Permissionable and HasPermission to avoid conflict. + * + * @param string $name The name of the method + * @param array $arguments The arguments passed + * @return mixed + */ public function __call($name, $arguments) { - // PART 1 + // get|create|delete|grant|attach|revoke Permission $pattern = '/^(get|create|delete|grant|attach|revoke)([A-Za-z]+)Permissions?$/'; $matches = []; if (preg_match($pattern, $name, $matches)) @@ -35,7 +41,7 @@ public function __call($name, $arguments) return call_user_func_array(array(static::class, $method), $arguments); } - // PART 2 + // add|has|del PermissionTo ${Model} $pattern = '/^(add|has|del)PermissionTo([A-Za-z]+)$/'; $matches = []; if (preg_match($pattern, $name, $matches)) @@ -58,7 +64,7 @@ public function __call($name, $arguments) return call_user_func_array(array($this, $method), $args); } - // PART 3 + // FALLBACK return parent::__call($name, $arguments); } -} +} \ No newline at end of file diff --git a/src/Traits/ResourcePolicy.php b/src/Traits/ResourcePolicy.php index 139e351..786a5f0 100644 --- a/src/Traits/ResourcePolicy.php +++ b/src/Traits/ResourcePolicy.php @@ -13,10 +13,10 @@ * * @param User $user the authenticated user * @param array $permissions the name of the permissions - * @param array $model the id of the models + * @param array $models the id of the models * @return \Illuminate\Auth\Access\Response|bool */ - public function userHasPermission(User $user, $permissions, $models) + public function userHasPermission(User $user, $permissions, $models): bool { // the model is the class name and the namespace // for example: User @@ -41,7 +41,7 @@ public function userHasPermission(User $user, $permissions, $models) * @param User $user * @return \Illuminate\Auth\Access\Response|bool */ - public function viewAny(User $user) + public function viewAny(User $user): bool { return $this->userHasPermission($user, ['viewAny'], [null]); } @@ -53,7 +53,7 @@ public function viewAny(User $user) * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Auth\Access\Response|bool */ - public function view(User $user, Model $model) + public function view(User $user, Model $model): bool { return $this->userHasPermission($user, ['view', 'viewAny'], [$model->id, null]); } @@ -64,7 +64,7 @@ public function view(User $user, Model $model) * @param User $user * @return \Illuminate\Auth\Access\Response|bool */ - public function create(User $user) + public function create(User $user): bool { return $this->userHasPermission($user, ['create'], [null]); } @@ -76,7 +76,7 @@ public function create(User $user) * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Auth\Access\Response|bool */ - public function update(User $user, Model $model) + public function update(User $user, Model $model): bool { return $this->userHasPermission($user, ['update', 'updateAny'], [$model->id, null]); } @@ -88,7 +88,7 @@ public function update(User $user, Model $model) * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Auth\Access\Response|bool */ - public function delete(User $user, Model $model) + public function delete(User $user, Model $model): bool { return $this->userHasPermission($user, ['delete', 'deleteAny'], [$model->id, null]); } @@ -100,7 +100,7 @@ public function delete(User $user, Model $model) * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Auth\Access\Response|bool */ - public function restore(User $user, Model $model) + public function restore(User $user, Model $model): bool { return $this->userHasPermission($user, ['restore', 'restoreAny'], [$model->id, null]); } @@ -112,7 +112,7 @@ public function restore(User $user, Model $model) * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Auth\Access\Response|bool */ - public function forceDelete(User $user, Model $model) + public function forceDelete(User $user, Model $model): bool { return $this->userHasPermission($user, ['forceDelete', 'forceDeleteAny'], [$model->id, null]); } diff --git a/tests/Feature/HasRoleTest.php b/tests/Feature/HasRoleTest.php index 8df6e2c..0311777 100644 --- a/tests/Feature/HasRoleTest.php +++ b/tests/Feature/HasRoleTest.php @@ -28,7 +28,7 @@ class HasRoleTest extends TestCase * * @return void */ - public function test_add_role() + public function test_add_role(): void { $user = User::factory()->createOne(); $role = Role::factory()->createOne(); @@ -48,7 +48,7 @@ public function test_add_role() * * @return void */ - public function test_add_roles() + public function test_add_roles(): void { $user = User::factory()->createOne(); $roles = Role::factory($this->m)->create(); @@ -68,7 +68,7 @@ public function test_add_roles() * * @return void */ - public function test_get_roles() + public function test_get_roles(): void { $user = User::factory()->createOne(); $roles = Role::factory($this->m)->create(); @@ -83,7 +83,7 @@ public function test_get_roles() * * @return void */ - public function test_has_role() + public function test_has_role(): void { $user = User::factory()->createOne(); $role = Role::factory()->createOne(); @@ -99,7 +99,7 @@ public function test_has_role() * * @return void */ - public function test_has_roles() + public function test_has_roles(): void { $user = User::factory()->createOne(); $roles = Role::factory($this->m)->create(); @@ -115,7 +115,7 @@ public function test_has_roles() * * @return void */ - public function test_has_any_roles() + public function test_has_any_roles(): void { $user = User::factory()->createOne(); $roles = Role::factory($this->m)->create(); @@ -131,7 +131,7 @@ public function test_has_any_roles() * * @return void */ - public function test_set_role() + public function test_set_role(): void { $user = User::factory()->createOne(); $roles = Role::factory($this->m)->create(); @@ -149,7 +149,7 @@ public function test_set_role() * * @return void */ - public function test_set_roles() + public function test_set_roles(): void { $user = User::factory()->createOne(); $oldRoles = Role::factory($this->m)->create(); @@ -170,7 +170,7 @@ public function test_set_roles() * * @return void */ - public function test_del_roles() + public function test_del_roles(): void { $m = $this->m; $n = $m * 5; @@ -188,7 +188,7 @@ public function test_del_roles() * * @return void */ - public function test_del_all_roles() + public function test_del_all_roles(): void { $m = $this->m; $user = User::factory()->createOne(); @@ -205,7 +205,7 @@ public function test_del_all_roles() * * @return void */ - public function test_mass_role_attribution() + public function test_mass_role_attribution(): void { $n = $this->n; $m = $this->m; @@ -233,7 +233,7 @@ public function test_mass_role_attribution() * * @return void */ - public function test_get_users_with_their_roles() + public function test_get_users_with_their_roles(): void { $n = $this->n; $m = $this->m; @@ -246,4 +246,4 @@ public function test_get_users_with_their_roles() foreach ($users as $u) $this->assertEquals($u->roles, $u->getRoleNames()); } -} +} \ No newline at end of file diff --git a/tests/app/Models/HasCustomAclModels.php b/tests/app/Models/HasCustomAclModels.php index 7202327..7cf697b 100644 --- a/tests/app/Models/HasCustomAclModels.php +++ b/tests/app/Models/HasCustomAclModels.php @@ -9,7 +9,7 @@ * * @return string */ - protected static function Role() + protected static function Role(): string { return Role::class; } @@ -19,7 +19,7 @@ protected static function Role() * * @return string */ - protected static function Permission() + protected static function Permission(): string { return Permission::class; }