Skip to content

Commit

Permalink
allows overriding Permission/Role model in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
uwla committed May 8, 2023
1 parent e1e37a0 commit b687461
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
29 changes: 29 additions & 0 deletions src/Traits/CustomAclModels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Uwla\Lacl\Traits;

use Uwla\Lacl\Models\Permission;
use Uwla\Lacl\Models\Role;

Trait CustomAclModels
{
/**
* Get the class of Role
*
* @return class
*/
protected static function Role()
{
return Role::class;
}

/**
* Get the class of Permission
*
* @return class
*/
protected static function Permission()
{
return Permission::class;
}
}
16 changes: 8 additions & 8 deletions src/Traits/HasPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Uwla\Lacl\Traits;

use ArgumentCountError;
use BadMethodCallException;
use Exception;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -18,7 +17,7 @@

Trait HasPermission
{
use Identifiable;
use Identifiable, CustomAclModels;

/**
* Ensure this object is instance of a Role.
Expand All @@ -36,12 +35,12 @@ private function getSelfRoleId()
$roleName = $this::class . ':' . $this->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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -121,6 +120,7 @@ private function getRoleIds()
return [$id];
}


/**
* Guess the name of the permission called upon dynamic method.
*
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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());
Expand Down Expand Up @@ -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();
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/Traits/HasRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +27,7 @@ private function getBaseQuery()
return RolePermission::where('permission_id', $this->id);
}


/**
* Get the roles associated with this model
*
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.');
}
Expand Down
23 changes: 11 additions & 12 deletions src/Traits/Permissionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,7 +16,7 @@
*/
public function deletetThisModelPermissions()
{
Permission::where([
self::Permission()::where([
'model' => $this::class,
'model_id' => $this->getModelId(),
])->delete();
Expand All @@ -30,7 +29,7 @@ public function deletetThisModelPermissions()
*/
public static function deletetAllModelPermissions()
{
Permission::where('model', self::class)->delete();
self::Permission()::where('model', self::class)->delete();
}

/**
Expand All @@ -40,7 +39,7 @@ public static function deletetAllModelPermissions()
*/
public static function deletetGenericModelPermissions()
{
Permission::where([
self::Permission()::where([
'model' => self::class,
'model_id' => null
])->delete();
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -169,7 +168,7 @@ protected static function createManyPermissions($names, $modelId=null): Collecti
];
}

Permission::insert($toCreate);
self::Permission()::insert($toCreate);
return self::getManyPermissions($names, $modelId);
}

Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit b687461

Please sign in to comment.