amir/laravel-permission 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

amir/laravel-permission

Composer 安装命令:

composer require amir/laravel-permission

包简介

an easy and flexible way for permissions management

README 文档

README

An easy and flexible Laravel authorization and roles permission management

Why we need a Laravel permission package

In many projects, we need to implement a role-based permissions management system for our users. Although Laravel has the best tools to manage users' permissions, I build this package to make it simpler, more flexible, and to avoid duplicate work in many web projects built on Laravel.

How to use

The idea is to use this package as easy and as flexible as possible.
This package creates a list of all your routes and assigns these permissions list to user roles.
Although the laravel-permission package does most of the work, you could easily extend it and implement your authorization system.

Installation

Start with installing the package through the composer. Run this command in your terminal:

$ composer require amir/laravel-permission

After that, you need to run the migration files:

$ php artisan migrate

How to authorize user

This package adds a role_id to the users table. Roles are stored in the roles table. You can assign a role to a user in your administrator panel or by creating a seed file.
Then, you only need to assign auth.role middleware to your routes.

Assign a route to a role

Besides middleware and other route settings, you can use a role key in your route groups to assign a role to your routes.
You can put your routes for a role in a Route group like this:

Route::group([
    'middleware' => 'auth.role',
    'prefix' => ...,
    'role' => ['admin', 'customer'],
    ...
],function (){
    ...
    Route::get('/home', 'HomeController@index')->name('home');
    Route::get('/product', 'ProductController@index');
    ...
});

Of course, you can have as many as route groups like this.
Then you need to run this artisan command to register all permissions:

$ php artisan permissions:generate 

This command will register all permissions and assign permissions to the roles.
If you add a fresh option to this command, it will delete all data and generate fresh permissions data:

$ php artisan permissions:generate --fresh

Now only users with the proper role can access the route assigned to them.
Don't forget that this package does not handle assigning roles to the users. You need to handle this in your administration panel or anywhere else you handle your users.
Again, if you want to add permissions to the routes manually it is not necessary to add `role` key in your route group. You can easily assign permissions to the roles in your administration panel or create another seed file for that.

How to create roles

The php artisan permissions:generate command will make all roles defined in the routes if they are not exist.
Also, You can create a seeder to fill the roles table. It takes only a name field.
Your RolesSeeser file can look like this.

Role::firstOrCreate(['name' => 'admin']);
Role::firstOrCreate(['name' => 'customer']);

Don't forget to import the Role model in your seeder.

use Amir\Permission\Models\Role;

How to clear permissions

To clear registered permissions you can run this command:

$ php artisan permissions:clear

You can use this command to clear all permissions data for a specific role

$ php artisan permissions:clear --roles role1 role2

To erase only permissions list, run permissions:clear command with this option:

$ php artisan permissions:clear --tables permissions

To clear all roles:

$ php artisan permissions:clear --tables roles

To clear only permissions role relation:

$ php artisan permissions:clear --tables permission_role

This command erases all permissions assigned to roles, so you can regenerate permissions

Also, you can use these options in combination:

$ php artisan permissions:clear --roles admin --tables permission_role

About

I used this Laravel permission management method in my projects for a while. It made manging Laravel permission easy and flexible for me. I hope it helps you as well. All pull requests are welcome.
Most of the work is based on my article about Laravel authorization and roles permission management

amir/laravel-permission 适用场景与选型建议

amir/laravel-permission 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.53k 次下载、GitHub Stars 达 47, 最近一次更新时间为 2020 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 amir/laravel-permission 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 amir/laravel-permission 我们能提供哪些服务?
定制开发 / 二次开发

基于 amir/laravel-permission 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.53k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 47
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 47
  • Watchers: 4
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-18