@@ -87,9 +87,10 @@ Add Traits to the application's user class:
87
87
<?php
88
88
89
89
use Uwla\Lacl\Traits\HasRole;
90
+ use Uwla\Lacl\Contracts\HasRoleContract;
90
91
use Illuminate\Foundation\Auth\User as Authenticatable;
91
92
92
- class User extends Authenticatable
93
+ class User extends Authenticatable implements HasRoleContract
93
94
{
94
95
use HasRole;
95
96
@@ -194,9 +195,24 @@ $users[0]->users
194
195
### HasPermission
195
196
196
197
Here, we will assign permissions to a role, but they can also be assigned
197
- directly to a user.
198
+ directly to a user or any model by using the traits and contracts as follow:
198
199
199
- Add a permission or multiple permissions to role (the permissions must already
200
+ ``` php
201
+ <?php
202
+
203
+ use Uwla\Lacl\Traits\HasPermission;
204
+ use Uwla\Lacl\Contracts\HasPermissionContract;
205
+ use Illuminate\Foundation\Auth\User as Authenticatable;
206
+
207
+ class User extends Authenticatable implements HasPermissionContract
208
+ {
209
+ use HasPermission;
210
+
211
+ //
212
+ }
213
+ ```
214
+
215
+ Add a permission or multiple permissions to a role (the permissions must already
200
216
exist):
201
217
202
218
``` php
@@ -247,7 +263,7 @@ $role->delPermission(['user.view', 'user.del']); // using string names
247
263
$role->delAllPermissions();
248
264
```
249
265
250
- Has role (returns ` bool ` ):
266
+ Has permission (returns ` bool ` ):
251
267
252
268
``` php
253
269
<?php
@@ -328,8 +344,47 @@ $user->add($permission);
328
344
$user->hasPermission('article.edit', Article::class, $article->id); // true
329
345
```
330
346
331
- You can perform any operations on ` Permission ` that are supported by Eloquent
332
- models, such as deleting, updating, fetching, searching, etc.
347
+ To get a permission by name:
348
+
349
+ ``` php
350
+ <?php
351
+ // standard Eloquent way
352
+ $permission = Permission::where('name', 'view documents')->first();
353
+
354
+ // shorter way
355
+ $permission = Permission::getByName('view documents');
356
+
357
+ // or many at once
358
+ $permissions = Permission::getByName([
359
+ 'view documents', 'edit documents', 'upload files'
360
+ ]);
361
+ ```
362
+
363
+ To get all roles associated with a permission:
364
+
365
+ ``` php
366
+ <?php
367
+ $roles = $permission->getRoles();
368
+
369
+ // or, get the role names
370
+ $roleNames = $permission->getRoleNames();
371
+ ```
372
+
373
+ To get all model of a custom model instance that have a permission:
374
+
375
+ ``` php
376
+ <?php
377
+ // the first parameter is the class of the model
378
+ // the second parameter is the name of the id column of the model
379
+ $users = $permission->getModels(User::class, 'id');
380
+
381
+ // it can be used on users, roles, or any model such as a Team
382
+ // a team could have permissions associated with the team members
383
+ $teams = $permissions->getModels(Team::class, 'id');
384
+ ```
385
+
386
+ It is worth noting that you can perform any operations on ` Permission ` that are
387
+ supported by Eloquent models, such as deleting, updating, fetching, etc.
333
388
334
389
### Per-model permission
335
390
0 commit comments