Skip to content

Commit

Permalink
Merge pull request #7 from eramitgupta/feat/code-optimize
Browse files Browse the repository at this point in the history
feat: optimize code, update README, update composer, and fix styles

- Optimized code for better performance and maintainability
- Updated README with latest instructions and clarifications
- Updated composer.json with improved descriptions and dependencies
- Fixed minor style issues to ensure code consistency
  • Loading branch information
eramitgupta authored Oct 18, 2024
2 parents 52c11ea + 47e9e84 commit 55e3223
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 44 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Laravel Role-Permission
<center>

[![Packagist License](https://img.shields.io/badge/Licence-MIT-blue)](http://choosealicense.com/licenses/mit/)
[![Latest Stable Version](https://img.shields.io/packagist/v/erag/laravel-role-permission?label=Stable)](https://packagist.org/packages/erag/laravel-role-permission)
[![Total Downloads](https://img.shields.io/packagist/dt/erag/laravel-role-permission.svg?label=Downloads)](https://packagist.org/packages/erag/laravel-role-permission)

</center>

<center>
<img width="956" alt="Screenshot 2024-10-04 at 10 34 23 PM" src="https://github.com/user-attachments/assets/e78bffcf-6665-464b-a9a1-f6d8c72a9301">
</center>
Expand Down Expand Up @@ -193,9 +196,9 @@ Here's an example `RolePermissionSeeder` that seeds roles, permissions, and user

namespace Database\Seeders;

use App\Models\Role;
use App\Models\User;
use App\Models\Permission;
use EragPermission\Models\Role;
use EragPermission\Models\Permission;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
{
"name": "erag/laravel-role-permission",
"description": "Role Permission handling for Laravel v10x, v11x",
"description": "A simple and easy-to-install role and permission management package for Laravel, supporting versions 10.x and 11.x.",
"license": "MIT",
"keywords": [
"laravel",
"permission",
"role-permission",
"permissions",
"roles",
"acl",
"rbac",
"security",
"access control",
"laravel role permissions",
"easy role permission laravel"
],
"authors": [
{
"name": "Er Amiit Gupta",
"name": "Er Amit Gupta",
"email": "info.eramitgupta@gmail.com"
}
],
Expand All @@ -40,4 +41,4 @@
]
}
}
}
}
10 changes: 0 additions & 10 deletions src/Commands/PublishPermissionMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@ public function __construct()

public function handle(): void
{
// Step 1: Publish the models
$this->info('Publishing models...');
$this->call('vendor:publish', [
'--tag' => 'erag:publish-permission-models',
'--force' => true,
]);

// Step 2: Publish the migrations
$this->info('Publishing migrations...');
$this->call('vendor:publish', [
'--tag' => 'erag:publish-permission-migrations',
'--force' => true,
]);

// Step 3: Run the migrations
$this->info('Running migrations...');
$exitCode = $this->call('migrate', ['--force' => true]);

Expand All @@ -41,14 +33,12 @@ public function handle(): void
$this->error('Migration process encountered errors.');
}

// Step 4: Publish the seeder
$this->info('Publishing seeder...');
$this->call('vendor:publish', [
'--tag' => 'erag:publish-permission-role-seeder',
'--force' => true,
]);

// Step 5: Run the seeder
$this->info('Running seeder...');
try {
$exitCode = $this->call('db:seed', [
Expand Down
10 changes: 10 additions & 0 deletions src/Contracts/PermissionContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace EragPermission\Contracts;

interface PermissionContract
{
public function roles();

public function users();
}
10 changes: 10 additions & 0 deletions src/Contracts/RoleContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace EragPermission\Contracts;

interface RoleContract
{
public function permissions();

public function users();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Models;
namespace EragPermission\Models;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Role.php.stub → src/Models/Role.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Models;
namespace EragPermission\Models;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down
40 changes: 22 additions & 18 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace EragPermission;

use App\Models\Permission;
use EragPermission\Commands\PublishPermissionMigrations;
use EragPermission\Contracts\PermissionContract;
use EragPermission\Contracts\RoleContract;
use EragPermission\Middleware\RolePermissionMiddleware;
use EragPermission\Models\Permission;
use EragPermission\Models\Role;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Gate;
Expand All @@ -21,11 +24,6 @@ public function register(): void
PublishPermissionMigrations::class,
]);

$this->publishes([
__DIR__.'/Models/Role.php.stub' => app_path('Models/Role.php'),
__DIR__.'/Models/Permission.php.stub' => app_path('Models/Permission.php'),
], 'erag:publish-permission-models');

$this->publishes([
__DIR__.'/database/01_create_roles_table.php.stub' => database_path('migrations/0001_create_roles_table.php'),
__DIR__.'/database/02_create_permissions_table.php.stub' => database_path('migrations/0002_create_permissions_table.php'),
Expand All @@ -38,6 +36,8 @@ public function register(): void
$this->publishes([
__DIR__.'/database/Seeder/RolePermissionSeeder.php.stub' => database_path('seeders/RolePermissionSeeder.php'),
], 'erag:publish-permission-role-seeder');

$this->ModelBindings();
}

/**
Expand All @@ -47,21 +47,14 @@ public function boot(Router $router): void
{
require __DIR__.'/HelperRolePermission.php';

// Register middleware alias
$router->aliasMiddleware('role', RolePermissionMiddleware::class);
$router->middlewareGroup('role', [RolePermissionMiddleware::class]);

try {
if (file_exists(app_path('Models/Permission.php'))) {
Permission::with('roles.users')->get()->each(function ($permission) {
Gate::define($permission->name, function ($user) use ($permission) {
return $user->hasPermissionTo($permission);
});
});
}
} catch (\Exception $e) {
report($e);
}
Permission::with('roles.users')->get()->each(function ($permission) {
Gate::define($permission->name, function ($user) use ($permission) {
return $user->hasPermissionTo($permission);
});
});

Blade::directive('role', function ($role) {
return "<?php if(auth()->check() && auth()->user()->hasRole({$role})) : ?>";
Expand All @@ -79,4 +72,15 @@ public function boot(Router $router): void
return '<?php endif; ?>';
});
}

protected function ModelBindings()
{
$this->app->bind(RoleContract::class, function ($app) {
return new Role;
});

$this->app->bind(PermissionContract::class, function ($app) {
return new Permission;
});
}
}
4 changes: 2 additions & 2 deletions src/Traits/HasPermissionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace EragPermission\Traits;

use App\Models\Permission;
use App\Models\Role;
use EragPermission\Models\Permission;
use EragPermission\Models\Role;

trait HasPermissionsTrait
{
Expand Down
2 changes: 1 addition & 1 deletion src/database/03_create_users_permissions_table.php.stub
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use App\Models\Permission;
use App\Models\User;
use EragPermission\Models\Permission;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand Down
2 changes: 1 addition & 1 deletion src/database/04_users_roles.php.stub
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use App\Models\Role;
use App\Models\User;
use EragPermission\Models\Role;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand Down
4 changes: 2 additions & 2 deletions src/database/05_create_roles_permissions_table.php.stub
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use App\Models\Permission;
use App\Models\Role;
use EragPermission\Models\Role;
use EragPermission\Models\Permission;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand Down
4 changes: 2 additions & 2 deletions src/database/Seeder/RolePermissionSeeder.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Database\Seeders;

use App\Models\Role;
use App\Models\User;
use App\Models\Permission;
use EragPermission\Models\Role;
use EragPermission\Models\Permission;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
Expand Down

0 comments on commit 55e3223

Please sign in to comment.