timedoor / laravel-role-js
Import role & permission in Laravel into Javascript.
Requires
- php: ^8.0
- illuminate/contracts: ^8.0 || ^9.0 || ^10.0
- spatie/laravel-package-tools: ^1.12.1
Requires (Dev)
- jeremykenedy/laravel-roles: ^10.0
- laravel/pint: ^1.0
- nunomaduro/collision: ^5.10 || ^6.4
- nunomaduro/larastan: ^1.0 || ^2.0
- orchestra/testbench: ^6.22 || ^7.24 || ^8.0
- pestphp/pest: ^1.23
- pestphp/pest-plugin-laravel: ^1.4
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
Suggests
- jeremykenedy/laravel-roles: Required to use the RoleJs package with default generator.
This package is auto-updated.
Last update: 2024-11-09 13:48:49 UTC
README
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.