geowrgetudor / laravel-spatie-permissions-vue
Use Laravel Spatie Permission package in Vue
Installs: 5 738
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 3
Forks: 24
Open Issues: 0
Language:JavaScript
Type:plugin
Requires
- spatie/laravel-permission: >=5.4.0
This package is auto-updated.
Last update: 2024-04-13 18:08:03 UTC
README
This package is fork of the original package (ahmedsaoud31/laravel-permission-to-vuejs) and requires laravel-permission by Spatie.
It supports SSR.
Installation
composer require geowrgetudor/laravel-spatie-permissions-vue
Setup
Add the trait to your User
model:
use SpatiePermissionVue\Traits\RolesPermissionsToVue; class User extends Authenticatable { // ... use RolesPermissionsToVue; }
Import and use laravel-spatie-permissions-vue
plugin into your app.js
file:
import RolesPermissionsToVue from "../../vendor/geowrgetudor/laravel-spatie-permissions-vue/src/js"; // ... app.use(RolesPermissionsToVue);
Pass the Spatie roles & permissions to a global var called vueSpatiePermissions
in your app.blade.php
or whatever your main blade template is called:
<script type="text/javascript"> window.vueSpatiePermissions = {!! auth()->check() ? auth()->user()->getRolesPermissionsAsJson() : 0 !!} </script>
Usage
You can make use of can
and is
global functions to check for permissions and roles of the current user.
<div v-if="can('edit post')"> <!-- Edit post form --> </div> <div v-if="is('super-admin')"> <!-- Show admin tools --> </div> <!-- you can use OR operator --> <div v-if="can('edit post | delete post | publish post')"> <!-- Do something --> </div> <div v-if="is('editor | tester | user')"> <!-- Do something --> </div> <!-- you can use AND operator --> <div v-if="can('edit post & delete post & publish post')"> <!-- Do something --> </div> <div v-if="is('editor & tester & user')"> <!-- Do something --> </div>
License
The MIT License (MIT).