-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Laravel Permission Lite | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.0", | ||
"laravel/framework": "^9.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Vollborn\\LaravelPermissionLite\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Vollborn\\LaravelPermissionLite\\Support\\ServiceProvider\\LaravelPermissionLiteServiceProvider" | ||
] | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/Migrations/2022_08_21_112622_create_permission_tables.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('permissions', function (Blueprint $table) { | ||
$table->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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Vollborn\LaravelPermissionLite\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Vollborn\LaravelPermissionLite\Traits\Relations\BelongsToManyRoles; | ||
use Vollborn\LaravelPermissionLite\Traits\Relations\BelongsToManyUsers; | ||
|
||
class Permission extends Model | ||
{ | ||
use BelongsToManyUsers; | ||
use BelongsToManyRoles; | ||
|
||
protected $fillable = [ | ||
'id', | ||
'name' | ||
]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Vollborn\LaravelPermissionLite\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Vollborn\LaravelPermissionLite\Traits\Relations\BelongsToManyPermissions; | ||
use Vollborn\LaravelPermissionLite\Traits\Relations\BelongsToManyUsers; | ||
|
||
class Role extends Model | ||
{ | ||
use BelongsToManyUsers; | ||
use BelongsToManyPermissions; | ||
|
||
protected $fillable = [ | ||
'id', | ||
'name', | ||
]; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Support/ServiceProvider/LaravelPermissionLiteServiceProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Vollborn\LaravelPermissionLite\Support\ServiceProvider; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class LaravelPermissionLiteServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap any package services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot(): void | ||
{ | ||
$this->loadMigrationsFrom(__DIR__ . '/../../Migrations'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Vollborn\LaravelPermissionLite\Traits; | ||
|
||
use App\Models\User; | ||
use Illuminate\Support\Facades\Auth; | ||
use Vollborn\LaravelPermissionLite\Traits\Relations\BelongsToManyPermissions; | ||
use Vollborn\LaravelPermissionLite\Traits\Relations\BelongsToManyRoles; | ||
|
||
/** | ||
* supposed to be used in your App\Models\User | ||
*/ | ||
trait HasPermissions | ||
{ | ||
use BelongsToManyRoles; | ||
use BelongsToManyPermissions; | ||
|
||
/** | ||
* @param string|null $permission | ||
* @param int|null $userId | ||
* @return bool | ||
*/ | ||
public static function authorize(?string $permission = null, ?int $userId = null): bool | ||
{ | ||
$id = $userId ?? Auth::id(); | ||
|
||
if (!$id) { | ||
return false; | ||
} | ||
|
||
if (!$permission) { | ||
return true; | ||
} | ||
|
||
return User::query() | ||
->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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Vollborn\LaravelPermissionLite\Traits\Relations; | ||
|
||
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
use Vollborn\LaravelPermissionLite\Models\Permission; | ||
|
||
trait BelongsToManyPermissions | ||
{ | ||
/** | ||
* @return BelongsToMany | ||
*/ | ||
public function permissions(): BelongsToMany | ||
{ | ||
return $this->belongsToMany(Permission::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Vollborn\LaravelPermissionLite\Traits\Relations; | ||
|
||
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
use Vollborn\LaravelPermissionLite\Models\Role; | ||
|
||
trait BelongsToManyRoles | ||
{ | ||
/** | ||
* @return BelongsToMany | ||
*/ | ||
public function roles(): BelongsToMany | ||
{ | ||
return $this->belongsToMany(Role::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Vollborn\LaravelPermissionLite\Traits\Relations; | ||
|
||
use App\Models\User; | ||
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
|
||
trait BelongsToManyUsers | ||
{ | ||
/** | ||
* @return BelongsToMany | ||
*/ | ||
public function users(): BelongsToMany | ||
{ | ||
return $this->belongsToMany(User::class); | ||
} | ||
} |