diff --git a/src/Models/Permission.php b/src/Models/Permission.php index e8e8722..d275ac0 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -6,8 +6,8 @@ class Permission extends Model { - protected $guarded = []; - + protected $fillable = ['name']; + public function roles() { return $this->belongsToMany(Role::class); diff --git a/src/Models/Role.php b/src/Models/Role.php index 930d1eb..68075e0 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -6,7 +6,7 @@ class Role extends Model { - protected $guarded = []; + protected $fillable = ['name']; public function permissions() { diff --git a/src/Traits/HasRoleAndPermissions.php b/src/Traits/HasRoleAndPermissions.php new file mode 100644 index 0000000..6ba4c26 --- /dev/null +++ b/src/Traits/HasRoleAndPermissions.php @@ -0,0 +1,93 @@ +first(); + + if ($role) { + // Ensure the user only has one role + $this->role()->associate($role); + $this->save(); + } + + return $this; + } + + // Get the user's role + public function getRole() + { + return $this->role; + } + + // Disassociate (remove) the current role from the user + public function removeRole() + { + if ($this->role) { + $this->role()->dissociate(); + $this->save(); + } + + return $this; + } + + // Check if the user has a specific permission + public function hasPermission(string $permissionName) + { + $role = $this->role; + + if ($role) { + return $role->permissions()->where('name', $permissionName)->exists(); + } + + return false; + } + + // Assign a permission to the role by permission name + public function assignPermissionToRole(string $permissionName) + { + $role = $this->role; + if ($role) { + $permission = Permission::where('name', $permissionName)->first(); + + if ($permission) { + $role->permissions()->syncWithoutDetaching([$permission->id]); + } + } + + return $this; + } + + // Remove a permission from the role by permission name + public function removePermissionFromRole(string $permissionName) + { + $role = $this->role; + if ($role) { + $permission = Permission::where('name', $permissionName)->first(); + + if ($permission) { + $role->permissions()->detach($permission->id); + } + } + + return $this; + } + + // Check if the user has the specified role + public function hasRole(string $roleName) + { + return $this->role && $this->role->name === $roleName; + } + + // Relationship for user role (one-to-one) + public function role() + { + return $this->belongsTo(Role::class); + } +} diff --git a/src/database/migrations/create_permission_tables.php b/src/database/migrations/create_permission_tables.php index 0e1d22e..317f16a 100644 --- a/src/database/migrations/create_permission_tables.php +++ b/src/database/migrations/create_permission_tables.php @@ -31,6 +31,13 @@ public function up() $table->foreignId('permission_id')->constrained()->onDelete('cascade'); }); } + + if (!Schema::hasTable('users')) { + Schema::table('users', function (Blueprint $table) { + $table->foreignId('role_id')->nullable()->constrained('roles')->onDelete('set null'); + }); + } + } public function down() @@ -38,5 +45,8 @@ public function down() Schema::dropIfExists('role_permission'); Schema::dropIfExists('permissions'); Schema::dropIfExists('roles'); + Schema::table('users', function (Blueprint $table) { + $table->dropForeign(['role_id']); + }); } };