A more files & structure

This commit is contained in:
a6a2f5842 2025-06-15 11:56:28 +02:00
parent e4a6824dcb
commit 7cfc7749cc
9 changed files with 166 additions and 9 deletions

View File

@ -6,7 +6,11 @@
"roles", "roles",
"permission", "permission",
"laravel", "laravel",
"blax" "blax",
"authorization",
"user management",
"access control",
"authentication"
], ],
"homepage": "http://www.blax.at", "homepage": "http://www.blax.at",
"license": "MIT", "license": "MIT",
@ -14,6 +18,7 @@
{ {
"name": "Fabian Wagner", "name": "Fabian Wagner",
"email": "fabian@blax.at", "email": "fabian@blax.at",
"homepage": "https://www.blax.at",
"role": "Developer" "role": "Developer"
} }
], ],
@ -23,8 +28,12 @@
} }
}, },
"require": { "require": {
"php": ">=8.2" "php": "^8.0",
}, "illuminate/auth": "^8.12|^9.0|^10.0|^11.0|^12.0",
"require-dev": { "illuminate/container": "^8.12|^9.0|^10.0|^11.0|^12.0",
"illuminate/contracts": "^8.12|^9.0|^10.0|^11.0|^12.0",
"illuminate/database": "^8.12|^9.0|^10.0|^11.0|^12.0",
"illuminate/support": "^8.12|^9.0|^10.0|^11.0|^12.0",
"laravel/pint": "^1.22"
} }
} }

View File

@ -1,10 +1,12 @@
<?php <?php
return [ return [
'models' => [ 'models' => [
'role' => YourVendor\Permissions\Models\Role::class, 'role' => \Blax\Roles\Models\Role::class,
'permission' => YourVendor\Permissions\Models\Permission::class, 'permission' => \Blax\Roles\Models\Permission::class,
], ],
'table_names' => [ 'table_names' => [
'roles' => 'roles', 'roles' => 'roles',
'permissions' => 'permissions', 'permissions' => 'permissions',
@ -12,4 +14,5 @@ return [
'model_has_permissions' => 'model_has_permissions', 'model_has_permissions' => 'model_has_permissions',
'role_has_permissions' => 'role_has_permissions', 'role_has_permissions' => 'role_has_permissions',
], ],
]; ];

View File

@ -0,0 +1,47 @@
<?php
namespace Blax\Roles\Migrations;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// TODO finish the migration
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->string('description')->nullable();
$table->timestamps();
});
Schema::create('permissions', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->string('description')->nullable();
$table->timestamps();
});
Schema::create('assignments', function (Blueprint $table) {
$table->morphs('affected');
$table->morphs('assignment');
$table->timestamp('expires_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};

4
pint.json Normal file
View File

@ -0,0 +1,4 @@
{
"preset": "laravel",
"rules": {}
}

View File

@ -0,0 +1,7 @@
<?php
namespace Blax\Roles\Models;
use Illuminate\Database\Eloquent\Model;
class Assignment extends Model {}

View File

@ -0,0 +1,7 @@
<?php
namespace Blax\Roles\Models;
use Illuminate\Database\Eloquent\Model;
class Permission extends Model {}

View File

@ -4,6 +4,4 @@ namespace Blax\Roles\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Role extends Model { class Role extends Model {}
}

View File

@ -1,2 +1,52 @@
<?php <?php
namespace Blax\Roles;
class PermissionsServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->offerPublishing();
}
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
// Load routes, migrations, etc. if needed
}
/**
* Set up the publishing of configuration files.
*
* @return void
*/
protected function offerPublishing()
{
if (! $this->app->runningInConsole()) {
return;
}
if (! function_exists('config_path')) {
// function not available and 'publish' not relevant in Lumen
return;
}
$this->publishes([
__DIR__.'/../config/permission.php' => config_path('permission.php'),
], 'permission-config');
$this->publishes([
__DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->getMigrationFileName('create_permission_tables.php'),
], 'permission-migrations');
}
}

32
src/Traits/HasRoles.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace Blax\Roles;
trait HasRoles
{
/**
* The roles that belong to the model.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles()
{
return $this->belongsToMany(
config('permissions.models.role'),
config('permissions.table_names.model_has_roles'),
'model_id',
'role_id'
);
}
/**
* Check if the model has a specific role.
*
* @param string $role
* @return bool
*/
public function hasRole($role)
{
return $this->roles()->where('name', $role)->exists();
}
}