Last active
July 27, 2017 13:18
-
-
Save stalniy/8653de4c7b5f5f7897e602f07e00e50d to your computer and use it in GitHub Desktop.
CASL abilities js-data aurelia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { DataStore } from '../services/ds' | |
function listenToSessionChanges(ds, onChange) { | |
ds.on('afterCreate', (name, session) => { | |
if (name === 'Session') { | |
onChange(session) | |
} | |
}) | |
ds.on('afterFind', (name, id, mapper, session) => { | |
if (name === 'Session') { | |
onChange(session) | |
} | |
}) | |
ds.on('afterDestroy', (name) => { | |
if (name === 'Session') { | |
onChange(null) | |
} | |
}) | |
} | |
export function configureAbility({ container }) { | |
const ability = new Ability([]) | |
container.registerInstance(Ability, ability) | |
listenToSessionChanges(container.get(DataStore), session => { | |
const rules = defineRulesFor(session ? session.user : null) | |
ability.update(rules) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment