Download the PHP package httpoz/roles without Composer
On this page you can find all versions of the php package httpoz/roles. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package roles
Short Description Powerful package for handling roles and permissions in Laravel
License MIT
Homepage https://github.com/httpoz/roles
Informations about the package roles
Roles for Laravel
Simple package for handling roles in Laravel.
Installation
This package is very easy to set up. There are only a couple of steps.
Previous Versions
For Previous versions please see the releases page
Composer
Add the package to your project via composer.
Config File And Migrations
To publish the package config's file and migrations to your application. Run this command inside your terminal.
Enable HasRole Trait And Contract
Include HasRole
trait and also implement HasRole
contract inside your User model.
And that's it!
Usage
Creating Roles
Because of
Slugable
trait, if you make a mistake and for example leave a space in slug parameter, it'll be replaced with a dot automatically, because ofstr_slug
function.
Attaching And Detaching Roles
It's really simple. You fetch a user from database and call attachRole
method. There is BelongsToMany
relationship between User
and Role
model.
Syncing Roles
You may also use the sync method to attach roles to a user model. Any roles that are not passed into the method will be detached from the user's roles. So, after this operation is complete, only the roles passed into the method will be attached to the user:
Checking For Roles
You can now check if the user has required role.
And of course, there is a way to check for multiple roles:
In this case, a user has to have at least one of the given roles. Multiple options have been illustrated below that achieve the same goal.
In this case, a user has to have all the given roles. Multiple options have been illustrated below that achieve the same goal.
Find users by role
There are multiple ways to get a list of users by their given role.
Using the role's id
Using the role's slug
Using the role's group
If you use soft delete on your Users model, and want to include deleted users, you can use
usersWithTrashed
method instead ofusers
.
Groups
If a user has multiple roles, method
group
returns the first one in alphabetical order (a better implementation of this will be explored).
Group
is intended to collectively organise and assign permissions (Laravel's built-in authorization feature) to a role group that can be shared by multiple roles (examples and implementation to be added to documentation in future).
Blade Extensions
There are two Blade extensions. Basically, it is replacement for classic if statements.
Middleware
This package comes with VerifyRole
and VerifyGroup
middleware. You must add them inside your app/Http/Kernel.php
file.
Now you can easily protect your routes.
It throws \HttpOz\Roles\Exceptions\RoleDeniedException
or \HttpOz\Roles\Exceptions\GroupDeniedException
exceptions if it goes wrong.
You can catch these exceptions inside app/Exceptions/Handler.php
file and do whatever you want. You can control the error page that your application users see when they try to open a page their role is not allowed to. This package already has a view bundled with it that should have been published to resources/views/vendor/roles/error.blade.php
when you published the package. Simply add the below condition inside your app\Exceptions\Handler.php
's render function. Feel free to point to another view of your choice.
Config File
You can change connection for models, slug separator, models path and there is also a handy pretend feature. Have a look at config file for more information.
Caching
The configuration for cache expiry is defaulted to 2 weeks (in seconds). You can update this value to suit your project specific needs.
License
This package is free software distributed under the terms of the MIT license.