I has permissions trait

This commit is contained in:
a6a2f5842 2025-07-10 10:29:53 +02:00
parent 5f8c190120
commit ebc23249c4
6 changed files with 22 additions and 92 deletions

2
.dockerignore Normal file
View File

@ -0,0 +1,2 @@
docker
.vscode

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
phpunit.xml
reports reports
sandbox sandbox
vendor vendor
composer.lock composer.lock
.idea/ .idea/
workbench
.phpunit.result.cache

View File

@ -54,11 +54,7 @@
"minimum-stability": "dev", "minimum-stability": "dev",
"prefer-stable": true, "prefer-stable": true,
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {}
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
}
}, },
"scripts": { "scripts": {
"post-autoload-dump": [ "post-autoload-dump": [
@ -75,6 +71,10 @@
], ],
"lint": [ "lint": [
"@php vendor/bin/pint --ansi" "@php vendor/bin/pint --ansi"
],
"test": [
"@clear",
"@php vendor/bin/phpunit"
] ]
} }
} }

View File

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
<testsuites>
<testsuite name="Permissions Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="CACHE_DRIVER" value="array"/>
<ini name="memory_limit" value="512M"/>
</php>
</phpunit>

View File

@ -8,7 +8,7 @@ trait HasPermissions
{ {
public function hasPermission(string $permission): bool public function hasPermission(string $permission): bool
{ {
$allpermissions = $this->allPermissions(); $allpermissions = $this->permissions();
if ($allpermissions->contains('slug', '*')) { if ($allpermissions->contains('slug', '*')) {
return true; // If any permission is '*', all permissions are granted return true; // If any permission is '*', all permissions are granted
@ -31,7 +31,7 @@ trait HasPermissions
); );
} }
public function permissions() public function individualPermissions()
{ {
return $this->morphToMany( return $this->morphToMany(
config('roles.models.permission'), config('roles.models.permission'),
@ -40,6 +40,16 @@ trait HasPermissions
); );
} }
public function permissions()
{
$rolePerms = $this->rolePermissions()->get();
$directPerms = $this->permissions()->get();
return $rolePerms
->merge($directPerms)
->unique('id');
}
public function assignPermission($permission) public function assignPermission($permission)
{ {
$permission_class = config('roles.models.permission'); $permission_class = config('roles.models.permission');
@ -85,32 +95,4 @@ trait HasPermissions
return true; return true;
} }
/**
* Get all permissions directly assigned or inherited via roles.
*
* @return Collection
*/
public function allPermissions()
{
// Directly assigned permissions
$direct = $this->permissions()->get();
// Permissions via roles (if the roles() relation exists)
if (method_exists($this, 'roles')) {
$rolePermissions = $this->roles()
->with('permissions')
->get()
->pluck('permissions')
->flatten();
} else {
$rolePermissions = collect();
}
// Merge and dedupe by 'id'
return $direct
->merge($rolePermissions)
->unique('id')
->values();
}
} }

View File

@ -1,33 +0,0 @@
laravel: '@testbench'
providers:
# - Workbench\App\Providers\WorkbenchServiceProvider
migrations:
- workbench/database/migrations
seeders:
- Workbench\Database\Seeders\DatabaseSeeder
workbench:
start: '/'
install: true
health: false
discovers:
web: true
api: true
commands: true
components: false
factories: true
views: false
build:
- asset-publish
- create-sqlite-db
- db-wipe
- migrate-fresh
assets:
- laravel-assets
sync:
- from: storage
to: workbench/storage
reverse: true