Download the PHP package alleyinteractive/simple-laravel-roles without Composer
On this page you can find all versions of the php package alleyinteractive/simple-laravel-roles. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alleyinteractive/simple-laravel-roles
More information about alleyinteractive/simple-laravel-roles
Files in alleyinteractive/simple-laravel-roles
Package simple-laravel-roles
Short Description Simple PHP-based permissions in Laravel
License MIT
Homepage https://github.com/alleyinteractive/simple-laravel-roles
Informations about the package simple-laravel-roles
Simple Laravel Roles
This package adds very simple role/capability functionality to a Laravel application, where the roles and capabilities are defined using a config file.
Once roles and capabilities are defined and a role is assigned to a user, capabilities can be checked using Laravel's built-in authorization functionality:
Installation
Via Composer
Setup and Configuration
-
Once the package is installed, you can publish the package files using artisan:
This will add two files to your application:
config/roles.php
is where you'll define your roles.database/migrations/<date>_add_roles_to_users_table.php
adds aroles
column to theusers
table. Delete this file if it doesn't apply to your use case.
- Configure your roles and capabilities in
config/roles.php
. - Run
php artisan migrate
if you just need to addroles
to theusers
table. If you want to addroles
to more tables, create your migrations first. - Add the
Alley\SimpleRoles\HasRoles
trait to theUser
model and/or any models that will have roles.
Usage
In Gates and Policies
This package is intended to augment Laravel's built-in authorization functionality by adding role and capability buckets in which to add users or other models. These buckets can be checked during your Gate or Policy checks.
Here's an example Policy method that leverages capabilities to allow deleting a post if the user owns the post or the user is allowed to delete others' posts:
In place of Gates and Policies
Depending on the complexity of your application, this package could even replace
your gate and policy checks. It will intercept all Gate checks before other authorization checks
and if the user has the given ability as a capability in any of their roles, the
check will pass. With this, Laravel's core authorization functionality will work
as usual, and capabilities will be checked instead of defined gates or policies.
In other words, if a user has a role with the delete_posts
capability, that
capability can be checked using:
As well as in blade templates:
The HasRoles
trait also includes some helpers to check capabilities and roles:
hasCapability(string $capability): bool
: Check if the object has the given capability.hasRole(string $role): bool
: Check if the object has the given role.getRoles(): Collection
: Get the roles for the object as Role objects.setRoles(array $roles): bool
: Set the object's roles.addRole(string $role): bool
: Append a role to the object.removeRole(string $role): bool
: Remove a role from the object.
Further, the package provides a Role
class which can be used to check
capabilities on a specific role.
Change log
See the changelog.
Testing
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email the author email (found in composer.json
) instead of using the issue tracker.
Credits
- [Matthew Boynes][@mboynes]
License
MIT. Please see the license file for more information.