PHPackages                             coyote6/laravel-permissions - 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. coyote6/laravel-permissions

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

coyote6/laravel-permissions
===========================

Provides permission and role models to assign to your users. If using with coyote6/laravel-crud then admin pages are automatically added.

v0.1.8(9mo ago)066MITPHP

Since Oct 27Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/Coyote6/laravel-permissions)[ Packagist](https://packagist.org/packages/coyote6/laravel-permissions)[ Docs](https://coyote6.com)[ RSS](/packages/coyote6-laravel-permissions/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (1)Versions (9)Used By (0)

Laravel Permissions
===================

[](#laravel-permissions)

A package for managing permissions and user roles.

To use
------

[](#to-use)

1. Install require coyote6/laravel-permissions
2. Optional: publish config file if you wish to change database table names.

Default below:

```
return [

	'tables' => [
		'permissions' => 'permissions',
		'roles' => 'roles',
		'role-permissions' => 'role_permissions',
		'user-roles' => 'user_roles',
		'users' => 'users'								// This needs to match your user table name
	]

];
```

```
php artisan vendor:publish --tag=permissions

```

3. Run migrations (and seed if you want the default values)

```
php artisan migrate

```

Or

```
php artisan migrate --seed

```

Or

```
db::seed --class=Coyote6\LaravelPermissions\Resources\Databases\Seeders\PermissionsSeeder

```

4. Add traits to the User model.

```
use Coyote6\LaravelPermissions\Traits\HasRoles;
use Coyote6\LaravelPermissions\Traits\UserRoles;

class User extends Authenticatable {

	// Add the traits.
	use HasRoles,
		UserRoles;

}
```

5. Add traits to all needed policies.

```
use Coyote6\LaravelPermissions\Traits\AdminAccessTrait;

class ExamplePolicy {
	use AdminAccessTrait;
}
```

6. Add admin role to your admin user(s) via code:

```
$u = User::find(1);
$u->addRole('administrator');
```

(Working on a better method)

7. Once your admin user has the administrator role you and if using the coyote6/laravel-crud package, go to the '/admin/users' on your site and manage any additional roles from there.
8. To create permissions and roles, and add permissions to roles you can use the following code example:

```
use Coyote6\LaravelPermissions\Models\Permissions;
use Coyote6\LaravelPermissions\Models\Models;

$permission = Permission::create (['name' => 'Name of Permission']); // A machine name id will automatically be created.
$role = Role::create (['name' => 'Name of Permission']); // A machine name id will automatically be created.

$role->addPermission($permission);
```

Or if using the coyote6/laravel-crud package, go to the a '/admin/users/permissions' and '/admin/users/roles' respectively on the site.

9. Optional: Add permission code to policies and/or use Policy Traits (see Policy Traits below):

```
class ExamplePolicy {

	use AdminAccessTrait,
		HandlesAuthorization;

	public function view (User $user) {

		//
		// Example using the machine name id for the permission
		//
		if ($user->hasPermissionTo ('do_something')) {
			return true;
		}
		return false;
	}

	public function create (User $user) {

		//
		// Example using the name for the permission
		//
		if ($user->hasPermissionTo ('Create Example', 'name')) {
			return true;
		}
		return false;
	}
}
```

10. Optional: Add permission code as needed to templates via blade directives.

Blade Directives
----------------

[](#blade-directives)

### User Can - Permission Id(s)

[](#user-can---permission-ids)

```

	@usercan ('do_something')
		Show if user can do something
	@endusercan

```

```

	@usercan ('do_something|do_something_else')
		Show if user can do something or do something else
	@endusercan

```

### User Is - User Id(s)

[](#user-is---user-ids)

```

	@useris (1)
		Show if user is user - 1
	@enduseris

```

```

	@useris ('1|2|3')
		Show if user is user - 1, 2, or 3
	@enduseris

```

### User Is and Can - User Id(s) and Permission Id(s)

[](#user-is-and-can---user-ids-and-permission-ids)

```

	@userisandcan (1, 'do_something')
		Show if user is user - 1
	@enduserisandcan

```

```

	@userisandcan ('1|2|3', 'do_something|do_something_else')
		Show if user is user - 1,2, or 3 and can do something or can do something else
	@enduserisandcan

```

### Author Or Can - User Id(s) and Has Permission or Has Other Permission

[](#author-or-can---user-ids-and-has-permission-or-has-other-permission)

```

	@authororcan (1, 'do_something', 'do_something_else')
		Show if user is user - 1 and can do something or the user is any user who can do something else
	@endauthororcan

```

```

	@authororcan ('1|2|3', 'do_something|do_something_else', 'do_some_other_thing_1|do_some_other_thing_2')
		Show if user is user - 1,2, or 3 and can do something or can do something else or if the user is any user who can do some other thing 1 or 2.
	@endauthororcan

```

Policy Traits
-------------

[](#policy-traits)

You can now use the StandardPolicy, StandardAuthorPolicy, StandardClientPolicy, and StandardAuthorOrClientPolicy to make short work out of writing policies for your models.

The policy traits follow a standard method for using permission names to check a user's permissions. The permission names for the StandardPolicy should follow this convention with the \[PLURAL\_MODEL\_NAME\] being replaced by lower cased plural form of your model. administer\_\[PLURAL\_MODEL\_NAME\] create\_\[PLURAL\_MODEL\_NAME\] update\_\[PLURAL\_MODEL\_NAME\] delete\_\[PLURAL\_MODEL\_NAME\] view\_\[PLURAL\_MODEL\_NAME\] search\_\[PLURAL\_MODEL\_NAME\]

The StandardAuthorPolicy expands on the StandardPolicy and allows the user to always access their own model permissions.

The StandardClientPolicy expands on the StandardPolicy but also adds the following permissions and checks if the user's client id matches that of the model's: administer\_client\_\[PLURAL\_MODEL\_NAME\] update\_client\_\[PLURAL\_MODEL\_NAME\] delete\_client\_\[PLURAL\_MODEL\_NAME\] view\_client\_\[PLURAL\_MODEL\_NAME\] search\_client\_\[PLURAL\_MODEL\_NAME\]

The StandardAuthorOrClientPolicy combines the StandardAuthorPolicy with the StandardClient Policy allowing a user permission to access the model if they are the owner or it is owned by their client.

### Using Policy Traits

[](#using-policy-traits)

To use these Policy traits, you attach the trait to your policy and write the name of the permission in the $modelPermissionName property. This will be the same \[PLURAL\_MODEL\_NAME\] you used when writing your permissions.

```
