timedoor/laravel-role-js 问题修复 & 功能扩展

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

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

timedoor/laravel-role-js

最新稳定版本:v0.1.2

Composer 安装命令:

composer require timedoor/laravel-role-js

包简介

Import role & permission in Laravel into Javascript.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Import roles & permissions data from Laravel into Javascript.

Installation

You can install the package via composer:

composer require timedoor/laravel-role-js

Don't forget to install jeremykenedy/laravel-roles package to use default setting.

You can publish the config file with:

php artisan vendor:publish --tag="laravel-role-js-config"

This is the contents of the published config file:

return [
    'generator' => timedoor\RoleJs\Generator\JeremyKenedyRoleGenerator::class,
];

Initialization

First, publish the JavaScript files that contain logic for roles and permissions. The files will publish to resources/js/roles path.

php artisan role-js:publish

if you want to change the publish path, spesify the path in the command.

php artisan role-js:publish your/publish/path/roles

Second, run the command to generate the JavaScript file contains roles & permissions data. It will generate file data.ts in the publish path.

php artisan role-js:generate

You can also spesify the publish path in the command.

php artisan role-js:generate your/publish/path/roles

Usage

You can use the generated file in your JavaScript code.

import { HasRolePermission, useRoles } from 'resources/js/roles';

// Example user data with one role
const admin: HasRolePermission = {
    roles: 'admin',
};

const { hasRole, hasPermission } = useRoles(admin);

// Check if user has the given role
if (hasRole('admin')) {
    // Do something
}

// check if user has one of the given roles
if (hasRole(['admin', 'editor'])) {
    // Do something
}

// check if user has a permission
if (hasPermission('edit.users')) {
    // Do something
}

// check if user has one of the given permissions
if (hasPermission(['view.users', 'edit.users'])) {
    // Do something
}

// check if user has all of the given permissions
if (hasPermission(['view.users', 'edit.users'], true)) {
    // Do something
}

Custom Generator

You can create your own generator by implementing timedoor\RoleJs\Generator\GeneratorInterface interface.

<?php

namespace App\RoleJs;

use timedoor\RoleJs\Generator\GeneratorInterface;

class CustomRoleGenerator implements GeneratorInterface
{
    /**
     * @return \Illuminate\Support\Collection<int, string>
     */
    public function getRoles()
    {
        return collect(['admin', 'editor']);
    }

    /**
     * @return \Illuminate\Support\Collection<int, string>
     */
    public function getPermissions()
    {
        return collect(['view.users', 'edit.users']);
    }

    /**
     * @return \Illuminate\Support\Collection<string, string[]>
     */
    public function getRolePermissions()
    {
        return collect([
            'admin' => ['view.users', 'edit.users'],
            'editor' => ['view.users'],
        ]);
    }
}

Then, change the generator class in the config file.

return [
    'generator' => App\RoleJs\CustomRoleGenerator::class,
];

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-24