Skip to content

Commit

Permalink
version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra committed Feb 25, 2024
1 parent 35b5560 commit 5617f80
Show file tree
Hide file tree
Showing 20 changed files with 584 additions and 525 deletions.
71 changes: 68 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,79 @@
# Laravel Simple ACL

## Requirements

- PHP 8.1
- Laravel 9+

## Installation

```JSON
{
"require": {
"rodrigopedra/laravel-simple-acl": "^0.1"
"rodrigopedra/laravel-simple-acl": "^1.0"
}
}
```

- Export configuration and set User model class name
- Optionally load Middleware
- Export configuration and set the project's User model class name
- default is: `App\Models\User`
- Add `\RodrigoPedra\LaravelSimpleACL\Concerns\HasACL` trait to the project's User model
- Run migrations
- Optionally load the included Middleware
- This will load and cache a logged in user's roles and permissions and define a gate to each permission

## Usage

Create roles and permissions through the included `RodrigoPedra\LaravelSimpleACL\Models\Role` and
`RodrigoPedra\LaravelSimpleACL\Models\Permission` Eloquent models.

Permissions are meant to be grouped into a role, you can create a database seeder, or migration for
your initial setup, for example:

```php
$addUsers = Permission::create([
'label' => 'add-users',
'description' => 'User is allowed to create new users',
'sort_index' => 1,
]);

$removeUsers = Permission::create([
'label' => 'remove-users',
'description' => 'User is allowed to remove users',
'sort_index' => 2,
]);

Role::create([
'label' => 'admin',
'sort_index' => 1,
])->attachPermission($addUsers)->attachPermission($removeUsers);

Role::create([
'label' => 'leader',
'sort_index' => 2,
])->attachPermission($addUsers);
```

You can then add or remove roles to individual users using the included trait's helper methods:

```php
$user->attachRole(Role::hasLabel('admin')->first());
$user->detachRole(Role::hasLabel('leader')->first());
```

If you add the `RodrigoPedra\LaravelSimpleACL\Http\Middleware\LoadSimpleACL` middleware
to your middleware stack, you can use Laravel's gate to check for permissions:

```php
if ($user->can('add-users')) {
// do something
}
```

Even on blade views

```blade
@can('add-users')
{{-- do something --}}
@endcan
```
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
],
"autoload": {
"psr-4": {
"RodrigoPedra\\LaravelSimpleACL\\": "src/LaravelSimpleACL/"
"RodrigoPedra\\LaravelSimpleACL\\": "src/"
}
},
"require": {
"php": "^7.2.5|^8.0",
"illuminate/support": "^6.20.12|^7.30.4|^8.22.1|^9.0",
"illuminate/config": "^6.20.12|^7.30.4|^8.22.1|^9.0",
"illuminate/database": "^6.20.12|^7.30.4|^8.22.1|^9.0",
"illuminate/http": "^6.20.12|^7.30.4|^8.22.1|^9.0"
"php": "^8.1",
"illuminate/support": "^9.0|^10.0|^11.0",
"illuminate/config": "^9.0|^10.0|^11.0",
"illuminate/database": "^9.0|^10.0|^11.0",
"illuminate/http": "^9.0|^10.0|^11.0"
},
"extra": {
"laravel": {
Expand Down
2 changes: 1 addition & 1 deletion config/simple-acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

return [
'db-connection' => null,
'user-class' => 'App\\User',
'user-class' => 'App\\Models\\User',
];
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRolesTable extends Migration
{
return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::connection('simple-acl')
->create('roles', function (Blueprint $table) {
$table->increments('id');
$table->id();

$table->unsignedSmallInteger('sort_index')->nullable();

$table->string('label')->unique();
$table->string('description');
$table->string('description')->nullable();

$table->timestamps();
$table->softDeletes();
Expand All @@ -32,8 +31,8 @@ public function up()
*
* @return void
*/
public function down()
public function down(): void
{
Schema::connection('simple-acl')->dropIfExists('roles');
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePermissionsTable extends Migration
{
return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::connection('simple-acl')
->create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->id();

$table->unsignedSmallInteger('sort_index')->nullable();

$table->string('label')->unique();
$table->string('description');
$table->string('description')->nullable();

$table->timestamps();
$table->softDeletes();
Expand All @@ -32,8 +31,8 @@ public function up()
*
* @return void
*/
public function down()
public function down(): void
{
Schema::connection('simple-acl')->dropIfExists('permissions');
}
}
};
41 changes: 41 additions & 0 deletions database/migrations/2017_09_17_100000_create_role_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use RodrigoPedra\LaravelSimpleACL\Models\Role;

return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
$userClassName = \config('simple-acl.user-class');

Schema::connection('simple-acl')
->create('role_user', function (Blueprint $table) use ($userClassName) {
/** @var \Illuminate\Database\Eloquent\Model $userModel */
$userModel = new $userClassName();

$table->foreignIdFor(Role::class)->constrained();
$table->foreignIdFor($userModel)->constrained();

$table->primary([$userModel->getForeignKey(), 'role_id']);

$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::connection('simple-acl')->dropIfExists('role_user');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use RodrigoPedra\LaravelSimpleACL\Models\Permission;
use RodrigoPedra\LaravelSimpleACL\Models\Role;

class CreatePermissionRoleTable extends Migration
{
return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::connection('simple-acl')
->create('permission_role', function (Blueprint $table) {
$table->unsignedInteger('permission_id');
$table->unsignedInteger('role_id');
$table->foreignIdFor(Permission::class)->constrained();
$table->foreignIdFor(Role::class)->constrained();

$table->primary(['role_id', 'permission_id']);

$table->timestamps();

$table->foreign('permission_id')->references('id')->on('permissions');
$table->foreign('role_id')->references('id')->on('roles');
});
}

Expand All @@ -32,8 +30,8 @@ public function up()
*
* @return void
*/
public function down()
public function down(): void
{
Schema::connection('simple-acl')->dropIfExists('permission_role');
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use RodrigoPedra\LaravelSimpleACL\Models\Permission;

return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
$userClassName = \config('simple-acl.user-class');

Schema::connection('simple-acl')
->create('permission_user', function (Blueprint $table) use ($userClassName) {
/** @var \Illuminate\Database\Eloquent\Model $userModel */
$userModel = new $userClassName();

$table->foreignIdFor(Permission::class)->constrained();
$table->foreignIdFor($userModel)->constrained();

$table->primary([$userModel->getForeignKey(), 'permission_id']);

$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::connection('simple-acl')->dropIfExists('permission_user');
}
};
49 changes: 0 additions & 49 deletions migrations/2017_09_17_100000_create_role_user_table.php

This file was deleted.

Loading

0 comments on commit 5617f80

Please sign in to comment.