PHPackages                             marcocastignoli/authorization - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. marcocastignoli/authorization

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

marcocastignoli/authorization
=============================

A package to manage authorization for Lumen

1.0.0(9y ago)1212MITPHP

Since Mar 27Pushed 8y ago2 watchersCompare

[ Source](https://github.com/marcocastignoli/authorization)[ Packagist](https://packagist.org/packages/marcocastignoli/authorization)[ RSS](/packages/marcocastignoli-authorization/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

[![Logo](https://camo.githubusercontent.com/174d2e66c18a0314b639de0d782c8340305630ec1d4343c7a1b61c95c941c6fd/68747470733a2f2f706963686f737465722e6e65742f696d616765732f323031372f30332f32372f36303730646665333862366230346166613035303462383332656566366339362e74682e706e67)](https://camo.githubusercontent.com/174d2e66c18a0314b639de0d782c8340305630ec1d4343c7a1b61c95c941c6fd/68747470733a2f2f706963686f737465722e6e65742f696d616765732f323031372f30332f32372f36303730646665333862366230346166613035303462383332656566366339362e74682e706e67)

authorization
=============

[](#authorization)

[![Latest Stable Version](https://camo.githubusercontent.com/13e185962b6b516a89c3d780d2e043a4daae6db60d3d01f501d84b6035be58f2/68747470733a2f2f706f7365722e707567782e6f72672f6d6172636f6361737469676e6f6c692f617574686f72697a6174696f6e2f76657273696f6e)](https://packagist.org/packages/marcocastignoli/authorization)[![Total Downloads](https://camo.githubusercontent.com/b644dfc7358de64fda8fffc20486bf65e458bbd90e8fee6e1c2097d96942490f/68747470733a2f2f706f7365722e707567782e6f72672f6d6172636f6361737469676e6f6c692f617574686f72697a6174696f6e2f646f776e6c6f616473)](https://packagist.org/packages/marcocastignoli/authorization)[![Latest Unstable Version](https://camo.githubusercontent.com/564f476768c6de18187c7ed27e12f27d2af5292604157c4af6a32bfe3a067691/68747470733a2f2f706f7365722e707567782e6f72672f6d6172636f6361737469676e6f6c692f617574686f72697a6174696f6e2f762f756e737461626c65)](//packagist.org/packages/marcocastignoli/authorization)[![License](https://camo.githubusercontent.com/91994c8dbec105fff278d7b415d703b6a801af978d0532bf672e42cebb5c82c3/68747470733a2f2f706f7365722e707567782e6f72672f6d6172636f6361737469676e6f6c692f617574686f72697a6174696f6e2f6c6963656e7365)](https://packagist.org/packages/marcocastignoli/authorization)

A package to manage authorization for Lumen

Dependencies
------------

[](#dependencies)

- PHP &gt;= 7.0
- Lumen &gt;= 5.3

Authentication system
---------------------

[](#authentication-system)

First, you have to implement an authentication system, I suggest you to use "passport", for Lumen you can check this:

Installation via Composer
-------------------------

[](#installation-via-composer)

Install Lumen if you don't have it yet:

```
$ composer create-project --prefer-dist laravel/lumen lumen-app
```

Then install "authorization":

```
$ cd lumen-app
$ composer require marcocastignoli/authorization
```

Or if you prefer, edit `composer.json` manually:

```
{
    "require": {
        "marcocastignoli/authorization": "dev-master"
    }
}
```

### Modify the bootstrap flow (`bootstrap/app.php` file)

[](#modify-the-bootstrap-flow-bootstrapappphp-file)

```
// Enable Facades
$app->withFacades();

// Enable Eloquent
$app->withEloquent();

// Register the service provider
$app->register(marcocastignoli\authorization\AuthorizationProvider::class);
```

### Migrate and seed the database

[](#migrate-and-seed-the-database)

```
# Create new tables
php artisan migrate

# Seed the database
php artisan db:seed --class=marcocastignoli\\authorization\\AuthorizationSeeder
```

Documentation
-------------

[](#documentation)

This package provides a simple way to create permissions for your application.

### Writing permissions

[](#writing-permissions)

In the *users* table you have to assign to the user an *auth* level.

In the *authorizations* table you can create the permissions.

- **auth**: a permission is referred to the user's auth level.
- **object**: a permission is referred to a Lumen's model.
- **field**: a permission is referred to a field of the object.
- **method**: a permission is referred to the action of the request (get, put, post, del).
- **entity**: a permission is referred to the entity of the request. (For example in "show *all* users" the entity is "*all*".)

#### Examples

[](#examples)

For each sentence you can see its implementation in the authorizations table.

*The user with authorization 0 can see the id of everyone*

authobjectfieldmethodentity0Useridshowall*The user with authorization 1 can see the email and the username of everyone*

authobjectfieldmethodentity1Useremailshowall1Userusernameshowall*The user with authorization 2 can edit every field for his cars*

authobjectfieldmethodentity2Car\*postmy### Using permissions

[](#using-permissions)

#### Create a new *Model*

[](#create-a-new-model)

When you create a new model instead of extending *Model*, you have to extend *AuthorizationScopes*.

Inside every model you can use the following scopes to filter your queries.

- **show( $entity )**
- **post( $entity, $arguments )**
- **put( $entity, $arguments )**
- **del( $entity, $id )**

#### Examples

[](#examples-1)

```
// Get information about my user using the permission set in the authorizations table
App\User::show("my")->get();

// Edit all cars where id < 5
App\Cars::where("id", "
