codinglabsau/laravel-roles 问题修复 & 功能扩展

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

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

codinglabsau/laravel-roles

Composer 安装命令:

composer require codinglabsau/laravel-roles

包简介

Simple Laravel roles

关键字:

README 文档

README

Latest Version on Packagist Test Total Downloads

A simple, flexible roles implementation for Laravel.

See v2.3 for Laravel 6-9 support.

Installation

Install with composer

$ composer require codinglabsau/laravel-roles

Publish migrations and migrate

php artisan vendor:publish --tag="roles-migrations"
php artisan migrate

Configuration

If you need to override the default Role model, you can do that by publishing the config and setting the models.role option.

php artisan vendor:publish --tag="roles-config"

Usage

Add the trait

Add the HasRoles trait to your user model:

use Codinglabs\Roles\HasRoles;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, HasRoles;
}

Create roles

$role = \Codinglabs\Roles\Role::create(['name' => 'manager']);

Get roles

$managerRole = \Codinglabs\Roles\Role::whereName('manager')->first();

Associate roles

Under the hood we are using Eloquent many-to-many relationships.

use Codinglabs\Roles\Role;

// attach multiple roles
$user->roles()->attach([
    Role::whereName('employee')->first()->id,
    Role::whereName('manager')->first()->id,
]);

// detach a single role
$user->roles()->detach(Role::whereName('employee')->first());

// update roles to match array
$user->roles()->sync([
    Role::whereName('employee')->first()->id,
]);

// ensure roles in array are attached without detaching others
$user->roles()->syncWithoutDetaching([
    Role::whereName('employee')->first()->id,
]);

Protect routes with middleware

In App\Http\Kernel, register the middeware:

protected $routeMiddleware = [
    // ...
    'role' => \Codinglabs\Roles\CheckRole::class,
];

And then call the middleware in your routes, seperating multiple roles with a pipe:

Route::middleware('role:employee')->...
Route::middleware('role:manager|admin')->...

Or with a gate:

class UserController extends Controller
{
    public function destroy()
    {
        $this->authorize('role', 'admin');
    }
}

Or in the constructor of a controller:

class ManagerDashboardController extends Controller
{
    public function __construct()
    {
        $this->middleware('role:manager');
    }
}

If the middleware check fails, a 403 response will be returned.

Check roles on a user

Call hasRole on the user model:

// check a single role
$user->hasRole('foo');

// check whether any role exists
$user->hasRole(['bar', 'baz']);

// get all roles
$user->roles;

Conditionally showing content with the blade directive

@role('admin')
<div>Super secret admin stuff goes here...</div>
@endrole

Sharing roles with UI (Inertiajs example)

// AppServiceProvider.php
Inertia::share([
    'auth' => function () {
        return [
            'user' => Auth::user() ? [
                'id' => Auth::user()->id,
                'roles' => Auth::user()->roles->pluck('name'),
            ] : null
        ];
    }
]);
// app.js
Vue.mixin({
  methods: {
    hasRole: function(role) {
      return this.$page.auth.user.roles.includes(role)
    }
  }
})
<!-- SomeComponent.vue -->
<div v-if="hasRole('manager')">I am a manager</div>

Upgrading from v1 to v2

Please see upgrading from v1 to v2 for details and instructions to avoid any issues after upgrading to v2.

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, create an issue on GitHub.

Credits

License

MIT. Please see the license file for more information.

About Coding Labs

Coding Labs is a web app development agency based on the Gold Coast, Australia. See our open source projects on our website.

codinglabsau/laravel-roles 适用场景与选型建议

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

它主要适用于以下技术方向: 「laravel」 「roles」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 90.21k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 15
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-19