Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
n0nag0n committed Jun 4, 2024
0 parents commit c2f35f5
Show file tree
Hide file tree
Showing 9 changed files with 574 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
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
21 changes: 21 additions & 0 deletions LICENSE
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.
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Flight Permissions Plugin
[![Latest Stable Version](http://poser.pugx.org/flightphp/permissions/v)](https://packagist.org/packages/flightphp/permissions)
[![License](https://poser.pugx.org/flightphp/permissions/license)](https://packagist.org/packages/flightphp/permissions)
[![PHP Version Require](http://poser.pugx.org/flightphp/permissions/require/php)](https://packagist.org/packages/flightphp/permissions)
[![Dependencies](http://poser.pugx.org/flightphp/permissions/dependents)](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
37 changes: 37 additions & 0 deletions composer.json
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"
}
}
10 changes: 10 additions & 0 deletions phpcs.xml
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>
13 changes: 13 additions & 0 deletions phpunit.xml
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>
Loading

0 comments on commit c2f35f5

Please sign in to comment.