-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c2f35f5
Showing
9 changed files
with
574 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
vendor/ | ||
build/ | ||
composer.lock | ||
test.db | ||
.vscode/ | ||
coverage/ | ||
clover.xml | ||
.phpunit.result.cache | ||
.runway-config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 FlightPHP, n0nag0n | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Flight Permissions Plugin | ||
[](https://packagist.org/packages/flightphp/permissions) | ||
[](https://packagist.org/packages/flightphp/permissions) | ||
[](https://packagist.org/packages/flightphp/permissions) | ||
[](https://packagist.org/packages/flightphp/permissions) | ||
|
||
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. | ||
|
||
This library follows a CRUD based permissions systems. See [basic example](#basic-example) for example on how this is accomplished. | ||
|
||
## Basic Example | ||
|
||
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: | ||
|
||
```php | ||
// index.php | ||
require 'vendor/autoload.php'; | ||
|
||
// some code | ||
|
||
// then you probably have something that tells you who the current role is of the person | ||
// likely you have something where you pull the current role | ||
// from a session variable which defines this | ||
// after someone logs in, otherwise they will have a 'guest' or 'public' role. | ||
$current_role = 'admin'; | ||
|
||
// setup permissions | ||
$permission = new \flight\Permission($current_role); | ||
$permission->defineRule('loggedIn', function($current_role) { | ||
return $current_role !== 'guest'; | ||
}); | ||
|
||
// You'll probably want to persist this object in Flight somewhere | ||
Flight::set('permission', $permission); | ||
``` | ||
|
||
Then in a controller somewhere, you might have something like this. | ||
|
||
```php | ||
<?php | ||
|
||
// some controller | ||
class SomeController { | ||
public function someAction() { | ||
$permission = Flight::get('permission'); | ||
if ($permission->has('loggedIn')) { | ||
// do something | ||
} else { | ||
// do something else | ||
} | ||
} | ||
} | ||
``` | ||
|
||
You can also use this to track if they have permission to do something in your application. | ||
For instance, if your have a way that users can interact with posting on your software, you can | ||
check if they have permission to perform certain actions. | ||
|
||
```php | ||
$current_role = 'admin'; | ||
|
||
// setup permissions | ||
$permission = new \flight\Permission($current_role); | ||
$permission->defineRule('post', function($current_role) { | ||
if($current_role === 'admin') { | ||
$permissions = ['create', 'read', 'update', 'delete']; | ||
} else if($current_role === 'editor') { | ||
$permissions = ['create', 'read', 'update']; | ||
} else if($current_role === 'author') { | ||
$permissions = ['create', 'read']; | ||
} else if($current_role === 'contributor') { | ||
$permissions = ['create']; | ||
} else { | ||
$permissions = []; | ||
} | ||
return $permissions; | ||
}); | ||
Flight::set('permission', $permission); | ||
``` | ||
|
||
Then in a controller somewhere... | ||
|
||
```php | ||
class PostController { | ||
public function create() { | ||
$permission = Flight::get('permission'); | ||
if ($permission->can('post.create')) { | ||
// do something | ||
} else { | ||
// do something else | ||
} | ||
} | ||
} | ||
``` | ||
|
||
See how much fun this is? Let's install it and get started! | ||
|
||
## Installation | ||
|
||
Simply install with Composer | ||
|
||
```php | ||
composer require flightphp/permissions | ||
``` | ||
|
||
## Documentation | ||
|
||
Head over to the [documentation page](https://docs.flightphp.com/awesome-plugins/permissions) to learn more about usage and how cool this thing is! :) | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "flightphp/permissions", | ||
"type": "library", | ||
"description": "Library for managing permissions in Flight Applications", | ||
"keywords": ["permissions", "authentication","lite","simple"], | ||
"homepage": "https://docs.flightphp.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "n0nag0n", | ||
"email": "[email protected]", | ||
"role": "Owner" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.4" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.0", | ||
"squizlabs/php_codesniffer": "^3.8", | ||
"rregeer/phpunit-coverage-check": "^0.3.1", | ||
"flightphp/core": "^3.10", | ||
"wruczek/php-file-cache": "^0.0.5" | ||
}, | ||
"autoload": { | ||
"psr-4": {"flight\\": "src/"} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": {"flight\\tests\\": "tests/"} | ||
}, | ||
"scripts": { | ||
"test": "phpunit", | ||
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100", | ||
"beautify": "phpcbf --standard=phpcs.xml", | ||
"phpcs": "phpcs --standard=phpcs.xml" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ruleset name="pcsg-generated-ruleset"> | ||
<description>Created with the PHP Coding Standard Generator. http://edorian.github.io/php-coding-standard-generator/</description> | ||
<arg name="colors"/> | ||
<arg name="tab-width" value="4"/> | ||
<rule ref="PSR12"/> | ||
<!-- <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/> --> | ||
<file>src/</file> | ||
<file>tests/</file> | ||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="true" executionOrder="random" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> | ||
<coverage> | ||
<include> | ||
<directory>src/</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="ActiveRecord Tests"> | ||
<directory>tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
Oops, something went wrong.