4.24.1 • Published 1 day ago

@strapi/permissions v4.24.1

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
github
Last release
1 day ago

Strapi Permissions

Highly customizable permission engine made for Strapi

Get Started

yarn add @strapi/permissions
const permissions = require('@strapi/permissions');

const engine = permissions.engine.new({ providers });

const ability = await engine.generateAbility([
  { action: 'read' },
  { action: 'delete', subject: 'foo' },
  { action: 'update', subject: 'bar', properties: { fields: ['foobar'] } },
  {
    action: 'create',
    subject: 'foo',
    properties: { fields: ['foobar'] },
    conditions: ['isAuthor'],
  },
]);

ability.can('read'); // true
ability.can('publish'); // false
ability.can('update', 'foo'); // false
ability.can('update', 'bar'); // true
  • You need to give both an action and a condition provider as parameters when instantiating a new permission engine instance. They must be contained in a providers object property.
  • You can also pass an abilityBuilderFactory to customize what kind of ability the generateAbility method will return. By default it'll use a @casl/ability builder.

You can also register to some hooks for each engine instance. See lib/engine/hooks.js -> createEngineHooks for available hooks.

const permissions = require('@strapi/permissions');

const engine = permissions.engine
  .new({ providers })
  .on('before-format::validate.permission', ({ permission }) => {
    if (permission.action === 'read') {
      return false;
    }
  });

const ability = await engine.generateAbility([
  { action: 'read' },
  { action: 'delete', subject: 'foo' },
  { action: 'update', subject: 'bar', properties: { fields: ['foobar'] } },
  {
    action: 'create',
    subject: 'foo',
    properties: { fields: ['foobar'] },
    conditions: ['isAuthor'],
  },
]);

ability.can('read'); // false since the validation hook prevents the engine from registering the permission
ability.can('publish'); // false
ability.can('update', 'foo'); // false
ability.can('update', 'bar'); // true

The format.permission hook can be used to modify the permission.

const permissions = require('@strapi/permissions');

const engine = permissions.engine
  .new({ providers })
  .on('before-format::validate.permission', ({ permission }) => {
    if (permission.action === 'modify') {
      return false;
    }
  })
  .on('after-format::validate.permission', ({ permission }) => {
    if (permission.action === 'update') {
      return false;
    }
  })
  .on('format.permission', ({ permission }) => {
    if (permission.action === 'update') {
      return {
        ...permission,
        action: 'modify',
      };
    }
    if (permission.action === 'delete') {
      return {
        ...permission,
        action: 'remove',
      };
    }
    return permission;
  });

const ability = await engine.generateAbility([{ action: 'update' }, { action: 'delete' }]);

ability.can('update'); // false
ability.can('modify'); // true, because create was changed to 'modify'

ability.can('delete'); // false, doesn't exist because it was changed by format.permission
ability.can('remove'); // true, before-format::validate.permission validates before format.permission changed it
4.24.1

3 days ago

5.0.0-beta.6

3 days ago

4.23.2

4 days ago

4.24.0

11 days ago

5.0.0-beta.5

11 days ago

4.23.1

18 days ago

5.0.0-beta.4

18 days ago

4.23.1-alpha.0

19 days ago

4.23.0

25 days ago

5.0.0-beta.3

24 days ago

4.22.1

30 days ago

4.22.0

1 month ago

5.0.0-beta.2

1 month ago

5.0.0-alpha.7

1 month ago

5.0.0-alpha.6

1 month ago

5.0.0-alpha.8

1 month ago

4.21.1

1 month ago

5.0.0-beta.1

1 month ago

5.0.0-alpha.5

2 months ago

5.0.0-alpha.4

2 months ago

5.0.0-alpha.3

2 months ago

4.21.0

2 months ago

5.0.0-beta.0

2 months ago

5.0.0-alpha.2

2 months ago

5.0.0-alpha.1

2 months ago

5.0.0-alpha.0

2 months ago

4.20.5

2 months ago

4.20.4

2 months ago

4.20.3

2 months ago

4.20.2

2 months ago

4.20.1

3 months ago

4.20.0

3 months ago

4.19.1

3 months ago

4.19.0

3 months ago

4.17.1

4 months ago

4.18.1-beta.0

4 months ago

4.18.1-beta.1

4 months ago

4.18.0

4 months ago

4.17.0

4 months ago

4.16.1

5 months ago

4.16.2

5 months ago

4.16.0

5 months ago

4.14.5

7 months ago

4.14.6

6 months ago

4.14.1

7 months ago

4.14.2

7 months ago

4.14.3

7 months ago

4.14.4

7 months ago

4.14.0

7 months ago

4.13.6

8 months ago

4.13.7

8 months ago

4.13.2

8 months ago

4.13.3

8 months ago

4.13.4

8 months ago

4.13.5

8 months ago

4.14.0-beta.0

8 months ago

4.13.0

8 months ago

4.13.1

8 months ago

4.15.4

6 months ago

4.15.5

5 months ago

4.15.0

6 months ago

4.15.1

6 months ago

4.15.2

6 months ago

4.15.3

6 months ago

4.15.5-alpha.1

6 months ago

4.15.5-alpha.0

6 months ago

4.15.5-alpha.3

6 months ago

4.15.5-alpha.2

6 months ago

4.15.5-alpha.5

6 months ago

4.15.5-alpha.4

6 months ago

4.14.0-alpha.0

8 months ago

4.15.5-alpha.6

5 months ago

4.12.7

8 months ago

4.12.4

9 months ago

4.12.5

9 months ago

4.12.6

9 months ago

4.12.0

9 months ago

4.12.1

9 months ago

4.12.2

9 months ago

4.11.4

10 months ago

4.11.5

10 months ago

4.11.6

10 months ago

4.11.7

10 months ago

4.11.3

10 months ago

4.13.0-beta.0

9 months ago

4.12.0-beta.1

10 months ago

4.12.0-beta.0

10 months ago

4.12.0-beta.5

10 months ago

4.12.0-beta.4

10 months ago

4.12.0-beta.3

10 months ago

4.15.0-alpha.0

7 months ago

4.13.0-alpha.0

8 months ago

4.13.0-alpha.1

8 months ago

4.13.0-alpha.2

8 months ago

4.15.3-alpha.0

6 months ago

4.15.3-alpha.1

6 months ago

4.15.3-alpha.2

6 months ago

4.15.3-alpha.3

6 months ago

4.10.8

11 months ago

4.11.0

11 months ago

4.11.1

11 months ago

4.11.2

11 months ago

4.10.8-exp.0

11 months ago

4.11.0-exp.2

11 months ago

4.11.0-exp.1

11 months ago

4.11.0-exp.0

11 months ago

4.11.1-beta.1

11 months ago

4.11.1-beta.0

11 months ago

4.11.1-beta.2

11 months ago

4.11.0-alpha.0

12 months ago

4.11.0-beta.1

11 months ago

4.11.0-beta.0

12 months ago

4.10.5

12 months ago

4.10.6

12 months ago

4.10.7

11 months ago

4.10.2

1 year ago

4.10.3

12 months ago

4.10.4

12 months ago

4.9.0

1 year ago

4.9.2

1 year ago

4.9.1

1 year ago

4.8.1

1 year ago

4.8.0

1 year ago

4.8.2

1 year ago

4.9.0-beta.2

1 year ago

4.10.2-alpha.0

1 year ago

4.10.1

1 year ago

4.10.0

1 year ago

4.9.0-beta.1

1 year ago

4.10.0-beta.0

1 year ago

4.10.0-beta.1

1 year ago

4.7.0

1 year ago

4.7.1

1 year ago

4.6.2

1 year ago

4.9.0-alpha.0

1 year ago

4.6.1

1 year ago

4.7.0-beta.0

1 year ago

4.5.4

1 year ago

4.5.6

1 year ago

4.5.5

1 year ago

4.6.0-alpha.1

1 year ago

4.6.0

1 year ago

4.6.0-beta.2

1 year ago

4.6.0-beta.1

1 year ago

4.6.0-beta.0

1 year ago

4.5.3

1 year ago

4.5.0-beta.0

2 years ago

4.6.0-alpha.0

1 year ago

4.4.3

2 years ago

4.4.2

2 years ago

4.4.5

2 years ago

4.4.4

2 years ago

4.4.7

1 year ago

4.4.6

2 years ago

4.5.0

1 year ago

4.5.2

1 year ago

4.5.1

1 year ago

4.4.1

2 years ago

4.4.0

2 years ago

4.4.0-rc.1

2 years ago

4.4.0-rc.0

2 years ago

4.4.0-beta.4

2 years ago

4.4.0-beta.1

2 years ago

0.0.0

2 years ago