Skip to content

Commit e772317

Browse files
Simplify the package (#16)
* Add Laravel 8.3 and necessary dependencies to Dockerfile * Refactor models for handling permissions and roles * Update composer.json and sync actions for roles and permissions * Apply fixes from StyleCI (#17) Co-authored-by: StyleCI Bot <bot@styleci.io> --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 8f4c333 commit e772317

File tree

14 files changed

+221
-106
lines changed

14 files changed

+221
-106
lines changed

.devcontainer/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Base image is from Microsoft
2+
FROM mcr.microsoft.com/vscode/devcontainers/base:ubuntu-22.04
3+
4+
# Avoid interactive prompts (e.g. tzdata) during installation
5+
ENV DEBIAN_FRONTEND=noninteractive
6+
7+
RUN mkdir -p /var/www/html
8+
9+
# Install 8.3 from Ondrej’s PPA
10+
RUN apt-get update && apt-get install -y \
11+
software-properties-common \
12+
&& add-apt-repository ppa:ondrej/php -y \
13+
&& apt-get update && apt-get install -y sqlite3 \
14+
&& apt-get install -y php8.3-cli php8.3-dev \
15+
php8.3-pgsql php8.3-sqlite3 php8.3-gd \
16+
php8.3-curl \
17+
php8.3-imap php8.3-mysql php8.3-mbstring \
18+
php8.3-xml php8.3-zip php8.3-bcmath php8.3-soap \
19+
php8.3-intl php8.3-readline \
20+
php8.3-ldap \
21+
php8.3-msgpack php8.3-igbinary php8.3-redis php8.3-swoole \
22+
php8.3-memcached php8.3-pcov php8.3-imagick php8.3-xdebug \
23+
&& apt-get clean && rm -rf /var/lib/apt/lists/*

.devcontainer/devcontainer.bash

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
alias art='php artisan --ansi'
2+
alias tinker='art tinker'
3+
alias format='php vendor/bin/pint'
4+
alias analyze='php vendor/bin/phpstan analyse'
5+
alias test='php vendor/bin/paratest --coverage-html coverage'
6+
alias stf='php vendor/bin/phpunit --filter'
7+
8+
# commit AI
9+
function commit() {
10+
commitMessage="$*"
11+
12+
git add .
13+
14+
if [ "$commitMessage" = "" ]; then
15+
aicommits
16+
return
17+
fi
18+
19+
eval "git commit -a -m '${commitMessage}'"
20+
}
21+
22+
# function gfind
23+
function gfind() {
24+
local excludeVendor="--exclude-dir=vendor" # Default to excluding the vendor directory
25+
local searchString=""
26+
local searchPath="./"
27+
28+
# Process all arguments
29+
for arg in "$@"; do
30+
if [[ "$arg" == "-w" || "$arg" == "--with-vendor" ]]; then
31+
excludeVendor="" # Remove the exclude directive to include vendor
32+
elif [[ -z "$searchString" && "$arg" != -* ]]; then
33+
searchString="$arg" # Set the search string if it's not a flag and is the first non-flag argument
34+
fi
35+
done
36+
37+
# Check if a search string was provided
38+
if [[ -z "$searchString" ]]; then
39+
echo -e "${RED}Error: Missing required search string.${NC}"
40+
echo -e "${YELLOW}Usage: ${NC}gfind searchString [-w|--with-vendor]"
41+
return 1
42+
fi
43+
44+
# Execute grep command
45+
grep --include=\*.php $excludeVendor -rnw $searchPath -e "$searchString"
46+
}

.devcontainer/devcontainer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "Laravel RBAC",
3+
"build": {
4+
"dockerfile": "Dockerfile",
5+
"context": "."
6+
},
7+
"features": {},
8+
"customizations": {
9+
"vscode": {
10+
"extensions": [
11+
"bmewburn.vscode-intelephense-client",
12+
"calebporzio.better-phpunit",
13+
"laravel.vscode-laravel",
14+
"mikestead.dotenv",
15+
"ms-azuretools.vscode-docker",
16+
"php.intelephense"
17+
],
18+
"settings": {
19+
"intelephense.environment.phpVersion": "8.3"
20+
}
21+
}
22+
},
23+
"remoteUser": "vscode",
24+
"postCreateCommand": "",
25+
"forwardPorts": [],
26+
"portsAttributes": {}
27+
}

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
.cursor
12
.idea
23
.php-cs-fixer.cache
34
.phpunit.cache
45
build
56
composer.lock
67
coverage
78
docs
9+
vendor
10+
workbench
11+
node_modules
812
phpunit.xml
913
phpstan.neon
14+
repomix-output.*
1015
testbench.yaml
11-
vendor
12-
node_modules

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![run-tests](https://img.shields.io/github/actions/workflow/status/binary-cats/laravel-rbac/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/binary-cats/laravel-rbac/actions/workflows/run-tests.yml)
99
[![GitHub Code Style Action Status](https://github.styleci.io/repos/773171043/shield?branch=main)](https://github.com/binary-cats/laravel-rbac/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
1010

11-
Enhance Laravel 11 with opinionated extension for [spatie/laravel-permissions](https://spatie.be/docs/laravel-permission/v6/introduction).
11+
Enhance your Laravel with opinionated extension for [spatie/laravel-permissions](https://spatie.be/docs/laravel-permission/v6/introduction).
1212
Before your permission list grows and maintenance becomes an issue, this package offers simple way of defining roles and their permissions.
1313

1414
## Installation

composer.json

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"php": "^8.2",
2121
"illuminate/contracts": "^11.0|^12.0",
2222
"lorisleiva/laravel-actions": "^2.8",
23-
"spatie/laravel-collection-macros": "^7.0|^8.0",
2423
"spatie/laravel-package-tools": "^1.16",
2524
"spatie/laravel-permission": "^6.4"
2625
},
@@ -36,15 +35,34 @@
3635
},
3736
"autoload-dev": {
3837
"psr-4": {
39-
"BinaryCats\\LaravelRbac\\Tests\\": "tests/"
38+
"BinaryCats\\LaravelRbac\\Tests\\": "tests/",
39+
"Workbench\\App\\": "workbench/app/",
40+
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
41+
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
4042
}
4143
},
4244
"suggest": {
4345
"binary-cats/laravel-mailgun-webhooks": "Handle Mailgun webhooks in your Laravel application",
4446
"binary-cats/laravel-sku": "Generate SKUs for Eloquent models"
4547
},
4648
"scripts": {
47-
"test": "vendor/bin/phpunit"
49+
"test": "vendor/bin/phpunit",
50+
"post-autoload-dump": [
51+
"@clear",
52+
"@prepare"
53+
],
54+
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
55+
"prepare": "@php vendor/bin/testbench package:discover --ansi",
56+
"build": "@php vendor/bin/testbench workbench:build --ansi",
57+
"serve": [
58+
"Composer\\Config::disableProcessTimeout",
59+
"@build",
60+
"@php vendor/bin/testbench serve"
61+
],
62+
"lint": [
63+
"@php vendor/bin/pint",
64+
"@php vendor/bin/phpstan analyse"
65+
]
4866
},
4967
"config": {
5068
"sort-packages": true
@@ -57,11 +75,11 @@
5775
"aliases": {
5876
"Rbac": "BinaryCats\\LaravelRbac\\Facades\\Rbac"
5977
}
60-
},
78+
},
6179
"branch-alias": {
6280
"dev-master": "1.x-dev"
63-
}
81+
}
6482
},
6583
"minimum-stability": "dev",
6684
"prefer-stable": true
67-
}
85+
}

src/Actions/StorePermission.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@
33
namespace BinaryCats\LaravelRbac\Actions;
44

55
use BackedEnum;
6-
use Illuminate\Support\Facades\Artisan;
76
use Lorisleiva\Actions\Action;
7+
use Spatie\Permission\Contracts\Permission;
88

99
class StorePermission extends Action
1010
{
11+
public function __construct(
12+
protected readonly Permission $permission
13+
) {
14+
}
15+
1116
/**
12-
* @param \BackedEnum $permission
13-
* @param string $guard
14-
*
15-
* @return void
17+
* Handle storing a permission.
1618
*/
17-
public function handle(BackedEnum $permission, string $guard): void
19+
public function handle(BackedEnum|string $permission, string $guard): void
1820
{
19-
Artisan::call('permission:create-permission', [
20-
'name' => $permission->value,
21-
'guard' => $guard,
22-
]);
21+
if ($permission instanceof BackedEnum) {
22+
$permission = $permission->value;
23+
}
24+
25+
$this->permission::findOrCreate($permission, $guard);
2326
}
2427
}

src/Actions/SyncDefinedRole.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,28 @@
33
namespace BinaryCats\LaravelRbac\Actions;
44

55
use BackedEnum;
6-
use Illuminate\Support\Facades\Artisan;
76
use Lorisleiva\Actions\Action;
7+
use Spatie\Permission\Contracts\Role;
88

99
class SyncDefinedRole extends Action
1010
{
11+
public function __construct(
12+
protected readonly Role $role
13+
) {
14+
}
15+
1116
/**
12-
* @return void
17+
* Handle syncing a defined role.
1318
*/
1419
public function handle(string $name, string $guard, array $permissions): void
1520
{
1621
$permissions = collect($permissions)
1722
->map(fn ($permission) => match (true) {
1823
$permission instanceof BackedEnum => $permission->value,
1924
default => (string) $permission
20-
})->implode('|');
25+
});
2126

22-
Artisan::call('permission:create-role', [
23-
'name' => $name,
24-
'guard' => $guard,
25-
'permissions' => $permissions,
26-
]);
27+
$this->role::findOrCreate($name, $guard)
28+
->syncPermissions($permissions);
2729
}
2830
}

src/Commands/RbacResetCommand.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,45 @@ class RbacResetCommand extends Command
2424

2525
/**
2626
* Execute the console command.
27-
*
28-
* @return int
2927
*/
30-
public function handle()
28+
public function handle(): int
3129
{
32-
if ($this->databaseReady()) {
33-
$this->withProgressBar($this->jobs(), fn ($job) => $job->dispatch());
34-
$this->newLine();
35-
$this->info('RBAC Reset Complete');
36-
} else {
37-
$this->error('DB is not ready');
30+
if (!$this->databaseReady()) {
31+
$this->error('DB is not ready. Please run migrations.');
32+
33+
return self::INVALID;
3834
}
3935

36+
$this->jobs()->each(function (string $job) {
37+
$this->components->task(
38+
$job,
39+
fn () => $this->laravel->make($job)->dispatchSync()
40+
);
41+
});
42+
4043
return self::SUCCESS;
4144
}
4245

4346
/**
4447
* Create the jobs.
45-
*
46-
* @return \Illuminate\Support\Collection
4748
*/
4849
protected function jobs(): Collection
4950
{
5051
$value = config('rbac.jobs');
5152

52-
return collect($value)
53-
->map(fn ($job) => app()->make($job));
53+
return collect($value);
5454
}
5555

5656
/**
5757
* True if the Database is prepared.
58-
*
59-
* @return bool
6058
*/
6159
protected function databaseReady(): bool
6260
{
63-
$value = config('permission.table_names');
61+
$tables = config('permission.table_names', []);
6462

65-
return collect($value)
66-
->validate(fn ($table) => Schema::hasTable($table));
63+
return collect($tables)
64+
->map(fn (string $table) => Schema::hasTable($table))
65+
->reject()
66+
->isEmpty();
6767
}
6868
}

src/Jobs/ResetPermissions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ResetPermissions
2323
/**
2424
* @param string|null $guard
2525
*/
26-
public function __construct(string $guard = null)
26+
public function __construct(?string $guard = null)
2727
{
2828
$this->guard = $guard ?? config('auth.defaults.guard');
2929
}

0 commit comments

Comments
 (0)