Download the PHP package isakzhanov-r/laravel-permissions without Composer
On this page you can find all versions of the php package isakzhanov-r/laravel-permissions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download isakzhanov-r/laravel-permissions
More information about isakzhanov-r/laravel-permissions
Files in isakzhanov-r/laravel-permissions
Package laravel-permissions
Short Description Package for using user roles and permissions in project laravel
License MIT
Informations about the package laravel-permissions
Laravel Permissions
Basic roles and permissions is a succinct and flexible way to add Role-based Permissions
Contents
- Installation
-
Configuration
- Role model
- Permission model
- Permissible model
- Usage
- Creating
- User model
- Attach, detach and sync permissions
- Attach permissions
- Detach permissions
- Syncing permissions
- Checking for permissions
- Middleware
- Artisan commands
- License
Installation
To get the latest version of Laravel Permissions package, simply require the project using Composer:
Instead, you can, of course, manually update the dependency block require
in composer.json
and run composer update
if you want to:
If you don't use auto-discovery, add the ServiceProvider
to the providers array in config/app.php
:
Copy the package config to your local config with the publish command:
You can create the DB tables by running the migrations:
This command will create such roles
, permissions
, user_roles
and permissible
tables.
But you can also use the command for automatic installation
This command will publish the configuration file. Run the migration command, generate models for tables, and change the User model . all configuration of the package will happen automatically. after executing this command, you don't need to configure anything.
Configuration
To further customize table names and model namespaces, edit the config/laravel_permissions.php
after you run command
before you run the command php artisan migrate
four new tables will be present:
-
tables
users
- the default table for the relationship to the userroles
- stores role recordspermissions
- stores permission recordsuser_roles
- stores many-to-many relations between roles and userspermissible
- stores many-to-manypolymorphic relations between another essences (User,Role, ...) and permissions
foreign_key
- the key relationships in the tablesmodels
- references to model classes
Models
Role model
-
Create a Role model, using the following example:
-
After creating model set the model reference in file
config/laravel_permissions
- Or use command
php artisan laravel-permissions:migrate
to generate this automaticaly
The Role
model has three main attributes:
slug
— Unique name for the Role, used for looking up role information in the application layer. For example: "admin", "owner", "manager".title
— Human readable name for the Role. Not necessarily unique and optional. For example: "User Administrator", "Project Owner", "Project Manager".description
— A more detailed explanation of what the Role does. Also optional. And description are optional; its field is nullable in the database.
Base model IsakzhanovR\Permissions\Models\Role
use trait HasPermissions
Permission model
-
Create a Permission model, using the following example:
-
After creating model set the model reference in file
config/laravel_permissions
- Or use command
php artisan laravel-permissions:migrate
to generate this automaticaly
The Permission
model has the same three attributes as the Role:
slug
— Unique name for the Permission, used for looking up role information in the application layer. For example: "create-user", "update-user", "delete-user".title
— Human readable name for the Permission. Not necessarily unique and optional. For example: "Create User", "Update User", "Delete User".description
— A more detailed explanation of what the Permission does.
if you look at the default model IsakzhanovR\Permissions\Models\Permission
, you will
see morphedByMany relationships for users and roles. You can extend this list with your
own entities.
Example :
And PostType
model must use trait HasPermissions
Permissible model
This is the standard polymorph pivot model The Permissible
model has the same three attributes:
permission_id
— Foreign key for the Permission.permissible_type
— Belong to model (Class name).permissible_id
— Belong to model (key).
You also need to create a model Permissible
and inherit from IsakzhanovR\Permissions\Models\Permissible
After creating model you also need to append link in config file
Or use the command to automatically install the package and generate models and a configuration file
Usage
Creating
Let's start by creating the following Roles
and Permissions
:
Now we just need to creat permissions: We will create permissions without a description, and slug is created from title
User model
First, use the IsakzhanovR\Permissions\Traits\HasPermissions
and IsakzhanovR\Permissions\Traits\HasRoles
traits to your User
model.
If you use the command php artisan laravel-permissions:migrate
, these changes will occur automatically and your User
model will look like this:
Attach, detach and sync permissions
This package allows users to be associated with permissions and roles. Each role is associated with several permissions. in this case, the User
can have a separate Permission
that is not included in the list of permissions for the role.
Role
and Permission
are the usual Eloquent models.
Attach permissions
So, when the roles are created let's assign them to the users. Thanks to the HasRoles
trait this is as easy as:
The method $user->attachRole()
accepts an argument that can be an id
, slug
, or instance of Role
the model
Let's start adding permissions to roles and users:
So we added permissions for the admin and manager roles and also gave personal permissions for the user.
All entities that use trait HasPermission
have access to permission relationships and the following methods for adding permissions:
Detach permissions
To revoke roles and permissions, use the following methods:
Syncing permissions
To synchronization roles and permissions, use the following methods:
Checking for permissions
Now we can check for roles and permissions simply by doing:
You can have as many roles as you want for each user, and Vice versa.
We can check permissions for entities using the following method:
You can also use placeholders (wildcards) to check any matching permission by doing:
Middleware
You can use a middleware to filter routes and route groups by permission or role. Add middlewares in $routeMiddleware
of app/Http/Kernel.php
file:
You can use a middleware to filter routes and route groups by permission or role
If you need to check whether there are matches in permissions use ability
Artisan commands
You can create a role or a permission from a console with artisan commands:
You can also invoke the creation of roles and permissions from your application:
Artisan::call('laravel-permissions:create-role', ['name' => $name]); Artisan::call('laravel-permissions:create-permission', ['name' => $name]);
License
This package was written under Andrey Helldar supervision under the MIT License.
All versions of laravel-permissions with dependencies
illuminate/auth Version ^5.5|^6.0|^7.0|^8.0
illuminate/cache Version ^5.5|^6.0|^7.0|^8.0
illuminate/console Version ^5.5|^6.0|^7.0|^8.0
illuminate/database Version ^5.5|^6.0|^7.0|^8.0
illuminate/http Version ^5.5|^6.0|^7.0|^8.0
illuminate/support Version ^5.5|^6.0|^7.0|^8.0
symfony/http-kernel Version ^3.3|^4.0|^5.0
symfony/finder Version ^5.0