How to use the @casl/ability.Ability 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 voluntarily / vly2 / server / middleware / getAbility / getAbility.js View on Github external
// console.log('getAbility req.session', req.session)
  const rootPath = require('path').join(__dirname, '/../../..')
  const pattern = rootPath + options.searchPattern
  // console.log('getAbility pattern', pattern)
  const userRoles = req.session && req.session.me ? req.session.me.role : [Role.ANON]
  // console.log('getAbility userRoles', userRoles)
  let allRules = []
  glob.sync(pattern).forEach(abilityPath => {
    // console.log('getAbility abilityPath', abilityPath)
    userRoles.forEach(role => {
      const rules = require(abilityPath)[role]
      // console.log('getAbility rules', rules)
      allRules = allRules.concat(rules)
    })
  })
  req.ability = new Ability(allRules)
  // console.log('getAbility req.ability', req.ability)
  next()
}
github feathers-nuxt / template-app / template / src / client / utils / store / plugins / casl.js View on Github external
import { Ability } from '@casl/ability'

export const abilityInstance = new Ability()

export const abilityPlugin = (store) => {

  abilityInstance.update(store.state.rules)

  return store.subscribe((mutation) => {
    switch (mutation.type) {
      case 'auth/setUser':
        // store.$router.app.$storyboard.mainStory.info('casl:store:plugin', 'user logged in, setting access rules')
        store.app.api.storyboard.mainStory.trace('casl:vuex', '@store/plugins/casl update permissions')
        abilityInstance.update(mutation.payload.rules)
        break
      case 'auth/logout':
        console.log('@store/plugins/casl user logged out, REsetting access rules')
        abilityInstance.update([{ actions: 'read', subject: 'all' }])
        break
github voluntarily / vly2 / server / api / person / __tests__ / personController.spec.js View on Github external
test.serial('Should call send status function for null record ', async t => {
  const userIDWantToUpdate = 'asdfasdfgadf'
  const fakeSendStatus = sinon.fake()
  const rawRules = [
    { action: 'update', subject: 'Person', conditions: { _id: userIDWantToUpdate } }
  ]
  const ability = new Ability(rawRules)

  const request = new MockExpressRequest()
  request.body = {
    _id: '5d48f775741eab0d344d4c29'
  }
  request.ability = ability

  const response = new MockResponse()
  response.sendStatus = (status) => { fakeSendStatus() }
  await updatePersonDetail(request, response)
  t.is(1, fakeSendStatus.callCount)
})
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 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 Xabaril / Esquio / src / Esquio.UI / ClientApp / src / app / shared / user / user.ability.ts View on Github external
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) {
    allow([Action.Manage], [Subject.Permission]);
  }

  if (!permissions.isAuthorized) {
    forbid(Action.Manage, [Subject.All]);
  }

  return new Ability(rules);
}
github stalniy / casl / packages / casl-angular / src / module.ts View on Github external
export function createAbility() {
  return new Ability([]);
}
github voluntarily / vly2 / server / middleware / ability / getAbility.js View on Github external
const rootPath = require('path').join(__dirname, '/../../..')
  const pattern = rootPath + options.searchPattern
  const userRoles = req.session && req.session.me ? req.session.me.role : [Role.ANON]
  let allRules = []
  glob.sync(pattern).forEach(abilityRuleBuilderPath => {
    const ruleBuilder = require(abilityRuleBuilderPath)
    const rules = ruleBuilder(req.session)
    for (const role of userRoles) {
      if (rules[role] == null) continue
      if (role) {
        allRules = allRules.concat(rules[role])
      }
      if (role === 'admin') break
    }
  })
  req.ability = new Ability(allRules)
  next()
}

@casl/ability

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

MIT
Latest version published 14 days ago

Package Health Score

88 / 100
Full package analysis