Skip to content

Commit

Permalink
Fill readme
Browse files Browse the repository at this point in the history
  • Loading branch information
potievdev committed Dec 23, 2017
1 parent 8b68537 commit e3ef1f0
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 13 deletions.
97 changes: 88 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,49 @@
This package helps you to release access control logic via [RBAC](https://en.wikipedia.org/wiki/Role-based_access_control) (Role Based Access Control) technology.

## Requirements
___
---

- The minimum required PHP version is PHP 5.4.

## Installation
___
---

### First step
Run next command.

```sh
$ composer require potievdev/slim-rbac "^1.0"
```

### Second step
After installing packages, you should apply database migrations for creating necessary tables. There are two ways dependently from you use or not use Doctrine2 ORM in your project.

After installing packages, you should apply database migrations for creating necessary tables.
There are two cases dependency from your project uses or not uses Doctrine 2 ORM.

##### If you use Doctrine2

1. Create a php file, which returns instance of EntityManager.

```php
...
return $entityManager;
```

2. Run applying migrations command

```sh
$ vendor/bin/slim-rbac migrate -c PATH_TO_ENTITY_MANAGER_FILE
```

#### Else

1. Run next command in root directory or in directory where save configuration files of project

```sh
$ vendor/bin/slim-rbac create
```

Command creates the `sr-config.php` file with next content

```php
<?php

Expand All @@ -59,22 +73,88 @@ $config = Setup::createAnnotationMetadataConfiguration([], false, null, null, fa

return EntityManager::create($dbParams, $config);
```

Configure database connections params and run applying migrations command

```sh
$ vendor/bin/slim-rbac migrate
```

If all OK you must see similar screen

![alt text](https://3.downloader.disk.yandex.ru/disk/782e11e8921bcfee4ea27a72435df7daed1143be642dc7d16f0e56f551e82c71/5a3db17f/YDaZG483KqpGyRGM7PeuU7xeykrU0TVIUy_eD6Cnj68YRmNeDwNIGK0sEtC9132Xv_8Lm7GO59c_KhyTJ4s3Cw%3D%3D?uid=0&filename=2017-12-23_00-12-42.png&disposition=inline&hash=&limit=0&content_type=image%2Fpng&fsize=14626&hid=f6d1fe369a897a4c23f44ef036e2d5a6&media_type=image&tknv=v2&etag=3d96d11be88f0d4c592264e02c6ccb50)

## How to use
___
---

There are tree major components

- `AuthOptions` - component for saving configurations
- `AuthManager` - component for managing roles and permissions
- `AuthMiddleware` - Slim3 middleware component

### How initialize middleware

1. Create AuthOptions instance and configure it

```php

$authOptions = new AuthOptions();
// Setting entity manager instance
$authOptions->setEntityManager($entityManager);

```
2. Adding middleware

```php
// $app is instance of Slim application
$app->add(new AuthMiddleware($authOptions));
```
### How initialize manager
1. Create AuthOptions instance and configure it

```php

$authOptions = new AuthOptions();
// Setting entity manager instance
$authOptions->setEntityManager($entityManager);

```
2. Create manager instance

```php
$authManager = new AuthManager($this->authOptions);
```

Simple example
```php
// Creating edit permission
$edit = $this->authManager->createPermission('edit');
$this->authManager->addPermission($edit);
// Creating write permission
$write = $this->authManager->createPermission('write');
$this->authManager->addPermission($write);
// Creating moderator role
$moderator = $this->authManager->createRole('moderator');
$this->authManager->addRole($moderator);
// Creating admin role
$admin = $this->authManager->createRole('admin');
$this->authManager->addRole($admin);
// Adding permissions to roles
$this->authManager->addChildPermission($moderator, $edit);
$this->authManager->addChildPermission($admin, $write);
// Adding child role to role
$this->authManager->addChildRole($admin, $moderator);
// Assigning roles to users
$this->authManager->assign($moderator, 1);
$this->authManager->assign($admin, 2);
```

## Contribution
___
---

## Licience
___
---
MIT License

Copyright (c) 2017 ABDULMALIK ABDULPOTIEV
Expand All @@ -95,5 +175,4 @@ 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.

SOFTWARE.
20 changes: 19 additions & 1 deletion src/Component/AuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Potievdev\SlimRbac\Component;

use Potievdev\SlimRbac\Structure\AuthOptions;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -22,8 +23,25 @@ class AuthMiddleware extends BaseComponent
*/
public function __invoke($request, $response, $next)
{
$variableName = $this->authOptions->getVariableName();
$storageType = $this->authOptions->getVariableStorageType();

/** @var integer $userId */
$userId = $request->getAttribute('userId');
switch ($storageType) {

case AuthOptions::ATTRIBUTE_STORAGE_TYPE:
$userId = $request->getAttribute($variableName);
break;

case AuthOptions::HEADER_STORAGE_TYPE:
$userId = $request->getHeaderLine($variableName);
break;

case AuthOptions::COOKIE_STORAGE_TYPE:
$params = $request->getCookieParams();
$userId = $params[$variableName];
break;
}

/** @var string $permissionName */
$permissionName = $request->getUri()->getPath();
Expand Down
10 changes: 7 additions & 3 deletions src/Component/BaseComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
class BaseComponent
{
/** @var AuthOptions $authOptions */
protected $authOptions;

/** @var EntityManager $entityManager */
protected $entityManager;

Expand All @@ -22,11 +25,12 @@ class BaseComponent

/**
* AuthManager constructor.
* @param AuthOptions $options
* @param AuthOptions $authOptions
*/
public function __construct(AuthOptions $options)
public function __construct(AuthOptions $authOptions)
{
$this->entityManager = $options->getEntityManager();
$this->authOptions = $authOptions;
$this->entityManager = $authOptions->getEntityManager();
$this->repositoryRegistry = new RepositoryRegistry($this->entityManager);
}

Expand Down
44 changes: 44 additions & 0 deletions src/Structure/AuthOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,22 @@
class AuthOptions
{

/** Default field name */
const DEFAULT_VARIABLE_NAME = 'userId';

const ATTRIBUTE_STORAGE_TYPE = 1;
const HEADER_STORAGE_TYPE = 2;
const COOKIE_STORAGE_TYPE = 3;

/** @var \Doctrine\ORM\EntityManager */
private $entityManager;

/** @var string $variableName variable name which saves user identifier */
private $variableName = self::DEFAULT_VARIABLE_NAME;

/** @var int $variableStorageType Type of storage where saves variable value */
private $variableStorageType = self::ATTRIBUTE_STORAGE_TYPE;

/**
* @return \Doctrine\ORM\EntityManager
*/
Expand All @@ -30,4 +43,35 @@ public function setEntityManager($entityManager)
$this->entityManager = $entityManager;
}

/**
* @return string
*/
public function getVariableName()
{
return $this->variableName;
}

/**
* @param string $variableName
*/
public function setVariableName($variableName)
{
$this->variableName = $variableName;
}

/**
* @return int
*/
public function getVariableStorageType()
{
return $this->variableStorageType;
}

/**
* @param int $variableStorageType
*/
public function setVariableStorageType($variableStorageType)
{
$this->variableStorageType = $variableStorageType;
}
}

0 comments on commit e3ef1f0

Please sign in to comment.