Giter VIP home page Giter VIP logo

feathers-mongoose-acl's Introduction

Build Status Coverage Status TypeScript License

feathers-mongoose-acl

feathers-mongoose-acl (or fma for short) gives you the power to add a simple yet powerful access layer mechanism to your feathers-mongoose app.

fma comes built it with two layers of access control.
Document level access control and Field level access control.

With document level access layer, you can control which user role can have access to a document inside each service method call.
With field level access layer, you can control which user can a field in the document.

Getting Started

npm i feathers-mongoose-acl

Basic Usage Example

import {OR, ACCESS_LAYER, AccessLayerHookExternal} from 'feathers-mongoose-acl'
const createdByUser = context => {createdBy: context.params.user._id}
const assignedToUser = context => {assignedTo: context.params.user._id}

const access: ACCESS_LAYER = {
  DOC_ACCESS: {
    find: { ADMIN: true, '*': OR(createdByUser, assignedToUser)},
    remove: { ADMIN: true }
  },
  NONE_PATCHABLE_FIELDS: {
    ADMIN: {},
    '*': {
      fields: ['deleted', 'role', 'account'],
      throw: true
    }
  }
}

const getUserRoleFromContext = context => context.params.user.role

const before: Partial<HookMap> = {
  all: [index.authenticate(['jwt']), AccessLayerHookExternal(getUserRoleFromContext, access)]
}

Let's unpack what we've done here.

  1. we create access layer object. each access layer object has doc access, and none_patchable_fields.
    in doc access, we defined for each method the user roles that have access to a document through service method calls. for example in "find" method, we give admin user permission to access all documents, but for all other user roles we only allow a user to access a document if he created it or if he is assigned to it.
  2. we defined none patchable fields, as their name indicates, once defined for a role, if he tries to modify them through patch, he will recieve an error.(if defined throw: true)
  3. we define a way to extract user role from the given context
  4. finally, we pass in the userRoleExtraction function and access object to the service before hook

Typescript

feathers-mongoose-acl comes with internal typescript support

Test

To test it locally please run

npm run test:local

Contribution

Any type of contribution is welcomed, feel free to pm me ๐Ÿ’“

feathers-mongoose-acl's People

Contributors

dependabot[bot] avatar naorzr avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.