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-protector

v0.1.5

Published

An attribute level access control module. Protects resources as one would expect from regular ACL, but allows for configuration to protect attributes of a given resource.

Downloads

20

Readme

mongoose-protector

mongoose-protector is a mongoose plugin that provides attribute level access control on your mongoose models. Protector was designed to work with your existing access control, it only will protect your model, not your routes.

Installation

npm install mongoose-protector

Updates

September 24, 2014:

Just patched a bug that was causing the rules to get fudged up.

Just added, but still testing and adding functionality (June 20, 2014):

Document#save(fn)

You can now prevent or allow users from creating or updating a document using Document#save()

#####note the properties and where keys are not enabled for this function at the moment.

####Usage

var myPerson = new Person({FirstName: "Bill", LastName: "Baggins"});

var userRole = 'admin';
myPerson.protect(userRole).save(function(err, person, numberAffected) {

});

At the moment if you don't use the callback, then an error will be thrown.

Usage Quick Overview

// /model/Person.js
PersonSchema = new Schema({
    FirstName: String,
    LastName: String,
    Birthdate: Date,
    TelephoneNumber: Number,
    Address: String
});

PersonSchema.plugin(require('mongoose-protector'));

// Set rules on the model
mongoose.model('Person', PersonSchema)
    .setRules([
        {
            role: {
                name: 'admin',
                allow: {

                    "*": "*"

                }
            }
        
        },
        {
            role: {
                name: 'guest',
                allow: {
                    read: {

                        properties: {
                            FirstName: 1
                        },
                        where: {

                            FirstName: "$dynamic.firstName"

                        }

                    }
                }

            }
        
        }
    
    
    ]);
// Controller/Person.js

var Person = mongoose.model('Person');

//example
var role = 'guest';

Person.protect(role, {firstName: 'John'}).find({}, function(err, persons) {

});

Currently Supported Mongoose Functions

  • Model#find()
  • Model#findOne()
  • Document#save()

Rules

Your rules define what can be done with a model and who can act on the model. You attach an array of rules to your model:

mongoose.model('Person', PersonSchema).setRules([])

A rule is an object of the following form (note, the object will be extended in the future beyond just the role top level key):

{
    role: {
        name: 'admin'
        allow: {
            create: {

            },
            read: {
                properties: {

                },
                where: {

                }

            },
            update: {

            },
            delete: {

            }

        }
    
    }

}

##Role ####.name A string of your choice, used to identify your role. ####.allow An object that contains your CRUD info for the role

##Allow ####.create ####.read ####.update ####.delete

If you want your role to have the ability to perform CRUD operations, you'll need to include one or more of the above properties.

####.properties An object that basically is your attribute level access control. These are the fields you want returned to that particular user. ####.where Where works exactly like a query in Mongodb. So, if you want your role to only fetch certain documents, you can specify that in where, using any standard mongoDB query.

#Usage

Person.protect(role).find({}, function(err, persons) {

});

$dynamic

$dynamic is a keyword of this plugin, and it allows you to dynamically define "where" criteria. In a simple case, you could imagine, that you have a use case where you only want users fetch documents that belong to them, how you do that is up to you, but in this example, we'll just demonstrate using the user's name:

{
            role: {
                name: 'guest',
                allow: {
                    read: {
                        properties: {
                            '*': '*' // allows all fields to be visible
                        },
                        where: {
                            // this is dynamic
                            name: "$dynamic.name"
                        }
                    }
                }
            }

        }

Above you can see that we have a dynamic query defined

name: "$dynamic.name"

This allows us to pass in a value at runtime. How? Like so:

Person.protect(role, {name: 'John'}).find({}, function(err, persons) {

});

License