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

@verful/permissions

v1.0.0

Published

Easily manage user roles and permissions

Downloads

440

Readme

npm-image license-image typescript-image

Pre-requisites

The @verful/permissions package requires @adonisjs/core >= 5 and @adonisjs/lucid >= 16

Setup

Install the package from the npm registry as follows.

npm i @verful/permissions
# or
yarn add @verful/permissions

Next, configure the package by running the following ace command.

node ace configure @verful/permissions

Getting started

Once the package is installed the first thing you wan't to do is apply the Authorizable mixin from @ioc:Verful/Permissions/Mixins into a model

import { Authorizable } from '@ioc:Verful/Permissions/Mixins'
import { compose } from '@ioc:Adonis/Core/Helpers'
import { BaseModel } from '@ioc:Adonis/Lucid/Orm'

const config = { 
  permissionsPivotTable: 'user_has_permissions',
  rolesPivotTable: 'user_has_roles'
}

export default class User extends compose(BaseModel, Authorizable(config)) {}

After the mixin is applied you can do stuff like this

await user.givePermissionTo('view-users')
await user.assignRole('admin')

await role.givePermissionTo('edit-users')

Basic usage

The package allows you to associate Lucid models with roles and permissions. Roles and Permissions are just Lucid models that can be directly managed like any other model

import Permission from '@ioc:Verful/Permissions/Permission'
import Role from '@ioc:Verful/Permissions/Role'

const role = await Role.create({ name: 'writer' })
const permission = await Permission.create({ name: 'edit-posts' })

Managing permissions

You can manage permissions for roles and models using the same methods

// Assigning permissions
await role.givePermissionTo('do-things')

// Removing permissions
await user.revokePermissionTo('do-things')

// Synchronize permissions
await role.syncPermissions('do-things', 'try-things')

Checking for permissions

// Checking permissions
await role.hasPermissionTo('do-things') // returns true or false
await user.checkPermissionTo('do-things') // returns true or throws

// Returns true if the model has any of the given permissions
await role.hasAnyPermission('do-things', 'try-things') 

// Returns true if the model has all of the given permissions
await user.hasAllPermissions('do-things', 'try-things')

// Returns all permission names
await user.getPermissionNames()

Managing Roles

You can manage roles for models using the Authorizable mixin

// Assign role
await user.assignRole('admin')

// Revoke role
await user.revokeRole('admin')

// Synchronize roles
await user.syncRoles('admin', 'writer', role)

Checking for roles

Generally you should be checking against permissions vs checking for roles, but if you want to check against a role instead use one of the following methods

await user.hasRole('admin')

// Returns true if the model has any of the given permissions
await role.hasAnyRoles('admin', 'writer') 

// Returns true if the model has all of the given permissions
await user.hasAllRoles('admin', 'writer')

// Returns all role names
await user.getRoleNames()

Accessing direct and role permissions

// Check if the model has the permission directly
await user.hasDirectPermission('do-things')

// Check if the model has the permission via role
await user.hasPermissionViaRole('do-things')

// Get all direct permissions
await user.getDirectPermissions()

// Get all permissions via roles
await user.getPermissionsViaRoles()

// Get all permissions combined
await user.getAllPermissions()