Skip to content

Commit

Permalink
refact: update table names to match Laravel standards
Browse files Browse the repository at this point in the history
This will make it easier to use Laravel morphToMany relation.
  • Loading branch information
uwla committed Nov 20, 2024
1 parent 95f017e commit edac744
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 77 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ $article = Article::first();

$permission = Permission::create([
'name' => 'article.edit', // can be any name, but standards help automation
'model' => Article::class,
'model_type' => Article::class,
'model_id' => $article->id;
]);

Expand Down Expand Up @@ -592,7 +592,7 @@ protected static function boot() {
parent::boot();
static::deleted(function($model) {
Permission::where([
'model' => $model::class,
'model_type' => $model::class,
'model_id' => $model->id,
])->delete();
});
Expand Down
22 changes: 11 additions & 11 deletions database/migrations/create_acl_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public function up()
Schema::create('permissions', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('model')->nullable();
$table->string('model_type')->nullable();
$table->string('model_id')->nullable();
$table->string('description')->nullable();
$table->unique(['name', 'model', 'model_id']);
$table->unique(['name', 'model_type', 'model_id']);
$table->timestamps();
});

Expand All @@ -30,27 +30,27 @@ public function up()
$table->timestamps();
});

Schema::create('permissions_models', function (Blueprint $table) {
$table->string('model');
Schema::create('permission_model', function (Blueprint $table) {
$table->string('model_type');
$table->string('model_id');
$table->unsignedBigInteger('permission_id');
$table->foreign('permission_id')
->references('id')
->on('permissions')
->cascadeOnDelete();
$table->primary(['model', 'model_id', 'permission_id']);
$table->primary(['model_type', 'model_id', 'permission_id']);
$table->timestamps();
});

Schema::create('roles_models', function (Blueprint $table) {
$table->string('model');
Schema::create('role_model', function (Blueprint $table) {
$table->string('model_type');
$table->string('model_id');
$table->unsignedBigInteger('role_id');
$table->foreign('role_id')
->references('id')
->on('roles')
->cascadeOnDelete();
$table->primary(['model', 'model_id', 'role_id']);
$table->primary(['model_type', 'model_id', 'role_id']);
$table->timestamps();
});
}
Expand All @@ -62,9 +62,9 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('permissions_models');
Schema::dropIfExists('roles_models');
Schema::dropIfExists('permission_model');
Schema::dropIfExists('role_model');
Schema::dropIfExists('roles');
Schema::dropIfExists('permissions');
}
};
};
4 changes: 2 additions & 2 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getModels($model_class, $id_column): Collection
{
$ids = PermissionModel::where([
'permission_id' => $this->id,
'model' => $model_class,
'model_type' => $model_class,
])->pluck('model_id');
return $model_class::whereIn($id_column, $ids)->get();
}
Expand Down Expand Up @@ -76,7 +76,7 @@ public static function getByName($names, $modelType = null, $models = null): Col
}

if ($modelType != null) {
$query = static::where('model', $modelType);
$query = static::where('model_type', $modelType);
} else {
$query = static::query();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/PermissionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ class PermissionModel extends Model
*
* @var string
*/
protected $table = 'permissions_models';
protected $table = 'permission_model';
}
2 changes: 1 addition & 1 deletion src/Models/RoleModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ class RoleModel extends Model
*
* @var string
*/
protected $table = 'roles_models';
protected $table = 'role_model';
}
22 changes: 11 additions & 11 deletions src/Traits/HasPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function addPermissions($permissions, $resource = null, $ids = null): voi
$model_id = $this->getSelfRoleId();
foreach ($permissions as $permission) {
$toAdd[] = [
'model' => $model,
'model_type' => $model,
'model_id' => $model_id,
'permission_id' => $permission->id,
];
Expand Down Expand Up @@ -160,7 +160,7 @@ public function delPermissions($permissions, $resource = null, $ids = null): voi
PermissionModel::query()
->where([
'model_id' => $this->getSelfRoleId(),
'model' => $this::class,
'model_type' => $this::class,
])
->whereIn('permission_id', $ids)
->delete();
Expand All @@ -176,7 +176,7 @@ public function delAllPermissions()
{
PermissionModel::where([
'model_id' => $this->getSelfRoleId(),
'model' => $this::class,
'model_type' => $this::class,
])->delete();
}

Expand All @@ -203,20 +203,20 @@ private function getThisPermissionsIds()
$model = $this::class;

$query = PermissionModel::where([
'model' => $model,
'model_type' => $model,
'model_id' => $model_id
]);

if ($this instanceof HasRoleContract) {
$role_ids = RoleModel::where([
'model' => $model,
'model_type' => $model,
'model_id' => $model_id
])->pluck('role_id');

if ($role_ids->count() > 0) {
$role_model = $this::Role();
$query->orWhere(function ($q) use ($role_model, $role_ids) {
$q->where('model', $role_model)->whereIn('model_id', $role_ids);
$q->where('model_type', $role_model)->whereIn('model_id', $role_ids);
});
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ public function getModels($class, $permissionNames = [], $addPrefix = true)
$permissionNames = [$permissionNames];

$query = static::Permission()::query()
->where('model', $class)
->where('model_type', $class)
->whereNotNull('model_id')
->whereIn('id', $this->getThisPermissionsIds());

Expand Down Expand Up @@ -279,7 +279,7 @@ public function getModels($class, $permissionNames = [], $addPrefix = true)
public function getSelfPermissions()
{
$ids = PermissionModel::where([
'model' => $this::class,
'model_type' => $this::class,
'model_id' => $this->getSelfRoleId(),
])->pluck('permission_id');
return static::Permission()::whereIn('id', $ids)->get();
Expand Down Expand Up @@ -438,7 +438,7 @@ public static function addPermissionsToMany($permissions, $models): void
$toCreate[] = [
'permission_id' => $permission->id,
'model_id' => $model->getSelfRoleId(),
'model' => $model::class,
'model_type' => $model::class,
];
}
}
Expand Down Expand Up @@ -474,7 +474,7 @@ public static function delPermissionsFromMany($permissions, $models): void
PermissionModel::query()
->whereIn('permission_id', $pids)
->whereIn('model_id', $rids)
->where('model', $model)
->where('model_type', $model)
->delete();
}

Expand Down Expand Up @@ -507,7 +507,7 @@ public static function withPermissions($models): Collection

// get the association models
$rps = PermissionModel::query()
->where('model', $model)
->where('model_type', $model)
->whereIn('model_id', $mids)
->get();

Expand Down
10 changes: 5 additions & 5 deletions src/Traits/HasRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait HasRole
private function getBaseQuery(): Builder
{
return RoleModel::where([
'model' => $this::class,
'model_type' => $this::class,
'model_id' => $this->getModelId(),
]);
}
Expand Down Expand Up @@ -71,7 +71,7 @@ public function addRoles($roles): void
$toAdd = [];
foreach ($roles as $role) {
$toAdd[] = [
'model' => $model,
'model_type' => $model,
'model_id' => $model_id,
'role_id' => $role->id,
];
Expand Down Expand Up @@ -218,7 +218,7 @@ public static function addRolesToMany($roles, $models): void
foreach ($role_ids as $rid) {
foreach ($model_ids as $mid) {
$toCreate[] = [
'model' => $model,
'model_type' => $model,
'model_id' => $mid,
'role_id' => $rid,
];
Expand Down Expand Up @@ -254,7 +254,7 @@ public static function delRolesFromMany($roles, $models): void
RoleModel::query()
->whereIn('role_id', $rids)
->whereIn('model_id', $uids)
->where('model', static::class)
->where('model_type', static::class)
->delete();
}

Expand All @@ -277,7 +277,7 @@ public static function withRoles($models): Collection

// get the association models
$rms = RoleModel::query()
->where('model', static::class)
->where('model_type', static::class)
->whereIn('model_id', $mids)
->get();

Expand Down
16 changes: 8 additions & 8 deletions src/Traits/Permissionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait Permissionable
public function deleteThisModelPermissions(): void
{
static::Permission()::where([
'model' => $this::class,
'model_type' => $this::class,
'model_id' => $this->getModelId(),
])->delete();
}
Expand All @@ -31,7 +31,7 @@ public function deleteThisModelPermissions(): void
*/
public static function deleteAllModelPermissions(): void
{
static::Permission()::where('model', static::class)->delete();
static::Permission()::where('model_type', static::class)->delete();
}

/**
Expand All @@ -42,7 +42,7 @@ public static function deleteAllModelPermissions(): void
public static function deleteGenericModelPermissions(): void
{
static::Permission()::where([
'model' => static::class,
'model_type' => static::class,
'model_id' => null
])->delete();
}
Expand Down Expand Up @@ -85,7 +85,7 @@ protected static function getPrefixed($permissionNames): array
protected static function createPermission($permissionName, $model_id = null): Model
{
return static::Permission()::firstOrCreate([
'model' => static::class,
'model_type' => static::class,
'model_id' => $model_id,
'name' => static::getPermissionPrefix() . '.' . $permissionName,
]);
Expand All @@ -101,7 +101,7 @@ protected static function createPermission($permissionName, $model_id = null): M
protected static function getPermission($permissionName, $model_id = null): Model
{
return static::Permission()::where([
'model' => static::class,
'model_type' => static::class,
'model_id' => $model_id,
'name' => static::getPermissionPrefix() . '.' . $permissionName,
])->first();
Expand Down Expand Up @@ -164,7 +164,7 @@ protected static function createManyPermissions($names, $model_id = null): Colle
foreach ($permission_names as $name) {
$toCreate[] = [
'name' => $name,
'model' => static::class,
'model_type' => static::class,
'model_id' => $model_id,
];
}
Expand All @@ -185,7 +185,7 @@ protected static function getManyPermissions($names, $model_id = null): Collecti
$names = static::getPrefixed($names);
return static::Permission()::query()
->whereIn('name', $names)
->where('model', static::class)
->where('model_type', static::class)
->where('model_id', $model_id)
->get();
}
Expand All @@ -202,7 +202,7 @@ protected static function deleteManyPermissions($names, $model_id = null): void
$names = static::getPrefixed($names);
static::Permission()::query()
->whereIn('name', $names)
->where('model', static::class)
->where('model_type', static::class)
->where('model_id', $model_id)
->delete();
}
Expand Down
13 changes: 7 additions & 6 deletions tests/Feature/HasPermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function test_add_permission()
// assert it currently does not have the permission
$this->assertFalse(
PermissionModel::where([
'model' => $role::class,
'model_type' => $role::class,
'model_id' => $role->id,
'permission_id' => $permission->id
])->exists()
Expand All @@ -56,7 +56,7 @@ public function test_add_permission()
// assert it now has the permission
$this->assertTrue(
PermissionModel::where([
'model' => $role::class,
'model_type' => $role::class,
'model_id' => $role->id,
'permission_id' => $permission->id
])->exists()
Expand All @@ -79,7 +79,7 @@ public function test_add_permissions()
$m = PermissionModel::query()
->whereIn('permission_id', $ids)
->where('model_id', $role->id)
->where('model', $role::class)
->where('model_type', $role::class)
->count();
$this->assertEquals(0, $m);

Expand All @@ -90,7 +90,7 @@ public function test_add_permissions()
$m = PermissionModel::query()
->whereIn('permission_id', $ids)
->where('model_id', $role->id)
->where('model', $role::class)
->where('model_type', $role::class)
->count();
$this->assertEquals($n, $m);
}
Expand Down Expand Up @@ -316,7 +316,7 @@ public function test_mass_permission_attribution()
$f = fn() => PermissionModel::query()
->whereIn('permission_id', $pid)
->whereIn('model_id', $rid)
->where('model', $roles->first()::class)
->where('model_type', $roles->first()::class)
->count();

Role::addPermissionsToMany($permissions, $roles);
Expand All @@ -341,7 +341,8 @@ public function test_get_roles_with_permissions()
$roles->each(fn($r) => $r->addPermissions($permissions->random(1, $n)));

$roles = Role::withPermissionNames($roles);
foreach ($roles as $r)
foreach ($roles as $r) {
$this->assertEquals($r->permissions, $r->getPermissionNames());
}
}
}
Loading

0 comments on commit edac744

Please sign in to comment.