npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

authorization-for-laravel-inertia-vue

v1.1.1

Published

Authorization package for roles and permissions with super roles to use in Vue.js with Inertia.js

Downloads

8

Readme

Authorization For Laravel Inertia Vue

Authorization For Laravel Inertia Vue is a js package to check for the logged in user roles and permissions.

After installed you can use it like this in Vue.js:

<!-- Check if the user has a permission -->
<div v-if="can('edit post')">
  <!-- Do something -->
</div>

<!-- Check if the user has a role -->
<div v-if="is('writer')">
  <!-- Do something -->
</div>
<!-- OR -->
<div v-if="hasRole('writer')">
  <!-- Do something -->
</div>

<!-- Check if the user has any permission in given a array -->
<div v-if="canAny(['create post', 'edit post', 'delete post'])">
  <!-- Do something -->
</div>

<!-- Check if the user has any permission using a separator -->
<div v-if="canAny('create post | edit post | delete post')">
  <!-- Do something -->
</div>

<!-- Check if the user has all the permission in a given array -->
<div v-if="canAll(['create post', 'publish post'])">
  <!-- Do something -->
</div>

<!-- Check if the user has any role in given a array -->
<div v-if="isAny(['writer', 'editor'])">
  <!-- Do something -->
</div>
<!-- OR -->
<div v-if="hasAnyRole('create post | edit post | delete post')">
  <!-- Do something -->
</div>

<!-- Check if the user has all the roles in a given array -->
<div v-if="isAll(['writer', 'editor', 'super admin'])">
  <!-- Do something -->
</div>

Installation

npm install authorization-for-laravel-inertia-vue

Configuration

First, return the roles and permissions of the logged in user and the defined superRules in the HandleInertiaRequests.php file in app\Http\Middleware\HandleInertiaRequests.php in the share method of inertia:

public function share(Request $request): array
{
    return array_merge(parent::share($request), [
        'auth' => [
            'roles' => $request->user() ? $request->user()->roles()->pluck('name') : [],
            'permissions' => $request->user() ? $request->user()->getAllPermissions()->pluck('name') : [],
        ],
        'superRoles' => ['Super Admin', 'Super Moderator'],
    ]);
}

Second, add and use the authorization-for-laravel-inertia-vue plugin in app.js file:

import AuthorizationForLaravelInertiaVue from 'authorization-for-laravel-inertia-vue';

// The use it with the createApp
return createApp({ render: () => h(App, props) })
            .use(AuthorizationForLaravelInertiaVue)
            .mount(el);

And now you are good to go 🚀.

Section Links

Permissions:

can - canNot - canAny - canNotAny - canAll - canNotAll - guest

Roles

is & hasRole - isNot & unlessRole - isAny & hasAnyRole - isNotAny & unlessAnyRole - isAll & hasAllRoles - isNotAll & unlessAllRoles - isExact & hasExactRoles - hasNoRoles - isSuper & hasSuperRole - isNotSuper & unlessSuperRole

Documentation

All the methods provided by this package can be used in the template directly or imported from the package and used in the script as following:

Check for a single permission:

  • You can use it inside the template directly like this:
<!-- Check if the user has a permission -->
<div v-if="can('edit post')">
  <!-- Do something -->
</div>
  • Or you can use it in the script like this:
import { can } from 'authorization-for-laravel-inertia-vue';

if (can('edit post')) {
    // Do something
}

Check if user does NOT has permission:

<!-- Check if the user doesn't has permission -->
<div v-if="canNot('delete post')">
  <!-- Do something -->
</div>

Check if user has ANY of the provided permissions:

<!-- Check if the user has Any permission -->
<div v-if="canAny(['edit post', 'delete post'])">
  <!-- Do something -->
</div>

<!-- OR By Using A Separator -->
<div v-if="canAny('create post | edit post | delete post')">
  <!-- Do something -->
</div>

Check if user does NOT has ANY of the provided permissions:

<!-- Check if the user doesn't has Any of those permissions -->
<div v-if="canNotAny(['edit post', 'delete post'])">
  <!-- Do something -->
</div>

Check if user has ALL of the provided permissions:

<!-- Check if the user has All of those permissions -->
<div v-if="canAll(['create post', 'edit post'])">
  <!-- Do something -->
</div>

Check if user does NOT has ALL of the provided permissions:

<!-- Check if the user does NOT has All of those permissions -->
<div v-if="canNotAll(['create post', 'edit post'])">
  <!-- Do something -->
</div>

Check if user is a GUEST and do NOT has ANY permission:

<!-- Check if the user is a guest permissions -->
<div v-if="guest()">
  <!-- Do something -->
</div>

Check for a single role:

<!-- Check if the user a role -->
<div v-if="is('writer')">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="hasRole('writer')">
  <!-- Do something -->
</div>

Check if user does NOT has role:

<!-- Check if the user doesn't has role -->
<div v-if="isNot('writer')">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="unlessRole('writer')">
  <!-- Do something -->
</div>

Check if user has ANY of the provided roles:

<!-- Check if the user has Any role -->
<div v-if="isAny(['writer', 'editor'])">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="hasAnyRole(['writer', 'editor'])">
  <!-- Do something -->
</div>

Check if user does NOT has ANY of the provided roles:

<!-- Check if the user doesn't has Any of those roles -->
<div v-if="isNotAny(['writer', 'editor'])">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="unlessAnyRole(['writer', 'editor'])">
  <!-- Do something -->
</div>

Check if user has ALL of the provided role:

<!-- Check if the user has All of those role -->
<div v-if="isAll(['writer', 'editor'])">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="hasAllRoles(['writer', 'editor'])">
  <!-- Do something -->
</div>

Check if user does NOT has ALL of the provided role:

<!-- Check if the user does NOT has All of those roles -->
<div v-if="isNotAll(['writer', 'editor'])">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="unlessAllRoles(['writer', 'editor'])">
  <!-- Do something -->
</div>

Check if user has Exactly ALL of a given list of roles:

<!-- Check if the user has exactly all of a given list of roles no more no less -->
<div v-if="isExact(['writer', 'editor'])">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="hasExactRoles(['writer', 'editor'])">
  <!-- Do something -->
</div>

Check if user has NO roles:

<!-- Check if the user has no roles -->
<div v-if="hasNoRoles()">
  <!-- Do something -->
</div>

Check if user has SUPER ROLE:

<!-- Check if the user has any super role -->
<div v-if="isSuper()">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="hasSuperRole()">
  <!-- Do something -->
</div>

Check if user has NO SUPER ROLE:

<!-- Check if the user has not any super role -->
<div v-if="isNotSuper()">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="unlessSuperRole()">
  <!-- Do something -->
</div>

License

MIT