PHPackages                             machuga/authority-l4 - 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. machuga/authority-l4

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

machuga/authority-l4
====================

A simple and flexible authorization system for PHP

2.2.0(12y ago)15747.7k33[1 issues](https://github.com/machuga/authority-l4/issues)2PHPPHP &gt;=5.3.0

Since Apr 11Pushed 11y ago13 watchersCompare

[ Source](https://github.com/machuga/authority-l4)[ Packagist](https://packagist.org/packages/machuga/authority-l4)[ Docs](https://github.com/machuga/authority-l4)[ RSS](/packages/machuga-authority-l4/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (3)Dependencies (4)Versions (5)Used By (2)

THIS REPO IS DEPRECATED
=======================

[](#this-repo-is-deprecated)

I no longer use PHP enough to be able to work on Authority so it's time to EOL it. If you're interested in taking it over, ping me in an issue with "@machuga".

Authority-L4
============

[](#authority-l4)

A simple and flexible authorization system for Laravel 4
--------------------------------------------------------

[](#a-simple-and-flexible-authorization-system-for-laravel-4)

### Installation via Composer

[](#installation-via-composer)

Add Authority to your composer.json file to require Authority

```
require : {
	"laravel/framework": "~4",
    "machuga/authority-l4" : "dev-master"
}

```

Now update Composer

```
composer update

```

The last **required** step is to add the service provider to `app/config/app.php`

```
    'Authority\AuthorityL4\AuthorityL4ServiceProvider',
```

Congratulations, you have successfully installed Authority. However, we have also included some other configuration options for your convenience.

### Additional (optional) Configuration Options

[](#additional-optional-configuration-options)

##### Add the alias (facade) to your Laravel app config file.

[](#add-the-alias-facade-to-your-laravel-app-config-file)

```
    'Authority' => 'Authority\AuthorityL4\Facades\Authority',
```

This will allow you to access the Authority class through the static interface you are used to with Laravel components.

```
	Authority::can('update', 'SomeModel');
```

##### Publish the Authority default configuration file

[](#publish-the-authority-default-configuration-file)

```
	php artisan config:publish machuga/authority-l4
```

This will place a copy of the configuration file at `app/config/packages/machuga/authority-l4`. The config file includes an 'initialize' function, which is a great place to setup your rules and aliases.

```
	//app/config/packages/machuga/authority-l4

	return array(

		'initialize' => function($authority) {
			$user = $authority->getCurrentUser();

			//action aliases
			$authority->addAlias('manage', array('create', 'read', 'update', 'delete'));
        	$authority->addAlias('moderate', array('read', 'update', 'delete'));

        	//an example using the `hasRole` function, see below examples for more details
        	if($user->hasRole('admin')){
        		$authority->allow('manage', 'all');
			}
		}

	);
```

##### Create Roles and Permissions Tables

[](#create-roles-and-permissions-tables)

We have provided a basic table structure to get you started in creating your roles and permissions.

Run the Authority migrations

```
	php artisan migrate --package="machuga/authority-l4"
```

This will create the following tables

- roles
- role\_user
- permissions

To utilize these tables, you can add the following methods to your `User` model. You will also need to create Role and Permission Model stubs.

```
	//app/models/User.php
	public function roles() {
        return $this->belongsToMany('Role');
    }

    public function permissions() {
        return $this->hasMany('Permission');
    }

	public function hasRole($key) {
		foreach($this->roles as $role){
			if($role->name === $key)
			{
				return true;
			}
		}
		return false;
	}

	//app/models/Role.php
	class Role extends Eloquent {}

	//app/models/Permission.php
	class Permission extends Eloquent {}
```

Lastly, in your Authority config file which you copied over in the previous configuration step. You can add some rules:

```
