diff --git a/README.md b/README.md new file mode 100644 index 0000000..8be0474 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Laravel Permission Lite + + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..63bf167 --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "vollborn/laravel-permission-lite", + "description": "A lightweight permissions package inspired by spatie/laravel-permission", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Oliver Vollborn", + "email": "oliver.vollborn@gmail.com" + } + ], + "require": { + "php": "^8.0", + "laravel/framework": "^9.0" + }, + "autoload": { + "psr-4": { + "Vollborn\\LaravelPermissionLite\\": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Vollborn\\LaravelPermissionLite\\Support\\ServiceProvider\\LaravelPermissionLiteServiceProvider" + ] + } + } +} diff --git a/src/Migrations/2022_08_21_112622_create_permission_tables.php b/src/Migrations/2022_08_21_112622_create_permission_tables.php new file mode 100644 index 0000000..073f212 --- /dev/null +++ b/src/Migrations/2022_08_21_112622_create_permission_tables.php @@ -0,0 +1,89 @@ +id(); + + $table->string('name', 100); + + $table->timestamps(); + }); + + Schema::create('roles', function (Blueprint $table) { + $table->id(); + + $table->string('name', 100); + + $table->timestamps(); + }); + + Schema::create('permission_role', function (Blueprint $table) { + $table->foreignId('permission_id') + ->references('id') + ->on('permissions') + ->cascadeOnDelete(); + + $table->foreignId('role_id') + ->references('id') + ->on('roles') + ->cascadeOnDelete(); + + $table->unique(['permission_id', 'role_id']); + }); + + Schema::create('permission_user', function (Blueprint $table) { + $table->foreignId('permission_id') + ->references('id') + ->on('permissions') + ->cascadeOnDelete(); + + $table->foreignId('user_id') + ->references('id') + ->on('users') + ->cascadeOnDelete(); + + $table->unique(['permission_id', 'user_id']); + }); + + Schema::create('role_user', function (Blueprint $table) { + $table->foreignId('role_id') + ->references('id') + ->on('roles') + ->cascadeOnDelete(); + + $table->foreignId('user_id') + ->references('id') + ->on('users') + ->cascadeOnDelete(); + + $table->unique(['role_id', 'user_id']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::dropIfExists('role_user'); + Schema::dropIfExists('permission_user'); + Schema::dropIfExists('permission_role'); + + Schema::dropIfExists('roles'); + Schema::dropIfExists('permissions'); + } +}; diff --git a/src/Models/Permission.php b/src/Models/Permission.php new file mode 100644 index 0000000..80d5c13 --- /dev/null +++ b/src/Models/Permission.php @@ -0,0 +1,19 @@ +loadMigrationsFrom(__DIR__ . '/../../Migrations'); + } +} diff --git a/src/Traits/HasPermissions.php b/src/Traits/HasPermissions.php new file mode 100644 index 0000000..6c9e235 --- /dev/null +++ b/src/Traits/HasPermissions.php @@ -0,0 +1,47 @@ +where('id', $id) + ->whereHas('permissions', static function ($query) use ($permission) { + $query->where('name', $permission); + }) + ->orWhereHas('roles', static function ($query) use ($permission) { + $query->whereHas('permissions', static function ($query) use ($permission) { + $query->where('name', $permission); + }); + }) + ->exists(); + } +} diff --git a/src/Traits/Relations/BelongsToManyPermissions.php b/src/Traits/Relations/BelongsToManyPermissions.php new file mode 100644 index 0000000..dd3b60b --- /dev/null +++ b/src/Traits/Relations/BelongsToManyPermissions.php @@ -0,0 +1,17 @@ +belongsToMany(Permission::class); + } +} diff --git a/src/Traits/Relations/BelongsToManyRoles.php b/src/Traits/Relations/BelongsToManyRoles.php new file mode 100644 index 0000000..d1c289e --- /dev/null +++ b/src/Traits/Relations/BelongsToManyRoles.php @@ -0,0 +1,17 @@ +belongsToMany(Role::class); + } +} diff --git a/src/Traits/Relations/BelongsToManyUsers.php b/src/Traits/Relations/BelongsToManyUsers.php new file mode 100644 index 0000000..924d2f4 --- /dev/null +++ b/src/Traits/Relations/BelongsToManyUsers.php @@ -0,0 +1,17 @@ +belongsToMany(User::class); + } +}