Download the PHP package xdroidteam/xtrust without Composer
On this page you can find all versions of the php package xdroidteam/xtrust. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xdroidteam/xtrust
More information about xdroidteam/xtrust
Files in xdroidteam/xtrust
Package xtrust
Short Description Role and permission based rights for laravel
License MIT
Informations about the package xtrust
Laravel permission, role based rights
Contents
- Key features
- Installation
- Migration
- Models
- Role
- Permission
- User
- Concept
- For example
- Usage
- Permission checking
- Simple checking
- Middleware
- Blade
- Role checking
- Simple checking
- Attaching, detaching
- Attach to a user
- Detach from a user
- Attach to a role
- Detach from a role
Key features
- You have roles and permissions. Permissions can be attached to a role, and roles can be attached to a single user, but also you can attach or detach permission/permissions from a specific user.
- Permissions and roles are stored in the cache, editing them automatically refreshes it. We use cache tags, so regular file or database cache drivers doesn't work, please use memcached instead.
- Inspired by Zizaco/entrust
- Please note, the package is under development, only tested with Laravel 5.2.
Installation
Add the following line to your composer.json (Laravel 5.4 and below):
"xdroidteam/xtrust": "0.1.*"
Add the following line to your composer.json (Laravel 5.5):
"xdroidteam/xtrust": "0.2.*"
Then run composer update
.
or you can run the composer require
command from your terminal:
composer require xdroidteam/xtrust
Then in your config/app.php
add to the providers
array (not needed after Laravel 5.5):
and to aliases
array (not needed after Laravel 5.5):
If you are going to use Middleware (requires Laravel 5.1 or later) you also need to add
to routeMiddleware
array in app/Http/Kernel.php
.
Migration
Deploy migration file:
You may now run it with the artisan migrate command:
Models
Role
Create a Role model inside app/models/Role.php
using the following example:
Permission
Create a Permission model inside app/models/Permission.php
using the following example:
User
Next, use the XTrustUserTrait
trait in your existing User
model. For example:
Don't forget to dump composer autoload
Concept
There are roles and permissions. You can attach many permissions to a role, and attach many roles to a user, like in Zizaco/entrust. The main difference, that you can directly attach or detach permissions to a user.
For example
You have four permissions:
- can_show
- can_create
- can_edit
- can_delete
You have two roles, with permissions:
- admin:
- can_show
- can_create
- can_edit
- can_delete
- user:
- can_show
You have two users, with roles:
- Adam Admin:
- admin
- Super User
- user
If you don't want Adam Admin, to be able to delete, you can simply detach the can_delete permission from him. The admin group can still have the can_delete permission, but Adam will not. If you want Super User to be able to edit, you can attach this permisson (can_edit) to her. The other users in the user role will still be unable to edit, except her.
Because of this logic, you can't check the user roles, only the permissions!
Example for UI:
Usage
Permission checking
Simple checking
Check one permission:
Returns true, if the authanticated user has the permission, returns false if not.
Check multiple permissions:
Returns true, if the authanticated user has all the permissions, returns false if not.
Or:
Returns true, if the authanticated user has one of the permissions, returns false if not.
You can also check within the user model:
Middleware
For multiple permission check use pipe symbol as OR operator:
To emulate AND functionality just use multiple instances of middleware For multiple permission check use pipe symbol as OR operator:
Blade
Multiple permissions:
Returns true, if the authanticated user has all the permissions, returns false if not.
Or:
Returns true, if the authanticated user has one of the permissions, returns false if not.
Role checking
Simple checking
Check one role:
Returns true, if the authanticated user has the role, returns false if not.
Check multiple roles:
Returns true, if the authanticated user has all the roles, returns false if not.
Or:
Returns true, if the authanticated user has one of the roles, returns false if not.
You can also check within the user model:
Attaching detaching
Always use the the id of the role or permission for attaching/detaching!
Attach to a user
Attach one role to a user:
Attach multiple roles to a user:
Attach one permission to a user:
Attach multiple permissions to a user:
Detach from a user
Detach one role from a user:
Detach multiple roles from a user:
Detach one permission from a user:
Detach multiple permissions from a user:
Attach to a role
Attach one permission to a role:
Attach multiple permissions to a role:
Detach from a role
Detach one permission from a role:
Detach multiple permissions from a role:
All versions of xtrust with dependencies
illuminate/auth Version ^9.0|^10.0
illuminate/support Version ^9.0|^10.0