Skip to content

Commit c2f35f5

Browse files
committed
initial commit
0 parents  commit c2f35f5

File tree

9 files changed

+574
-0
lines changed

9 files changed

+574
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
vendor/
2+
build/
3+
composer.lock
4+
test.db
5+
.vscode/
6+
coverage/
7+
clover.xml
8+
.phpunit.result.cache
9+
.runway-config.json

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 FlightPHP, n0nag0n
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Flight Permissions Plugin
2+
[![Latest Stable Version](http://poser.pugx.org/flightphp/permissions/v)](https://packagist.org/packages/flightphp/permissions)
3+
[![License](https://poser.pugx.org/flightphp/permissions/license)](https://packagist.org/packages/flightphp/permissions)
4+
[![PHP Version Require](http://poser.pugx.org/flightphp/permissions/require/php)](https://packagist.org/packages/flightphp/permissions)
5+
[![Dependencies](http://poser.pugx.org/flightphp/permissions/dependents)](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

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "flightphp/permissions",
3+
"type": "library",
4+
"description": "Library for managing permissions in Flight Applications",
5+
"keywords": ["permissions", "authentication","lite","simple"],
6+
"homepage": "https://docs.flightphp.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "n0nag0n",
11+
"email": "[email protected]",
12+
"role": "Owner"
13+
}
14+
],
15+
"require": {
16+
"php": ">=7.4"
17+
},
18+
"require-dev": {
19+
"phpunit/phpunit": "^9.0",
20+
"squizlabs/php_codesniffer": "^3.8",
21+
"rregeer/phpunit-coverage-check": "^0.3.1",
22+
"flightphp/core": "^3.10",
23+
"wruczek/php-file-cache": "^0.0.5"
24+
},
25+
"autoload": {
26+
"psr-4": {"flight\\": "src/"}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {"flight\\tests\\": "tests/"}
30+
},
31+
"scripts": {
32+
"test": "phpunit",
33+
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
34+
"beautify": "phpcbf --standard=phpcs.xml",
35+
"phpcs": "phpcs --standard=phpcs.xml"
36+
}
37+
}

phpcs.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset name="pcsg-generated-ruleset">
3+
<description>Created with the PHP Coding Standard Generator. http://edorian.github.io/php-coding-standard-generator/</description>
4+
<arg name="colors"/>
5+
<arg name="tab-width" value="4"/>
6+
<rule ref="PSR12"/>
7+
<!-- <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/> -->
8+
<file>src/</file>
9+
<file>tests/</file>
10+
</ruleset>

phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<coverage>
4+
<include>
5+
<directory>src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="ActiveRecord Tests">
10+
<directory>tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

0 commit comments

Comments
 (0)