Code Monkey home page Code Monkey logo

simple-abac-1's Introduction

simple-abac

Node.js package that makes attribute based access control (ABAC) simple.

Usage

Installation:

npm i simple-abac -S

Import in your project:

  • Typescript/ES6:
  import { SimpleAbac } from 'simple-abac';
  const abac = new SimpleAbac();
  • Javascript(ES5):
  let SimpleAbac = require('simple-abac').SimpleAbac;
  let abac = new SimpleAbac();

Defining permissions:

Allow editor to read all attributes of posts except authorId:

  abac.allow({
    role: 'editor',
    actions: 'read',
    targets: 'post',
    attributes: { mode: 'all', except: ['authorId']},
  });

Allow admin to read all attributes of posts:

  abac.allow({
    role: 'admin',
    actions: 'read',
    targets: 'post',
    attributes: { mode: 'all' },
  });

Allow any to read only content and title of posts:

  abac.allow({
    role: 'any',
    actions: 'read',
    targets: 'post',
    attributes: { mode: 'nothing', except: ['content', 'title'] },
  });

Allow editor to delete only posts created by him:

  abac.allow({
    role: 'editor',
    actions: 'delete',
    targets: 'post',
    condition: (userId, targetOptions) => {
      return userId === targetOptions.authorId;
    }
  });

Asking permissions:

Asking if editor with id: 1 can read post:

  const permission = await abac.can({ id: 1, role: 'editor' }, 'read', 'post', {});
  /* 
  {
    granted: true,
    attributes: {
      mode: 'all',
      except: ['authorId']
    }
  } 
  */

Asking if any can read post:

  const permission = await abac.can(undefined, 'read', 'post', {});
  /* 
  {
    granted: true,
    attributes: {
      mode: 'nothing',
      except: ['content', 'title']
    }
  } 
  */

Asking if editor with id: 1 can delete a post written by editor with id: 3:

  const permission = await abac.can({ id: 1, role: 'editor' }, 'delete', 'post', {authorId: 3, ...});
  /* 
  {
    granted: false,
    attributes: {
      mode: 'nothing'
    }
  }
  */

Asking if editor with id: 3 can delete a post written by editor with id: 3:

  const permission = await abac.can({ id: 3, role: 'editor' }, 'delete', 'post', {authorId: 3, ...});
  /* 
  {
    granted: true,
    attributes: {
      mode: 'all'
    }
  }
  */

simple-abac-1's People

Contributors

agroprediccion avatar selarep avatar

Watchers

 avatar

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.