GithubHelp home page GithubHelp logo

gateman's Introduction

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

gateman's People

Contributors

ibesoft11 avatar nwangwuositadinma avatar valentine-mario avatar

Stargazers

Osaro Imohe avatar Segun_OS avatar Ehsan A avatar Joseph Bassey avatar Cristi Minica avatar Charles avatar Lucas Recknagel avatar Koji Hirano avatar Roman Hossain Shaon avatar Álvaro García León avatar Gabriel Margoses avatar  avatar Dan Levy avatar Gonzalo avatar José M. Gulias Lugo avatar keith avatar Thomas Coats avatar moyu avatar Anushka Mahesh avatar okumurakengo avatar Artem Kryvchun avatar Kristian Koivisto-Kokko avatar Baskar Puvanathasan avatar Ahmad Ali avatar Alex Lyalin avatar Erfan  avatar Oleksii Levchenko avatar  avatar Daniel Shim avatar Hengki Sihombing avatar bruno avatar Mostafa Shaheydari avatar  avatar Stefan Bogdanović avatar  avatar Željko Šević avatar Tetsu avatar Brandon Van Noy avatar Łukasz Karbownik avatar Gregg B avatar Waliur Rahman avatar Sergey Bekharsky avatar Tino Butz avatar Usman Salami avatar Alex Levanov avatar Jeremiah Ajayi avatar Simon Tegg avatar  avatar Olivier Allouch avatar Jeff Haynie avatar Andrew R. Powers avatar Jungho avatar Saad Shahd avatar Luciano Mammino avatar Alexander Kampfmann avatar Jason Morganson avatar Yordan Yordanov avatar Dalci de Jesus Bagolin avatar Anthony Young avatar Lancy Goyal avatar Ionut Cenac avatar Liucw avatar Vitaly Rtishchev avatar Dinesh Nagar avatar R Arun avatar Dmitriev Sergey avatar Niall O'Brien avatar Hongbo LU avatar  avatar Eduardo Rabelo avatar Emmanuel Salomon avatar  avatar Pradeep Chauhan avatar Ahmet Simsek avatar Parker avatar Justin Sisley avatar  avatar Henrique Silvério avatar Erick B avatar Jacob Beltran avatar  avatar Ruslan Konviser avatar Adam Bright avatar Robert McGuinness avatar Arthur Ibik avatar Leonid Ilin avatar Yannick Meeus avatar OkoyeApps avatar  avatar AUGUSTINE IGWE avatar  avatar Travis avatar Jeremy Black avatar Igor Oliveira avatar  avatar  avatar Deolu avatar Muhammad Farid Zia avatar Antoine Amara avatar Shaughn Dolcy avatar

Watchers

James Cloos avatar Mariusz Dugin avatar Chidi Onuekwusi avatar Barnabas Ifebude avatar  avatar Ogbonna Vitalis avatar Sanel K. avatar  avatar

gateman's Issues

createRole: Cannot read property 'then' of undefined

I am trying to create role using gateman.

const mongoose = require("mongoose");
mongoose.connect('mongodb://localhost:27017/authorization', { useNewUrlParser: true });
const gateman = require("gatemanjs").GateMan(mongoose);

gateman.createRole("admin").then((role) => {
  console.log(role);
}).catch((err) => {
  console.log(err);
});

But it is giving me following error:

gateman.createRole("admin").then((role) => {
                           ^

TypeError: Cannot read property 'then' of undefined
    at Object.<anonymous> (C:\Users\saad.mehmood\Desktop\authroization\index.js:4:28)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

Inconsistency in Error

Some functions like async disallow(claimName) throw this error
throw new Error({ message: "The claim does not exist. Consider creating it first" });
while all others throw this one
throw new Error('Error message');

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.