diff --git a/database/migrations/create_acl_tables.php b/database/migrations/create_acl_tables.php index dc3b7df..6b3128f 100644 --- a/database/migrations/create_acl_tables.php +++ b/database/migrations/create_acl_tables.php @@ -46,15 +46,15 @@ public function up() $table->timestamps(); }); - Schema::create('role_model', function (Blueprint $table) { - $table->string('model_type'); - $table->string('model_id'); + Schema::create('roleables', function (Blueprint $table) { + $table->string('roleable_type'); + $table->string('roleable_id'); $table->unsignedBigInteger('role_id'); $table->foreign('role_id') ->references('id') ->on('roles') ->cascadeOnDelete(); - $table->primary(['model_type', 'model_id', 'role_id']); + $table->primary(['roleable_type', 'roleable_id', 'role_id']); $table->timestamps(); }); } @@ -67,7 +67,7 @@ public function up() public function down() { Schema::dropIfExists('permissionables'); - Schema::dropIfExists('role_model'); + Schema::dropIfExists('roleables'); Schema::dropIfExists('roles'); Schema::dropIfExists('permissions'); } diff --git a/src/Models/RoleModel.php b/src/Models/Roleable.php similarity index 78% rename from src/Models/RoleModel.php rename to src/Models/Roleable.php index 481cce6..10a6720 100644 --- a/src/Models/RoleModel.php +++ b/src/Models/Roleable.php @@ -5,7 +5,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -class RoleModel extends Model +class Roleable extends Model { use HasFactory; @@ -14,5 +14,5 @@ class RoleModel extends Model * * @var string */ - protected $table = 'role_model'; + protected $table = 'roleables'; } \ No newline at end of file diff --git a/src/Traits/HasPermission.php b/src/Traits/HasPermission.php index 0eddda8..aecf72a 100644 --- a/src/Traits/HasPermission.php +++ b/src/Traits/HasPermission.php @@ -12,7 +12,7 @@ use Uwla\Lacl\Contracts\HasRoleContract; use Uwla\Lacl\Models\Permission; use Uwla\Lacl\Models\Permissionable; -use Uwla\Lacl\Models\RoleModel; +use Uwla\Lacl\Models\Roleable; trait HasPermission { @@ -218,9 +218,9 @@ private function getThisPermissionsIds() return $query->pluck('permission_id'); } - $role_ids = RoleModel::where([ - 'model_type' => $model, - 'model_id' => $model_id, + $role_ids = Roleable::where([ + 'roleable_type' => $model, + 'roleable_id' => $model_id, ])->pluck('role_id'); if ($role_ids->count() > 0) { @@ -563,4 +563,4 @@ public static function withPermissionNames($models): Collection } return $models; } -} \ No newline at end of file +} diff --git a/src/Traits/HasRole.php b/src/Traits/HasRole.php index ce6bc47..54677c0 100644 --- a/src/Traits/HasRole.php +++ b/src/Traits/HasRole.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; use Uwla\Lacl\Models\Role; -use Uwla\Lacl\Models\RoleModel; +use Uwla\Lacl\Models\Roleable; trait HasRole { @@ -19,9 +19,9 @@ trait HasRole */ private function getBaseQuery(): Builder { - return RoleModel::where([ - 'model_type' => $this::class, - 'model_id' => $this->getModelId(), + return Roleable::where([ + 'roleable_type' => $this::class, + 'roleable_id' => $this->getModelId(), ]); } @@ -71,12 +71,12 @@ public function addRoles($roles): void $toAdd = []; foreach ($roles as $role) { $toAdd[] = [ - 'model_type' => $model, - 'model_id' => $model_id, + 'roleable_type' => $model, + 'roleable_id' => $model_id, 'role_id' => $role->id, ]; } - RoleModel::insert($toAdd); + Roleable::insert($toAdd); } /* @@ -212,13 +212,13 @@ public static function addRolesToMany($roles, $models): void foreach ($role_ids as $rid) { foreach ($model_ids as $mid) { $toCreate[] = [ - 'model_type' => $model, - 'model_id' => $mid, + 'roleable_type' => $model, + 'roleable_id' => $mid, 'role_id' => $rid, ]; } } - RoleModel::insert($toCreate); + Roleable::insert($toCreate); } /* @@ -245,10 +245,10 @@ public static function delRolesFromMany($roles, $models): void $roles = static::normalizeRoles($roles); $rids = $roles->pluck('id'); $uids = $models->pluck(static::getIdColumn()); - RoleModel::query() + Roleable::query() ->whereIn('role_id', $rids) - ->whereIn('model_id', $uids) - ->where('model_type', static::class) + ->whereIn('roleable_id', $uids) + ->where('roleable_type', static::class) ->delete(); } @@ -263,9 +263,9 @@ public static function withRoles($models): Collection $models = static::normalizeModels($models); $idColumn = static::getIdColumn(); $mids = $models->pluck($idColumn); - $role_models = RoleModel::query() - ->where('model_type', static::class) - ->whereIn('model_id', $mids) + $role_models = Roleable::query() + ->where('roleable_type', static::class) + ->whereIn('roleable_id', $mids) ->get(); $role_ids = $role_models->pluck('role_id'); $roles = Role::whereIn('id', $role_ids)->get(); @@ -289,7 +289,7 @@ public static function withRoles($models): Collection $model->roles = collect(); foreach ($role_models as $role_model) { - $model_id = $role_model->model_id; + $model_id = $role_model->roleable_id; $role_id = $role_model->role_id; $model = $id2model[$model_id]; $role = $id2role[$role_id]; diff --git a/tests/Feature/HasRoleTest.php b/tests/Feature/HasRoleTest.php index 74a8d52..ab83a7f 100644 --- a/tests/Feature/HasRoleTest.php +++ b/tests/Feature/HasRoleTest.php @@ -5,7 +5,7 @@ use Tests\App\Models\Role; use Tests\App\Models\User; use Tests\TestCase; -use Uwla\Lacl\Models\RoleModel; +use Uwla\Lacl\Models\Roleable; class HasRoleTest extends TestCase { @@ -35,9 +35,9 @@ public function test_add_role(): void $user->addRole($role); $this->assertTrue( - RoleModel::where([ - 'model_type' => $user::class, - 'model_id' => $user->id, + Roleable::where([ + 'roleable_type' => $user::class, + 'roleable_id' => $user->id, 'role_id' => $role->id ])->exists() ); @@ -54,10 +54,10 @@ public function test_add_roles(): void $roles = Role::factory($this->m)->create(); $user->addRoles($roles); - $ids = RoleModel::query() + $ids = Roleable::query() ->where([ - 'model_id' => $user->id, - 'model_type' => $user::class + 'roleable_id' => $user->id, + 'roleable_type' => $user::class ])->pluck('role_id'); $user_roles = Role::whereIn('id', $ids)->get(); $this->assertTrue($roles->diff($user_roles)->isEmpty()); @@ -214,10 +214,10 @@ public function test_mass_role_attribution(): void $uid = $users->pluck('id'); $rid = $roles->pluck('id'); - $f = fn() => RoleModel::query() + $f = fn() => Roleable::query() ->whereIn('role_id', $rid) - ->whereIn('model_id', $uid) - ->where('model_type', $users->first()::class) + ->whereIn('roleable_id', $uid) + ->where('roleable_type', $users->first()::class) ->count(); User::addRolesToMany($roles, $users);