@fabrix/spool-permissions
TypeScript icon, indicating that this package has built-in type declarations

1.5.1 • Public • Published

spool-permission

Gitter NPM version Build Status [![Test Coverage][coverage-image]][coverage-url] Dependency Status Follow @FabrixApp on Twitter

Permissions built for speed, security, and scalability

The Permissions spool is built to be used on Fabrix. It's purpose is to allow for complex ERP style permissions down to the model level as well as restrict routes based on permissions.

Dependencies

Supported ORMs

Repo Build Status (edge)
spool-sequelize Build status

Supported Webserver

Repo Build Status (edge)
spool-express Build status

Install

$ npm install --save spool-permission

Configuration

First you need to add this spool to your main configuration :

// config/main.ts

export const main = {
   // ...

   spools: [
      // ...
      require('@fabirx/spool-permission').PermissionsSpool,
      // ...
   ]
   // ...
}

Then permissions config:

// config/permissions.ts
export const permissions = {
  //Role name to use for anonymous users
  defaultRole: 'public',
  //Role name to add to users on create
  defaultRegisteredRole: 'registered',
  // Name of the association field for Role under User model
  userRoleFieldName: 'roles',
  // add all models as resources in database on initialization
  modelsAsResources: true,
  // Initial data added when DB is empty
  fixtures: {
    roles: [],
    resources: [],
    permissions: []
  },
  // The default super admin username
  defaultAdminUsername: 'admin',
  // The default super admin password
  defaultAdminPassword: 'admin1234'
}

You also need to have a User model like:

import { User as PermissionsUser } from '@fabrix/spool-permissions/dist/models'
import { defaults, defaultsDeep } from 'lodash'

class User extends PermissionsUser {
  static config(app, Sequelize) {
    return defaultsDeep(PermissionsUser.config, {
      options: {
        // your options
      }
    })
  }
  static schema(app, Sequelize) {
    return defaults(PermissionsUser.schema, {
     // your schema
    })
  }
  static associate(models) {
    PermissionsUser.associate(models)
    // your associations
  }
}

Usage

Default Admin

When your applications starts, if there are no users in your database, permission will create a super admin using your defaults

Manage roles

Use the native sequelize model under this.app.models.Roles, if you need initial roles just add them on permissions config file under fixtures.roles.

Manage resources

Use the native sequelize model under this.app.models.Resources, if you need initial resources just add them on permissions config file under fixtures.resources.

Manage model permissions

Static declaration under config

//config/permissions.ts
export const permissions = {
// ...  

fixtures: {
    roles: [{
      name: 'role_name',
      public_name: 'Role name'
    }],
    resources: [{
      type: 'model',
      name: 'modelName',
      public_name: 'Model name'
    }],
    permissions: [{
       role_name: 'role_name',
       resourceName: 'modelName',
       action: 'create'
     }, {
       role_name: 'role_name',
       resourceName: 'modelName',
       action: 'update'
     }, {
       role_name: 'role_name',
       resourceName: 'modelName',
       action: 'destroy'
     }, {
       role_name: 'role_name',
       resourceName: 'modelName',
       action: 'access'
     }]
  }
}

Owner permissions

This spool can manage owner permissions on model instance, to do this you need to declare your permissions like this :

{
  roleName: 'roleName',
  relation: 'owner',
  resourceName: 'modelName',
  action: 'create'
}

You can create this permissions with sequelize model, with fixtures options or with PermissionsService like this:

this.app.services.PermissionsService.grant('roleName', 'modelName', 'create', 'owner').then(perm => () => {})
.catch(err => this.app.log.error(err))

Then you need to declare an owners attributes on your models like this :

export class Item extends Model {
  static config(app, Sequelize) {
    return {
      options: { }
    }
  }

  public static get resolver() {
    return SequelizeResolver
  }

  public static associate (models) {
    models.Item.belongsToMany(models.User, {
      as: 'owners',
      through: 'UserItem'//If many to many is needed
    })
  }
}

If the model is under a spool and you don't have access to it you can add a model with same name on your project, let's do this for the model User witch is already in spool-permission and spool-proxy-passport:

import { User as PermissionsUser } from '@fabrix/spool-permissions/dist/models'
import { defaults, defaultsDeep } from 'lodash'

class User extends PermissionsUser {
  static config(app, Sequelize) {
    return defaultsDeep(PermissionsUser.config, {
      options: {
        // your options
      }
    })
  }
  static schema(app, Sequelize) {
    return defaults(PermissionsUser.schema, {
     // your schema
    })
  }
  static associate(models) {
    PermissionsUser.associate(models)
    // your associations
  }
}

Like this you can add owners permissions on all preferred models.

WARNING! Currently owner permissions are not supported for update destroy actions on multiple items (with no ID)

Dynamically with PermissionsService

// Grant a permission to create 'modelName' to 'roleName'
this.app.services.PermissionsService.grant('roleName', 'modelName', 'create').then(perm => () => {})
.catch(err => this.app.log.error(err))

// Revoke a permission to create 'modelName' to 'roleName'
this.app.services.PermissionsService.revoke('roleName', 'modelName', 'create').then(perm => () => {})
.catch(err => this.app.log.error(err))

Manage route permissions

Route permissions can be added directly under route definition:

export const routes = {
  // ...
  '/api/myroute': {
    'GET': 'DefaultController.myroute',
    config: {
      app: {
        permissions: {
          resourceName: 'myrouteId',
          roles: ['roleName']
        }
      }
    }
  }
  // ...
}

When the DB is empty all routes permissions will be created, if you make any change after this you'll have to update permissions yourself.

You can use PermissionsService anytime you want to grant or revoke routes permissions.

Policies

You have 2 policies to manage permissions, they return a 403 when user is not allowed :

CheckPermissions.checkRoute

This one will check your route permissions, if they are no explicit permissions then the route is accessible. The easy way to setup is :

//config/policies.ts
'*': [ 'CheckPermissions.checkRoute' ]
//or
ViewController: [ 'CheckPermissions.checkRoute' ] 

CheckPermissions.checkModel

This one will check your model permissions, if there are no explicit permissions models are not accessible

//config/policies.ts
TapestryController: [ 'CheckPermissions.checkModel' ] // To check permissions on models

Package Sidebar

Install

npm i @fabrix/spool-permissions

Homepage

fabrix.app

Weekly Downloads

8

Version

1.5.1

License

MIT

Unpacked Size

104 kB

Total Files

94

Last publish

Collaborators

  • scottbwyatt