Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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()
}
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
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)
})
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);
}
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 })
}
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);
}
export function createAbility() {
return new Ability([]);
}
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()
}