12
12
This package allows you to manage user permissions and groups in a database.
13
13
14
14
* [ Installation] ( #installation )
15
+ * [ Using install command] ( #install-using-aclinstall-command )
16
+ * [ Step by step installation] ( #step-by-step-installation )
15
17
* [ Usage] ( #usage )
16
18
* [ Check for permissions] ( #checking-for-permissions )
19
+ * [ Check for permissions using wildcards] ( #checking-for-permissions-using-wildcards )
17
20
* [ Syncing user permissions] ( #syncing-user-permissions )
18
21
* [ Syncing group permissions] ( #syncing-group-permissions )
19
22
* [ Local scopes] ( #local-scopes )
@@ -37,7 +40,7 @@ This package allows you to manage user permissions and groups in a database.
37
40
38
41
39
42
## Installation
40
-
43
+
41
44
To get started with laravel-acl, use Composer to add the package to your project's dependencies:
42
45
43
46
``` bash
@@ -67,12 +70,24 @@ After installing the laravel-acl package, register the service provider in
67
70
Junges\ACL\ACLEventsServiceProvider::class,
68
71
];
69
72
```
73
+
74
+ ### Install using ` acl:install ` command
75
+
76
+ You can install this package by running the provided install command:
77
+ ``` bash
78
+ php artisan acl:install
79
+ ```
80
+
81
+ After run this command, the package installation is done. Proceed to the [ usage] ( #usage ) section.
82
+
83
+ ### Step by step installation
84
+
70
85
All migrations required for this package are already included. If you
71
86
need to customize the tables, you can publish [ the migrations] ( https://github.com/mateusjunges/laravel-acl/tree/master/src/database/migrations )
72
87
with:
73
88
74
89
``` bash
75
- php artisan vendor:publish --provider=" Junges\ACL\ACLServiceProvider" --tag=" migrations"
90
+ php artisan vendor:publish --provider=" Junges\ACL\ACLServiceProvider" --tag=" acl- migrations"
76
91
```
77
92
and set the ` config ` for ` custom_migrations ` to ` true ` , which is false by default.
78
93
@@ -89,7 +104,7 @@ publish the config file and update the tables array.
89
104
You can publish the config file with:
90
105
91
106
``` bash
92
- php artisan vendor:publish --provider=" Junges\ACL\ACLServiceProvider" --tag=" config"
107
+ php artisan vendor:publish --provider=" Junges\ACL\ACLServiceProvider" --tag=" acl- config"
93
108
```
94
109
95
110
When published, the [ ` config/acl.php ` ] ( https://github.com/mateusjunges/laravel-acl/blob/master/config/acl.php ) config file contains:
@@ -348,6 +363,37 @@ $group->hasAnyPermission([Permission::find(1), Permission::find(2), Permission::
348
363
$group->hasAnyPermission([1, 'permission-slug' Permission::find(3)]);
349
364
```
350
365
366
+ ## Checking for permissions using wildcards
367
+ Sometimes, you want to know if the logged in user has any permission related to users, like
368
+ ` *.users ` . It can easily be done with the ` ACLWildcardsTrait ` .
369
+
370
+ Add the ` ACLWildcardsTrait ` to your ` user ` model:
371
+
372
+ ``` php
373
+ use Illuminate\Foundation\Auth\User as Authenticatable;
374
+ use Junges\ACL\Traits\UsersTrait;
375
+ use Junges\ACL\Traits\ACLWildcardsTrait;
376
+
377
+ class User extends Authenticatable
378
+ {
379
+ use UsersTrait;
380
+ use ACLWildcardsTrait;
381
+
382
+ //
383
+ }
384
+ ```
385
+ Then, you can check for wildcard permissions using the ` hasPermissionWithWildcard ` method:
386
+ ``` php
387
+ $user->hasPermissionWithWildcards('users.*');
388
+ ```
389
+
390
+ You can also use this trait to check for group permissions using wildcards.
391
+ The ` ACLWildcardsTrait ` is used by the Group model by default:
392
+
393
+ ``` php
394
+ $group->hasPermissionWithWildcards('users.*');
395
+ ```
396
+
351
397
## Syncing user permissions
352
398
The user permissions can synced with this method:
353
399
``` php
@@ -541,14 +587,15 @@ Check for any group:
541
587
542
588
543
589
If you want to use the middleware provided by this package
544
- (` PermissionMiddleware ` , ` GroupMiddleware ` e ` PermissionOrGroupMiddleware ` ),
590
+ (` PermissionMiddleware ` , ` GroupMiddleware ` , ` HierarchicalPermissions ` e ` PermissionOrGroupMiddleware ` ),
545
591
you need to add them to the ` app/Http/Kernel.php ` file,
546
592
inside the ` routeMiddleware ` array:
547
593
``` php
548
594
protected $routeMiddleware = [
549
595
'permissions' => \Junges\ACL\Middlewares\PermissionMiddleware::class,
550
596
'groups' => \Junges\ACL\Middlewares\GroupMiddleware::class,
551
597
'permissionOrGroup' => \Junges\ACL\Middlewares\PermissionOrGroupMiddleware::class,
598
+ 'hierarchical_permissions' => \Junges\ACL\Middlewares\HierarchicalPermissionsMiddleware::class
552
599
];
553
600
```
554
601
Then you can protect you routes using middleware rules:
@@ -571,6 +618,12 @@ Route::get('/', function(){
571
618
})->middleware('groups:admin');
572
619
```
573
620
621
+ ``` php
622
+ Route::get('/', function(){
623
+ echo "Middlewares working!";
624
+ })->middleware('hierarchical_permissions:user.auth.admin');
625
+ ```
626
+
574
627
Alternatively, you can separate multiple groups or permissions with a ` | ` (pipe) character:
575
628
``` php
576
629
Route::get('/', function(){
@@ -590,6 +643,12 @@ Route::get('/', function(){
590
643
})->middleware('groups:admin|manager');
591
644
```
592
645
646
+ ``` php
647
+ Route::get('/', function(){
648
+ echo "Middlewares working!";
649
+ })->middleware('hierarchical_permissions:user.auth.admin|user.manager.user.admin');
650
+ ```
651
+
593
652
You can protect controller similarly, by setting desired middleware in the constructor:
594
653
595
654
``` php
@@ -605,6 +664,13 @@ public function __construct()
605
664
}
606
665
```
607
666
667
+ ``` php
668
+ public function __construct()
669
+ {
670
+ $this->middleware('hierarchical_permissions:user.auth.admin|user.manager.user.admin');
671
+ }
672
+ ```
673
+
608
674
The ` groups ` middleware will check if the current logged in user has any of the groups passed to the middleware.
609
675
610
676
The ` permissions ` middleware will check if the current logged in user has any of the required groups
@@ -613,6 +679,16 @@ for a route.
613
679
The ` permissionOrGroup ` will check if the current logged in user has any of the required permissions or
614
680
groups necessary to access a route.
615
681
682
+ The ` hierarchical_permissions ` middleware will check if the current logged in user has any of the "sub-permissions"
683
+ in the passed string. The ` user.manager.user.admin ` matches with all the following:
684
+ ``` text
685
+ user
686
+ user.manager
687
+ user.manager.user
688
+ user.manager.user.admin
689
+ ```
690
+ If the user has any of the above permissions, the access is granted.
691
+
616
692
In positive case, both middleware guarantee access to the route.
617
693
618
694
# Handling group and permission exceptions
@@ -756,7 +832,7 @@ This package also provides translations for some messages. To use them is easy:
756
832
- Change your ` config/app.php ` file locale for your corresponding locale, like ` en ` or ` pt-br ` .
757
833
- Publish the translation files with
758
834
``` bash
759
- php artisan vendor:publish --provider=" Junges\ACL\ACLServiceProvider" --tag=" translations"
835
+ php artisan vendor:publish --provider=" Junges\ACL\ACLServiceProvider" --tag=" acl- translations"
760
836
```
761
837
762
838
# Tests
0 commit comments