You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
44
46
45
47
To get the latest version, simply require the project using Composer:
48
+
46
49
```sh
47
50
$ composer require enea/laravel-authorization
48
51
```
49
52
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`.
51
56
52
57
and finally, it only remains to run in the console:
58
+
53
59
```sh
54
60
$ php artisan authorization:install
55
61
```
56
62
57
63
## Quick Start
64
+
58
65
Starting with laravel-authorization is as simple as extending the `User` model that provides the package:
66
+
59
67
```php
60
68
use Enea\Authorization\Models\User as Authorizable;
61
69
62
70
class User extends Authorizable {
63
71
//
64
72
}
65
73
```
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
+
67
78
```php
68
79
use Enea\Authorization\Contracts\Authorizable as AuthorizableContract;
69
80
use Enea\Authorization\Traits\Authorizable;
@@ -76,17 +87,20 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
76
87
use Authenticatable, Authorizable;
77
88
}
78
89
```
90
+
79
91
### Checks
92
+
80
93
There are some methods available for checking roles and permissions:
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)
To prohibit certain accesses to a user can do it through the method `deny` and `denyMultiple`:
158
+
136
159
```php
137
160
// deny a permission to a user
138
161
$user->deny($permission);
139
162
// deny multiple permissions to a user
140
163
$user->denyMultiple($permissions);
141
164
```
165
+
142
166
## 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
+
144
171
```php
145
172
// automatic middleware configuration.
146
173
'middleware' => [
@@ -157,7 +184,9 @@ The middleware are activated automatically from the beginning, to change this yo
157
184
],
158
185
159
186
```
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:
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):
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.
179
212
180
213
### 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
+
182
218
```php
183
219
public function render($request, Exception $exception)
184
220
{
@@ -188,34 +224,44 @@ public function render($request, Exception $exception)
188
224
return parent::render($request, $exception);
189
225
}
190
226
```
227
+
191
228
## Blade Directives
229
+
192
230
This package also adds Blade directives to verify if the currently connected user has a specific role or permission.
193
231
Optionally you can pass in the `guard` that the check will be performed on as a second argument.
0 commit comments