Skip to content

Commit a3f0d5c

Browse files
author
Given Ncube
committed
docs: updated the readme
1 parent 8f0b73c commit a3f0d5c

File tree

1 file changed

+182
-22
lines changed

1 file changed

+182
-22
lines changed

README.md

Lines changed: 182 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,215 @@
88
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/flixtechs-labs/laravel-authorizer/Fix%20PHP%20code%20style%20issues?label=code%20style)](https://github.com/flixtechs-labs/laravel-authorizer/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
99
[![Total Downloads](https://img.shields.io/packagist/dt/flixtechs-labs/laravel-authorizer.svg?style=flat-square)](https://packagist.org/packages/flixtechs-labs/laravel-authorizer)
1010

11-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
11+
This package helps you to quickly create strong policy authorization logic in your Laravel application with minimal effort. In most cases the defaults will be just enough and all you'd need to do is
12+
authorize.
1213

13-
## Support us
14+
## Installation
15+
You can install the package via composer:
1416

15-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-authorizer.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-authorizer)
17+
```bash
18+
composer require flixtechs-labs/laravel-authorizer
19+
```
20+
You can publish the config file with:
1621

17-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
22+
```bash
23+
php artisan vendor:publish --tag="laravel-authorizer-config"
24+
```
1825

19-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
26+
This is the contents of the published config file:
2027

21-
## Installation
28+
```php
29+
return [
30+
'permissions' => [
31+
'create',
32+
'update',
33+
'delete',
34+
'view all',
35+
'view',
36+
'force delete',
37+
'restore',
38+
],
39+
];
40+
```
2241

23-
You can install the package via composer:
42+
## Setup
43+
44+
This package depends on the [spatie/laravel-permission](https://github.com/spatie/laravel-permission) package. It's installed automatically when you install this package.
45+
46+
To setup the package all you need to is run the following command:
2447

2548
```bash
26-
composer require flixtechs-labs/laravel-authorizer
49+
php artisan authorizer:setup
2750
```
2851

29-
You can publish and run the migrations with:
52+
If your project is ready you can generate the permissions on setup by adding the `--permissions` option:
3053

3154
```bash
32-
php artisan vendor:publish --tag="laravel-authorizer-migrations"
33-
php artisan migrate
55+
php artisan authorizer:setup --permissions
3456
```
57+
You can also generate the policies on setup by adding the `--policies` option:
3558

36-
You can publish the config file with:
59+
```bash
60+
php artisan authorizer:setup --policies
61+
```
62+
Or you can generate both on setup by adding the `--permissions` and `--policies` options:
3763

3864
```bash
39-
php artisan vendor:publish --tag="laravel-authorizer-config"
65+
php artisan authorizer:setup --permissions --policies
4066
```
67+
This will publish the migrations from the spatie/laravel-permission package, migrate the database and generate the permissions and policies.
4168

42-
This is the contents of the published config file:
69+
## Usage
70+
71+
This package generates a batteries included policy skeletons. You just have to generate a policy and authorize in your controllers.
72+
73+
### Generate a policy for one model
74+
75+
```bash
76+
php artisan authorizer:policies:generate Post --model=Post
77+
```
78+
79+
This will generate a `PostPolicy` in the `App\Policies\` namespace. The generated Policy would look something like this:
4380

4481
```php
45-
return [
46-
];
82+
<?php
83+
84+
namespace App\Policies;
85+
86+
use App\Enums\PostState;
87+
use App\Models\Post;
88+
use App\Models\User;
89+
use Illuminate\Auth\Access\HandlesAuthorization;
90+
use Illuminate\Auth\Access\Response;
91+
92+
class PostPolicy
93+
{
94+
use HandlesAuthorization;
95+
96+
/**
97+
* Determine whether the user can view any models.
98+
*
99+
* @param User $user
100+
* @return Response|bool
101+
*/
102+
public function viewAny(User $user): Response|bool
103+
{
104+
return $user->can('view all posts');
105+
}
106+
107+
/**
108+
* Determine whether the user can view the model.
109+
*
110+
* @param User|null $user
111+
* @param Post $post
112+
* @return Response|bool
113+
*/
114+
public function view(?User $user, Post $post): Response|bool
115+
{
116+
return $user->can('view post')
117+
}
118+
119+
/**
120+
* Determine whether the user can create models.
121+
*
122+
* @param User $user
123+
* @return Response|bool
124+
*/
125+
public function create(User $user): Response|bool
126+
{
127+
return $user->can('create post');
128+
}
129+
130+
/**
131+
* Determine whether the user can update the model.
132+
*
133+
* @param User $user
134+
* @param Post $post
135+
* @return Response|bool
136+
*/
137+
public function update(User $user, Post $post): Response|bool
138+
{
139+
return ($user->can('update post');
140+
}
141+
142+
/**
143+
* Determine whether the user can delete the model.
144+
*
145+
* @param User $user
146+
* @param Post $post
147+
* @return Response|bool
148+
*/
149+
public function delete(User $user, Post $post): Response|bool
150+
{
151+
return ($user->can('delete post');
152+
}
153+
154+
/**
155+
* Determine whether the user can restore the model.
156+
*
157+
* @param User $user
158+
* @param Post $post
159+
* @return Response|bool
160+
*/
161+
public function restore(User $user, Post $post): Response|bool
162+
{
163+
return $user->can('restore post');
164+
}
165+
166+
/**
167+
* Determine whether the user can permanently delete the model.
168+
*
169+
* @param User $user
170+
* @param Post $post
171+
* @return Response|bool
172+
*/
173+
public function forceDelete(User $user, Post $post): Response|bool
174+
{
175+
return $user->can('force delete post');
176+
}
177+
}
47178
```
179+
Now all you just need to do is authorize the user in your controllers:
180+
181+
```php
182+
<?php
183+
184+
namespace App\Controllers;
48185

49-
Optionally, you can publish the views using
186+
use App\Models\Post;
187+
188+
public function __construct()
189+
{
190+
$this->authorizeResource(Post::class, 'post');
191+
}
192+
```
193+
Or authorize per action
194+
195+
```php
196+
public function update(UpdatePostRequest $request, Post $post)
197+
{
198+
$this->authorize('update', $post);
199+
}
200+
```
201+
202+
### Generating policies for all models
50203

51204
```bash
52-
php artisan vendor:publish --tag="laravel-authorizer-views"
205+
php artisan authorizer:policies:generate
53206
```
207+
This will generate policies for all models in your project.
54208

55-
## Usage
209+
### Generating permissions for one model
210+
211+
```php
212+
php artisan authorizer:permissions:generate --model=Post
213+
```
214+
This will generate all the CRUD permissions for one specific model. You can add additional permission to be generated by adding them to the config file in `config/authorizer.php`
215+
216+
Or you can just generate for all the models
56217

57218
```php
58-
$laravelAuthorizer = new FlixtechsLabs\LaravelAuthorizer();
59-
echo $laravelAuthorizer->echoPhrase('Hello, FlixtechsLabs!');
219+
php artisan authorizer:generate:permissions
60220
```
61221

62222
## Testing
@@ -79,7 +239,7 @@ Please review [our security policy](../../security/policy) on how to report secu
79239

80240
## Credits
81241

82-
- [Given Ncube](https://github.com/flixtechs-labs)
242+
- [Given Ncube](https://github.com/slimgee)
83243
- [All Contributors](../../contributors)
84244

85245
## License

0 commit comments

Comments
 (0)