Skip to content

Commit 1103046

Browse files
committed
docs: update readme
1 parent 8e27d55 commit 1103046

File tree

1 file changed

+65
-16
lines changed

1 file changed

+65
-16
lines changed

README.md

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Laravel Authorization
22
=====================
33

4-
[![Build Status](https://github.com/vaened/laravel-authorization/actions/workflows/tests.yml/badge.svg)](https://github.com/vaened/laravel-authorization/actions?query=workflow%3ATests) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/vaened/laravel-authorization/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/vaened/laravel-authorization/?branch=master) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
4+
[![Build Status](https://github.com/vaened/laravel-authorization/actions/workflows/tests.yml/badge.svg)](https://github.com/vaened/laravel-authorization/actions?query=workflow%3ATests) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/vaened/laravel-authorization/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/vaened/laravel-authorization/?branch=master) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
55

66
Laravel Authorization is a package that provides a simple administration interface for roles and permissions.
77

@@ -30,6 +30,7 @@ $user->can('annul-documents'); // false
3030
```
3131

3232
## Table of Contents
33+
3334
* [Installation](#installation)
3435
* [Quick Start](#quick-start)
3536
- [checks](#checks)
@@ -40,30 +41,40 @@ $user->can('annul-documents'); // false
4041
* [Blade Directives](#blade-directives)
4142

4243
## Installation
43-
Laravel Authorization requires PHP 8.1. This version supports Laravel 9 only.
44+
45+
Laravel Authorization requires PHP 8.1. This version supports Laravel 10 only.
4446

4547
To get the latest version, simply require the project using Composer:
48+
4649
```sh
4750
$ composer require enea/laravel-authorization
4851
```
4952

50-
Once installed, if you are not using automatic package discovery, then you need to register the [Enea\Authorization\AuthorizationServiceProvider](https://github.com/eneav/laravel-authorization/blob/master/src/AuthorizationServiceProvider.php) service provider in your `config/app.php`.
53+
Once installed, if you are not using automatic package discovery, then you need to register
54+
the [Enea\Authorization\AuthorizationServiceProvider](https://github.com/eneav/laravel-authorization/blob/master/src/AuthorizationServiceProvider.php)
55+
service provider in your `config/app.php`.
5156

5257
and finally, it only remains to run in the console:
58+
5359
```sh
5460
$ php artisan authorization:install
5561
```
5662

5763
## Quick Start
64+
5865
Starting with laravel-authorization is as simple as extending the `User` model that provides the package:
66+
5967
``` php
6068
use Enea\Authorization\Models\User as Authorizable;
6169

6270
class User extends Authorizable {
6371
//
6472
}
6573
```
66-
Or in case you need to customize your user model, you must implement the `Enea\Authorization\Contracts\Authorisable` interface and use the `Enea\Authorization\Traits\Authorisable` trait:
74+
75+
Or in case you need to customize your user model, you must implement the `Enea\Authorization\Contracts\Authorisable` interface and use
76+
the `Enea\Authorization\Traits\Authorisable` trait:
77+
6778
``` php
6879
use Enea\Authorization\Contracts\Authorizable as AuthorizableContract;
6980
use Enea\Authorization\Traits\Authorizable;
@@ -76,17 +87,20 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
7687
use Authenticatable, Authorizable;
7788
}
7889
```
90+
7991
### Checks
92+
8093
There are some methods available for checking roles and permissions:
8194

82-
Method | Parameter | Return
83-
------------------|-----------------|------------------
84-
can | permission-name | boolean
85-
cannot | permission-name | boolean
86-
isMemberOf | role-name | boolean
87-
isntMemberOf | role-name | boolean
95+
Method | Parameter | Return
96+
--------------|-----------------|---------
97+
can | permission-name | boolean
98+
cannot | permission-name | boolean
99+
isMemberOf | role-name | boolean
100+
isntMemberOf | role-name | boolean
88101

89102
#### Example
103+
90104
```php
91105
// verify if a user has a permission
92106
$user->can('permission-name');
@@ -97,7 +111,9 @@ $user->isMemberOf('role-name');
97111
// verify if a user is not a member of a role
98112
$user->isntMemberOf('role-name');
99113
```
114+
100115
On the other hand, a role can only have permissions:
116+
101117
```php
102118
// verify if a role has a permission
103119
$role->can('permission-name');
@@ -106,7 +122,9 @@ $role->cannot('permission-name');
106122
```
107123

108124
### GRANT
109-
Simplify the way in which roles and permissions are granted, both can be granted through the `grant` method in your model, you can see an example [here](https://github.com/eneav/laravel-authorization-example/blob/master/database/seeds/AuthorizationsSeeder.php)
125+
126+
Simplify the way in which roles and permissions are granted, both can be granted through the `grant` method in your model, you can see an
127+
example [here](https://github.com/eneav/laravel-authorization-example/blob/master/database/seeds/AuthorizationsSeeder.php)
110128

111129
```php
112130
// grant an authorization to user
@@ -118,8 +136,11 @@ $role->grant($permission);
118136
// grant multiple permissions to role
119137
$user->grantMultiple([$firstPermission, $secondPermission]);
120138
```
139+
121140
### REVOKE
141+
122142
To revoke a permission or role of a model, you must use the `revoke` or `revokeMultiple` method:
143+
123144
```php
124145
// revoke an authorization to a user
125146
$user->revoke($authorization);
@@ -132,15 +153,21 @@ $user->revokeMultiple([$firstPermission, $secondPermission]);
132153
```
133154

134155
### DENY
156+
135157
To prohibit certain accesses to a user can do it through the method `deny` and `denyMultiple`:
158+
136159
```php
137160
// deny a permission to a user
138161
$user->deny($permission);
139162
// deny multiple permissions to a user
140163
$user->denyMultiple($permissions);
141164
```
165+
142166
## Middleware
143-
The middleware are activated automatically from the beginning, to change this you can do it from the [configuration](https://github.com/eneav/laravel-authorization/blob/master/config/authorization.php) file:
167+
168+
The middleware are activated automatically from the beginning, to change this you can do it from
169+
the [configuration](https://github.com/eneav/laravel-authorization/blob/master/config/authorization.php) file:
170+
144171
```php
145172
// automatic middleware configuration.
146173
'middleware' => [
@@ -157,7 +184,9 @@ The middleware are activated automatically from the beginning, to change this yo
157184
],
158185

159186
```
160-
Or in case you want to do a manual configuration you can deactivate the automatic load and modify your [kernel](https://github.com/eneav/laravel-authorization-example/blob/master/app/Http/Kernel.php#L64-L65) file:
187+
188+
Or in case you want to do a manual configuration you can deactivate the automatic load and modify
189+
your [kernel](https://github.com/eneav/laravel-authorization-example/blob/master/app/Http/Kernel.php#L64-L65) file:
161190

162191
```php
163192
protected $routeMiddleware = [
@@ -168,17 +197,24 @@ protected $routeMiddleware = [
168197
'authenticated.is' => \Enea\Authorization\Middleware\RoleAuthorizerMiddleware::class,
169198
];
170199
```
171-
Then you can use it in your routes like any other [middleware](https://github.com/eneav/laravel-authorization-example/blob/master/routes/web.php#L33-L40):
200+
201+
Then you can use it in your routes like any
202+
other [middleware](https://github.com/eneav/laravel-authorization-example/blob/master/routes/web.php#L33-L40):
172203

173204
```php
174205
$router->get('create', 'CreateController@create')->middleware('authenticated.can:create-articles');
175206
$router->get('admin', 'DashboardController@index')->middleware('authenticated.is:admin');
176207
```
177208

178-
In case any user tries to access a protected route without authorization, an exception of type [`UnauthorizedOwnerException`](https://github.com/eneav/laravel-authorization/blob/master/src/Exceptions/UnauthorizedOwnerException.php) will be throw.
209+
In case any user tries to access a protected route without authorization, an exception of
210+
type [`UnauthorizedOwnerException`](https://github.com/eneav/laravel-authorization/blob/master/src/Exceptions/UnauthorizedOwnerException.php) will be
211+
throw.
179212

180213
### Custom errors
181-
To show a custom error, we can edit the [`Handler`](https://github.com/eneav/laravel-authorization-example/blob/master/app/Exceptions/Handler.php#L52-L54) file:
214+
215+
To show a custom error, we can edit
216+
the [`Handler`](https://github.com/eneav/laravel-authorization-example/blob/master/app/Exceptions/Handler.php#L52-L54) file:
217+
182218
```php
183219
public function render($request, Exception $exception)
184220
{
@@ -188,34 +224,44 @@ public function render($request, Exception $exception)
188224
return parent::render($request, $exception);
189225
}
190226
```
227+
191228
## Blade Directives
229+
192230
This package also adds Blade directives to verify if the currently connected user has a specific role or permission.
193231
Optionally you can pass in the `guard` that the check will be performed on as a second argument.
232+
194233
### For Roles
234+
195235
```php
196236
@authenticatedIs('articles-owner')
197237
// is articles owner
198238
@else
199239
// it's not articles owner
200240
@endauthenticatedIs
201241
```
242+
202243
and to deny
244+
203245
```php
204246
@authenticatedIsnt('articles-owner')
205247
// it's not articles owner
206248
@else
207249
// is articles owner
208250
@endauthenticatedIsnt
209251
```
252+
210253
### For Permissions
254+
211255
```php
212256
@authenticatedCan('edit-articles')
213257
// can edit articles
214258
@else
215259
// cannot edit articles
216260
@endauthenticatedCan
217261
```
262+
218263
and to deny
264+
219265
```php
220266
@authenticatedCannot('edit-articles')
221267
// cannot edit articles
@@ -225,10 +271,13 @@ and to deny
225271
```
226272

227273
## Examples
274+
228275
[Simple CRUD](https://github.com/eneav/laravel-authorization-example)
229276

230277
## Changelog
278+
231279
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
232280

233281
## License
282+
234283
Laravel Authorization is licensed under [The MIT License (MIT)](LICENSE.md).

0 commit comments

Comments
 (0)