I'm trying to update the rules based on a user acces level
here's my ability js
// in ability.js
'use strict' //this line I added because of google, not sure if it works or not
import { Ability, AbilityBuilder } from "@casl/ability";
var ability = new Ability();
function setUserAbilities (user) {
const { rules, can } = AbilityBuilder.extract()
// console.log(rules)
if(user.orgAccessLevel){
if (user.orgAccessLevel === 3) {
// console.log(user.user.id)
can('delete', 'someone')
can('delete', 'someone1')
can('delete', 'someone2')
can('delete', 'someone3')
}
if (user.orgAccessLevel === 2) {
// Reglas de coordinador
can('[read, create, update]', 'all')
}
else {
//Usuario Normal
can('read', 'all')
}
// console.log(rules)
return new Ability(rules, {subjectName})
}
// console.log(rules)
}
function subjectName(subject) {
console.log(subject, 'subject')
if (!subject || typeof subject === 'string') {
return subject
}
return subject[TYPE_KEY]
}
export {ability, setUserAbilities}
//mainjs
import {ability} from './ability/ability.js'
Vue.use(abilitiesPlugin, ability)
vue component
import {setUserAbilities} from '../../ability/ability.js'
setUserAbilities(myAccess) // which this will return a 1-3
I know this creates the right rules, but when inserting them to the new Ability(rules), it wont work, I'm fascinated by this vision of yours, that's why I want to implement this, but I've been stuck for a week until now I'm out of ideas, can you help me?
BTW this is more a question than an issue I think
Hi @hectort90 ,
First of all, you don't need use strict because all ES6+ modules are considered to be strict.
Second, I don't understand the code completely :)
What is myAccess? According to definition of setUserAbilities it should be a user instance. If you later want to update abilities, then just update existing one. Inside any Vue component call this.$ability.update(newRules), where newRules is an array of rules (not Ability instance!)
Hello @stalniy
I've already determined my two problems:
First, in Vue, I wanted to created a permission by default which is (read, all), and then modify the instance by setting new users permissions and returning a new Ability(rules), but I can't override the instance of ability since its loaded by vue in the main.js. No worries, I had to make a work around and do it by the this.$ability.update, which is cool cool!
Second: I was sending the array in wrong, so now my ability instance has worked with its new rules!
Thanks for the plugin,
Cool! I鈥檓 closing this then