Skip to content

Commit

Permalink
Add models & migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
vollborn committed Aug 21, 2022
1 parent 0c1efc7 commit 8bf1039
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Laravel Permission Lite


28 changes: 28 additions & 0 deletions composer.json
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 src/Migrations/2022_08_21_112622_create_permission_tables.php
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');
}
};
19 changes: 19 additions & 0 deletions src/Models/Permission.php
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'
];

}
18 changes: 18 additions & 0 deletions src/Models/Role.php
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',
];
}
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');
}
}
47 changes: 47 additions & 0 deletions src/Traits/HasPermissions.php
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();
}
}
17 changes: 17 additions & 0 deletions src/Traits/Relations/BelongsToManyPermissions.php
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);
}
}
17 changes: 17 additions & 0 deletions src/Traits/Relations/BelongsToManyRoles.php
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);
}
}
17 changes: 17 additions & 0 deletions src/Traits/Relations/BelongsToManyUsers.php
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);
}
}

0 comments on commit 8bf1039

Please sign in to comment.