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

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

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

timedoor/laravel-route-permission

最新稳定版本:v1.0.2

Composer 安装命令:

composer require timedoor/laravel-route-permission

包简介

Extented from spatie/laravel-permission, this package help developer to authorize user permission based from URI and request method.

README 文档

README

Extended from spatie/laravel-permission version 5, this package help developer authorize user permission based on URI and request method.

Installation

Use the package manager composer to install this package.

composer require timedoor/laravel-route-permission

Publish the package in your project with:

php artisan routepermission:install

Then run the database migration.

php artisan migrate

Usage

You can use provided route middleware alias from this package named route-permission that can be configured from config/permission.php, for example:

Route::middleware(['route-permission'])->group(function () {
    //
});

Or if you using a guard you can use it like this, for example an api guard.

Route::middleware(['route-permission:api'])->group(function () {
    //
});

Your user must be logged in if you use this middleware, this middleware will automatically match logged-in user permission with the current route URI.

For creating permission for REST API you can use provided permission model by this package, for example:

use Timedoor\RoutePermission\Models\Permission;

Permission::createForRoute(['uri' => 'api/user']);

Permission::createForRoute(['uri' => 'api/user/{id}', 'method' => 'POST']);

Permission::createForRoute(['uri' => 'api/user', 'guard_name' => 'api']);

Permission::createForRoute(['uri' => 'api/user/*']);

When saving the permission this package will trim all whitespace and / character from provided uri.

You can use * as a wildcard character for route permissions.

Make sure you include the traits from Timedoor\RoutePermission for role and permission models to use the extended features for the route.

For the uri value use string provided from one of the below codes for accurate reference.

\Route::current()->uri(); // Return current route URI.

collect(\Route::getRoutes())->map(function ($route) { return $route->uri(); }); // Listing all registered route URI.

In the database, the permission name will be stored as route>>api/user>>* (if the second parameter isn't provided it will be stored as a wildcard *) or route>>api/user/{parameter}>>POST (all route parameters will be converted to parameter or you can change the behaviour from config/permission.php)

You can access it with default spatie/laravel-permission utility like:

$user->can('route>>api/user/{parameter}>>POST');

Or using provided helper function to generate formatted permission name.

$user->can(getRoutePermissionName('api/user/{id}', 'POST'));

For manipulating route permission here's the list of other extended methods:

use Timedoor\RoutePermission\Models\Permission;

Permission::createForRoute(['uri' => 'api/user']);

Permission::firstOrCreateForRoute(['uri' => 'api/user']);

Permission::findByNameForRoute('api/user/{id}', 'POST');

Permission::findOrCreateForRoute('api/user/{id}', 'POST');

// Scope a query to only include route permissions.
Permission::route()->get();

// Scope a query to not include route permissions.
Permission::withoutRoute()->get();

// You can call below methods from role instance too.
$user->givePermissionForRoute('api/user/{id}', 'POST');

$user->revokePermissionForRoute('api/user/{id}', 'POST');

$user->syncForRoute([
    ['uri' => 'api/user'],
    ['uri' => 'api/user/{id}', 'method' => 'POST'],
]);

$user->hasPermissionForRoute('api/user/{id}', 'POST');

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固