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

mongoose-rbac-plugin

v1.0.3

Published

Adds a role and access level functionality to a model

Downloads

9

Readme

mongoose-rbac-plugin

npm version Build Status

A mongoose plugin to help manage user roles and user access levels.

There are many ways to handle user account access levels. This one has a very specific methodology.

  • A Model has a single role, and it is required.
  • You can add as many roles as you want.
  • An access level has many roles.
  • Access levels are used to make sure a user has access to a given functionality.

I've thought about adding the ability to have multiple roles and add methods to check if a user has any of the given roles and all of the given roles. The single role and setting access levels suits my personal needs, but if you find it would be useful to have this functionality, sound off in issues and pull requests.

Installation

npm install --save mongoose-rbac-plugin

Usage

'use strict'

var UserSchema = new require('mongoose').Schema({
  email: String
})

UserSchema.plugin(require('mongoose-rbac-plugin'), {
  roles: ['public', 'user', 'admin'],
  accessLevels: {
    public: ['public', 'user', 'admin'],
    anon: ['public'],
    user: ['user', 'admin'],
    admin: ['admin']
  }
})

var User = mongoose.model('User', UserSchema)

var newUser = new User({ email: '[email protected]', role: 'user' })

// The string passed in is an access level
console.log(newUser.hasAccess('public')) // true
console.log(newUser.hasAccess('anon')) // false
console.log(newUser.hasAccess('user')) // true
console.log(newUser.hasAccess('admin')) // false
console.log(newUser.hasAccess(['public', 'user'])) // true
console.log(newUser.hasAccess(['public', 'anon'])) // false (because the user isn't a part of 'anon' access level)

It it required that you pass in options to the plugin. This way you can set up the roles and access levels. As mentioned, users must have one of the roles passed into the roles option. The accessLevels are an easy way to configure which types of users will have access to certain functionality of your app.

For example, you could create an express middleware that verifies a user's permissions before they can access an API endpoint.

function hasAccess(accessLevel) {
  return function(req, res, next) {
    if (req.session.user && req.session.user.hasAccess(accessLevel)) {
      return next()
    }
    return res.json({
      success: false,
      error: 'Unauthorized'
    })
  }
}

var router = require('express').Router()

router.get('/some-protected-route', [
  hasAccess('user'), // protection middleware
  function(req, res, next) {
    console.log('you have access!')
    res.json({
      secure: true,
      data: 'super secret data'
    })
  }
])

This plugin adds a role String field to a schema. It's set as an enum with the given roles values in the options. It is required, but I may be convinced to make that optional and set a default value or something.

It also adds a hasAccess() method to the model instances. It takes in a string of one of the accessLevels you defined in the options. If nothing is passed in, it will return false. Note that in v1, it used to return true. It was decided that passing in nothing was usually a case of not handling some edge data cases, such as looking up valid roles for an endpoint from a database. If a string is passed that isn't one of the access levels, it will return false.

Options

  • roles (Array of Strings) - The string representation of the user roles. Default: []
  • accessLevels (Object) - An Object hash that has access levels as the property keys, and arrays of strings as the roles that have that access level. Default: {}
  • rolePath (String) - The path that the role property will be saved to the model instance. Default: 'role'
  • rolesStaticPath (String) - The path to the exposed roles given in the options. Default: 'roles'
  • accessLevelsStaticPath (String) - The path to the exposed access levels given in the options. Default: 'accessLevels'
  • hasAccessMethod (String) - The instance method name to be used to check a user's access level. Default: 'hasAccess'