How to use the @casl/ability.AbilityBuilder.extract function in @casl/ability

To help you get started, we’ve selected a few @casl/ability examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stalniy / casl-feathersjs-example / src / hooks / abilities.js View on Github external
function defineAbilitiesFor(user) {
  const { rules, can } = AbilityBuilder.extract()

  can('create',['users'])
  can('read', ['posts', 'comments'])

  if (user) {
    can('manage', ['posts', 'comments'], { author: user._id })
    can(['read', 'update'], 'users', { _id: user._id })
  }

  if (process.env.NODE_ENV !== 'production') {
    can('create', ['users'])
  }

  return new Ability(rules, { subjectName })
}
github stalniy / casl-express-example / src / modules / auth / abilities.js View on Github external
function defineAbilitiesFor(user) {
  const { rules, can } = AbilityBuilder.extract();

  can('read', ['Post', 'Comment']);
  can('create', 'User');

  if (user) {
    can(['create', 'delete', 'update'], ['Post', 'Comment'], { author: user._id });
    can(['read', 'update'], 'User', { _id: user.id });
  }

  return new Ability(rules);
}
github feathers-nuxt / template-app / template / src / client / utils / initAuth.js View on Github external
function defineRulesFor(user){
  var ref$, rules, can, i$, len$, module, actions, j$, len1$, action;
  ref$ = AbilityBuilder.extract(), rules = ref$.rules, can = ref$.can;
  if (user.permissions && typeof user.permissions === 'object') {
    for (i$ = 0, len$ = (ref$ = Object.keys(user.permissions)).length; i$ < len$; ++i$) {
      module = ref$[i$];
      actions = eval('(' + user.permissions[module] + ')');
      for (j$ = 0, len1$ = actions.length; j$ < len1$; ++j$) {
        action = actions[j$];
        can(action, module);
      }
    }
  }
  return rules;
};
github Xabaril / Esquio / src / Esquio.UI / ClientApp / src / app / shared / user / user.ability.ts View on Github external
export function defineAbilitiesFor(permissions: UserPermissions) {
  const { rules, can: allow, cannot: forbid } = AbilityBuilder.extract();

  if (permissions.isAuthorized) {
    allow(Action.Create, [Subject.Token]);
  }

  if (permissions.readPermission || permissions.writePermission || permissions.managementPermission) {
    allow(Action.Read, [Subject.Product, Subject.Flag, Subject.Toggle]);
  }

  if (permissions.writePermission || permissions.managementPermission) {
    allow(Action.Create, [Subject.Product, Subject.Flag, Subject.Toggle]);
    allow(Action.Update, [Subject.Product, Subject.Flag, Subject.Toggle]);
    allow(Action.Delete, [Subject.Product, Subject.Flag, Subject.Toggle]);
  }

  if (permissions.managementPermission) {

@casl/ability

CASL is an isomorphic authorization JavaScript library which restricts what resources a given user is allowed to access

MIT
Latest version published 10 days ago

Package Health Score

88 / 100
Full package analysis