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

gatemanjs-franko4don

v1.1.1

Published

Gateman is package for handling user authorization in node-mongo applications

Downloads

4

Readme

Gateman.js

Gatemanjs is an authorization system designed to manage roles and claims in node applications that use mongodb for data storage. It works together with mongoose to provide a fluent approach to managing roles and claims.

Installation

You can install gateman using npm package manager.

npm install gatemanjs

Usage

Before using gateman in your node application, you'll have to import the gateman package and setup the gateman class by passing in a valid mongoose connection object

var mongoose = require('mongoose');
var gateman = require("gatemanjs").GateMan(mongoose);

Creating roles & claims

You have to create a role before using it in your application, Gateman provides an easy way of doing that.

//Syntax
gateman.createRole(roleName);

//Example
let role = await gateman.createRole("rolename");

Creating claims is similar to creating roles

//Syntax
gateman.createClaim(claimName);

//Example
let role = await gateman.createRole("claimname");
Note: To get a collection of existing roles, you can use
//Syntax
gateman.getRoles(callback);

//Example
let roles = await gateman.getRoles();

Allowing members of a role to perform a claim

Adding claims to roles is made extremely easy. You do not have to create a claim in advance. Simply pass the name of the claim, and Gateman will create it if it doesn't exist.

gateman.allow('role').to('claim'); //for an existing role

You can also assign a claim to a role immediately after creating it

let role = await gateman.createRole("admin");
await gateman.allow("admin").to("delete");

//this provides every member of the admin role the claim to delete

Disallowing members of a role from performing a claim

Retracting claims from a role is very easy, you just need the rolename and claimname

await gateman.disallow('role').from('claim');

//Gateman does nothing if the role doesn't possess the claim

Checking for Role claims

Checking if a Claim has been assigned to a Role can be done this way

let result = await gateman.role('rolename').can('claimname');
//result is true if the claim has been assigned, else it will be false

Using gateman with user models

It is important to set up your User model to extend the HasRolesAndClaims class from the gateman package.

const mongoose = require('mongoose');
const hasRolesAndClaims = require('gatemanjs').hasRolesAndClaims(mongoose);

var UserSchema =  mongoose.Schema({
    name: String,
    email: String
});

UserSchema.loadClass(hasRolesAndClaims);
module.exports = mongoose.model('User', UserSchema)

After setting up your user model, you can call gateman methods on your mongoose user model.

Allowing users to perform a claim

//Example

 let user = await UserModel.findOne({name: "chioma"});
 await user.allow("claim");

/*
The Gateman hasRolesAndClaims class is loaded into a valid mongoose model which means that the methods are only accessible to valid user objects.
*/

//Disallowing a user from performing a claim

let user = await UserModel.findOne({name: "chioma"});
await user.disallow("claim");

Assigning a role to a user

Before assigning a role to a user, make sure it has been created.

//Example

 let user = await UserModel.findOne({name: "chioma"});
 await user.assign("role");

/*
The Gateman hasRolesAndClaims class is loaded into a valid mongoose model which means that the methods are only accessible to valid user objects.
*/

//Retracting a role from a user

let user = await UserModel.findOne({name: "chioma"});
await user.retract("role");

Checking for User claims and Roles

Gateman provides an easy way of verifying if a user belongs to a role or can perform a claim

//To verify if a User belongs to a Role

let user = await User.findOne({name: "chioma"});
let userHasRole = await user.isA("role");
if (userHasRole){
    //user belongs to role
}

//To verify if a User can perform a claim

let user = await User.findOne({name: "chioma"});
let userHasClaim = await user.can("claim");
if (userHasClaim){
    //user can perform claim
}

Retrieving User Roles and Claims

Gateman provides an easy way of retrieving a User's roles and/or claims

//Returns a collection of Roles assigned to a User

let user = await User.findOne({name: "chioma"});
let roles = await user.getRolesForUser();
console.log(roles);

//Returns a collection of Claims a User can perform

let user = await User.findOne({name: "chioma"}, (err, user)=>{
let claims = await user.getClaimsForUser();
console.log(claims);

Documentation

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

  • This project was inspired by Joseph Silber's Bouncer
  • Mongoose was used to build this