Skip to content

Commit 19b2269

Browse files
committed
update readme
1 parent b132f47 commit 19b2269

File tree

1 file changed

+61
-6
lines changed

1 file changed

+61
-6
lines changed

README.md

+61-6
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ Add Traits to the application's user class:
8787
<?php
8888

8989
use Uwla\Lacl\Traits\HasRole;
90+
use Uwla\Lacl\Contracts\HasRoleContract;
9091
use Illuminate\Foundation\Auth\User as Authenticatable;
9192

92-
class User extends Authenticatable
93+
class User extends Authenticatable implements HasRoleContract
9394
{
9495
use HasRole;
9596

@@ -194,9 +195,24 @@ $users[0]->users
194195
### HasPermission
195196

196197
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:
198199

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
200216
exist):
201217

202218
```php
@@ -247,7 +263,7 @@ $role->delPermission(['user.view', 'user.del']); // using string names
247263
$role->delAllPermissions();
248264
```
249265

250-
Has role (returns `bool`):
266+
Has permission (returns `bool`):
251267

252268
```php
253269
<?php
@@ -328,8 +344,47 @@ $user->add($permission);
328344
$user->hasPermission('article.edit', Article::class, $article->id); // true
329345
```
330346

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.
333388

334389
### Per-model permission
335390

0 commit comments

Comments
 (0)