|
| 1 | +# Flight Permissions Plugin |
| 2 | +[](https://packagist.org/packages/flightphp/permissions) |
| 3 | +[](https://packagist.org/packages/flightphp/permissions) |
| 4 | +[](https://packagist.org/packages/flightphp/permissions) |
| 5 | +[](https://packagist.org/packages/flightphp/permissions) |
| 6 | + |
| 7 | +Permissions are an important part to any application. Even in a RESTful API you'll need to check that the API key has permission to perform the action requested. In some cases it makes sense to handle authentication in a middleware, but in other cases, it's more helpful to have a standard set of permissions. |
| 8 | + |
| 9 | +This library follows a CRUD based permissions systems. See [basic example](#basic-example) for example on how this is accomplished. |
| 10 | + |
| 11 | +## Basic Example |
| 12 | + |
| 13 | +Let's assume you have a feature in your application that checks if a user is logged in. You can create a permissions object like this: |
| 14 | + |
| 15 | +```php |
| 16 | +// index.php |
| 17 | +require 'vendor/autoload.php'; |
| 18 | + |
| 19 | +// some code |
| 20 | + |
| 21 | +// then you probably have something that tells you who the current role is of the person |
| 22 | +// likely you have something where you pull the current role |
| 23 | +// from a session variable which defines this |
| 24 | +// after someone logs in, otherwise they will have a 'guest' or 'public' role. |
| 25 | +$current_role = 'admin'; |
| 26 | + |
| 27 | +// setup permissions |
| 28 | +$permission = new \flight\Permission($current_role); |
| 29 | +$permission->defineRule('loggedIn', function($current_role) { |
| 30 | + return $current_role !== 'guest'; |
| 31 | +}); |
| 32 | + |
| 33 | +// You'll probably want to persist this object in Flight somewhere |
| 34 | +Flight::set('permission', $permission); |
| 35 | +``` |
| 36 | + |
| 37 | +Then in a controller somewhere, you might have something like this. |
| 38 | + |
| 39 | +```php |
| 40 | +<?php |
| 41 | + |
| 42 | +// some controller |
| 43 | +class SomeController { |
| 44 | + public function someAction() { |
| 45 | + $permission = Flight::get('permission'); |
| 46 | + if ($permission->has('loggedIn')) { |
| 47 | + // do something |
| 48 | + } else { |
| 49 | + // do something else |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +You can also use this to track if they have permission to do something in your application. |
| 56 | +For instance, if your have a way that users can interact with posting on your software, you can |
| 57 | +check if they have permission to perform certain actions. |
| 58 | + |
| 59 | +```php |
| 60 | +$current_role = 'admin'; |
| 61 | + |
| 62 | +// setup permissions |
| 63 | +$permission = new \flight\Permission($current_role); |
| 64 | +$permission->defineRule('post', function($current_role) { |
| 65 | + if($current_role === 'admin') { |
| 66 | + $permissions = ['create', 'read', 'update', 'delete']; |
| 67 | + } else if($current_role === 'editor') { |
| 68 | + $permissions = ['create', 'read', 'update']; |
| 69 | + } else if($current_role === 'author') { |
| 70 | + $permissions = ['create', 'read']; |
| 71 | + } else if($current_role === 'contributor') { |
| 72 | + $permissions = ['create']; |
| 73 | + } else { |
| 74 | + $permissions = []; |
| 75 | + } |
| 76 | + return $permissions; |
| 77 | +}); |
| 78 | +Flight::set('permission', $permission); |
| 79 | +``` |
| 80 | + |
| 81 | +Then in a controller somewhere... |
| 82 | + |
| 83 | +```php |
| 84 | +class PostController { |
| 85 | + public function create() { |
| 86 | + $permission = Flight::get('permission'); |
| 87 | + if ($permission->can('post.create')) { |
| 88 | + // do something |
| 89 | + } else { |
| 90 | + // do something else |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +See how much fun this is? Let's install it and get started! |
| 97 | + |
| 98 | +## Installation |
| 99 | + |
| 100 | +Simply install with Composer |
| 101 | + |
| 102 | +```php |
| 103 | +composer require flightphp/permissions |
| 104 | +``` |
| 105 | + |
| 106 | +## Documentation |
| 107 | + |
| 108 | +Head over to the [documentation page](https://docs.flightphp.com/awesome-plugins/permissions) to learn more about usage and how cool this thing is! :) |
| 109 | + |
| 110 | +## License |
| 111 | + |
| 112 | +MIT |
0 commit comments