-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35b5560
commit 5617f80
Showing
20 changed files
with
584 additions
and
525 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
|
||
return [ | ||
'db-connection' => null, | ||
'user-class' => 'App\\User', | ||
'user-class' => 'App\\Models\\User', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
database/migrations/2017_09_17_100000_create_role_user_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
database/migrations/2017_09_17_100010_create_permission_user_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.