From b687461fb9c1af66f6e4702eb48c3c79e10937b3 Mon Sep 17 00:00:00 2001 From: uwla Date: Mon, 8 May 2023 10:16:40 -0300 Subject: [PATCH] allows overriding Permission/Role model in traits --- src/Traits/CustomAclModels.php | 29 +++++++++++++++++++++++++++++ src/Traits/HasPermission.php | 16 ++++++++-------- src/Traits/HasRole.php | 18 +++++++++--------- src/Traits/Permissionable.php | 23 +++++++++++------------ 4 files changed, 57 insertions(+), 29 deletions(-) create mode 100644 src/Traits/CustomAclModels.php diff --git a/src/Traits/CustomAclModels.php b/src/Traits/CustomAclModels.php new file mode 100644 index 0000000..a8762c7 --- /dev/null +++ b/src/Traits/CustomAclModels.php @@ -0,0 +1,29 @@ +id; // get the role that uniquely represents this user - $role = Role::where('name', $roleName)->first(); + $role = self::Role()::where('name', $roleName)->first(); // if null, create it if ($role == null) { - $role = Role::create(['name' => $roleName]); + $role = self::Role()::create(['name' => $roleName]); $this->addRole($role); } @@ -71,7 +70,7 @@ private static function normalizePermissions($permissions, $resource=null, $ids= if (! $normalized instanceof Collection) throw new Exception('Permissions must be array or Eloquent Collection'); if (is_string($normalized->first())) - $normalized = Permission::getPermissionsByName($permissions, $resource, $ids); + $normalized = self::Permission()::getByName($permissions, $resource, $ids); else if (! $normalized->first() instanceof Permission) throw new Exception('Expected a collection of Permission. Got something else.'); return $normalized; @@ -121,6 +120,7 @@ private function getRoleIds() return [$id]; } + /** * Guess the name of the permission called upon dynamic method. * @@ -252,7 +252,7 @@ private function getThisPermissionsIds() public function getPermissions() { $ids = $this->getThisPermissionsIds(); - return Permission::whereIn('id', $ids)->get(); + return self::Permission()::whereIn('id', $ids)->get(); } /** @@ -270,7 +270,7 @@ public function getModels($class, $permissionNames=[], $addPrefix=true) if (gettype($permissionNames) == 'string') $permissionNames = [$permissionNames]; - $query = Permission::query() + $query = self::Permission()::query() ->where('model', $class) ->whereNotNull('model_id') ->whereIn('id', $this->getThisPermissionsIds()); @@ -309,7 +309,7 @@ public function getUserPermissions() ->pluck('permission_id'); // retrieve the permissions by id - return Permission::whereIn('id', $ids)->get(); + return self::Permission()::whereIn('id', $ids)->get(); } /** diff --git a/src/Traits/HasRole.php b/src/Traits/HasRole.php index 15a917c..c13fe8b 100644 --- a/src/Traits/HasRole.php +++ b/src/Traits/HasRole.php @@ -3,7 +3,6 @@ namespace Uwla\Lacl\Traits; use Exception; -use Uwla\Lacl\Exceptions\NoSuchRoleException; use Uwla\Lacl\Models\Permission; use Uwla\Lacl\Models\Role; use Uwla\Lacl\Models\RolePermission; @@ -28,6 +27,7 @@ private function getBaseQuery() return RolePermission::where('permission_id', $this->id); } + /** * Get the roles associated with this model * @@ -36,7 +36,7 @@ private function getBaseQuery() public function getRoles() { $roleIds = $this->getBaseQuery()->get()->pluck('role_id'); - return Role::whereIn('id', $roleIds)->get(); + return self::Role()::whereIn('id', $roleIds)->get(); } /** @@ -92,7 +92,7 @@ public function addRoles($roles) /** * delete single role * - * @param Uwla\Lacl\Role|string $role + * @param Role|string $role * @return void */ public function delRole($role) @@ -162,15 +162,15 @@ public function countRoles() /** * check whether this model has the given role * - * @param Uwla\Lacl\Role]|string $role + * @param Role|string $role * @return bool */ public function hasRole($role) { if (gettype($role) == 'string') - $role = Role::where('name', $role)->first(); - if (! ($role instanceof Role)) - throw new NoSuchRoleException('Role does not exist'); + $role = self::Role()::where('name', $role)->first(); + if (! $role instanceof Role) + throw new Exception('Role must be valid role'); return $this->getBaseQuery()->where('role_id', $role->id)->exists(); } @@ -251,7 +251,7 @@ public static function delRoleFromMany($role, $models) /** * delete many roles from many models * - * @param \Uwla\Lacl\Role[]|string[] $role + * @param Role[]|string[] $role * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ @@ -294,7 +294,7 @@ private static function normalizeRoles($roles) throw new Exception('Roles must not be empty'); if (is_string($roles->first())) { - $roles = Role::whereIn('name', $roles)->get(); + $roles = self::Role()::whereIn('name', $roles)->get(); if ($roles->count() != $n) throw new Exception('One or more roles do not exist.'); } diff --git a/src/Traits/Permissionable.php b/src/Traits/Permissionable.php index dbec8b1..cfa4db4 100644 --- a/src/Traits/Permissionable.php +++ b/src/Traits/Permissionable.php @@ -4,11 +4,10 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Str; -use Uwla\Lacl\Models\Permission; Trait Permissionable { - use Identifiable; + use Identifiable, CustomAclModels; /** * Delete all permissions associated with this model instance. @@ -17,7 +16,7 @@ */ public function deletetThisModelPermissions() { - Permission::where([ + self::Permission()::where([ 'model' => $this::class, 'model_id' => $this->getModelId(), ])->delete(); @@ -30,7 +29,7 @@ public function deletetThisModelPermissions() */ public static function deletetAllModelPermissions() { - Permission::where('model', self::class)->delete(); + self::Permission()::where('model', self::class)->delete(); } /** @@ -40,7 +39,7 @@ public static function deletetAllModelPermissions() */ public static function deletetGenericModelPermissions() { - Permission::where([ + self::Permission()::where([ 'model' => self::class, 'model_id' => null ])->delete(); @@ -81,9 +80,9 @@ protected static function getPrefixed($strings) * @param mixed $modelId * @return Permission */ - protected static function createPermission($permissionName, $modelId=null): Permission + protected static function createPermission($permissionName, $modelId=null) { - return Permission::firstOrCreate([ + return self::Permission()::firstOrCreate([ 'model' => self::class, 'model_id' => $modelId, 'name' => self::getPermissionPrefix() . '.' . $permissionName, @@ -97,9 +96,9 @@ protected static function createPermission($permissionName, $modelId=null): Perm * @param mixed $modelId * @return Permission */ - protected static function getPermission($permissionName, $modelId=null): Permission + protected static function getPermission($permissionName, $modelId=null) { - return Permission::where([ + return self::Permission()::where([ 'model' => self::class, 'model_id' => $modelId, 'name' => self::getPermissionPrefix() . '.' . $permissionName, @@ -169,7 +168,7 @@ protected static function createManyPermissions($names, $modelId=null): Collecti ]; } - Permission::insert($toCreate); + self::Permission()::insert($toCreate); return self::getManyPermissions($names, $modelId); } @@ -183,7 +182,7 @@ protected static function createManyPermissions($names, $modelId=null): Collecti protected static function getManyPermissions($names, $modelId=null): Collection { $names = self::getPrefixed($names); - return Permission::query() + return self::Permission()::query() ->whereIn('name', $names) ->where('model', self::class) ->where('model_id', $modelId) @@ -200,7 +199,7 @@ protected static function getManyPermissions($names, $modelId=null): Collection protected static function deleteManyPermissions($names, $modelId=null) { $names = self::getPrefixed($names); - Permission::query() + self::Permission()::query() ->whereIn('name', $names) ->where('model', self::class) ->where('model_id', $modelId)