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
| const { AbilityBuilder, Ability } = require('casl') | |
| function defineAbilitiesFor(user) { | |
| const { rules, can } = AbilityBuilder.extract() | |
| can('read', ['Post', 'Comment']) | |
| can('create', 'User') | |
| if (user) { | |
| can('manage', ['Post', 'Comment'], { author: user._id }) |
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
| function defineAbilitiesFor(user) { | |
| const { rules, can } = AbilityBuilder.extract() | |
| if (user) { | |
| can('manage', ['Post', 'Comment'], { author: user._id }) | |
| can(['read', 'update'], 'User', { _id: user.id }) | |
| } else { | |
| can('read', ['Post', 'Comment']) | |
| can('create', 'User') | |
| } |
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 { AbilityBuilder } from 'casl' | |
| const user = whateverLogicToGetUser() | |
| const ability = AbilityBuidler.define(can => { | |
| can('read', ['Post', 'Comment']) | |
| if (user.isLoggedIn) { | |
| can('create', 'Post') | |
| can('manage', ['Post', 'Comment'], { authorId: user.id }) | |
| } |
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
| const { mongoosePlugin, AbilityBuilder } = require('casl') | |
| const mongoose = require('mongoose') | |
| mongoose.plugin(mongoosePlugin) | |
| const Post = mongoose.model('Post', mongoose.Schema({ | |
| title: String, | |
| author: String, | |
| content: String, | |
| createdAt: Date |
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 { Ability } from 'casl' | |
| export class CanValueConverter { | |
| static inject = [Ability] | |
| constructor(ability) { | |
| this.ability = ability | |
| } | |
| toView(subject, action) { |
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 { AbilityBuilder, Ability } from 'casl' | |
| function defineRulesFor(user) { | |
| const { can, rules } = AbilityBuilder.extract() | |
| can('read', ['Post', 'Comment']) | |
| if (user) { | |
| can('create', 'Post') | |
| can('manage', ['Post', 'Comment'], { authorId: user._id }) |
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) => { |
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
| function subjectName(subject) { | |
| if (!subject || typeof subject === 'string') { | |
| return subject | |
| } | |
| return subject.constructor.mapper.name | |
| } | |
| export function configureAbility({ container }) { | |
| const ability = new Ability([], { subjectName }) |
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
| <nav class="menu"> | |
| <h3>Menu</h3> | |
| <ul> | |
| <li if.bind="'Post' | can: 'create'"> | |
| <a route-href="route: newPost">Add Post</a> | |
| </li> | |
| </ul> | |
| </nav> |
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
| <nav class="menu"> | |
| <h3>Menu</h3> | |
| <ul> | |
| <li if.bind="'Post' | can: 'create' & signal: 'ability-changed'"> | |
| <a route-href="route: newPost">Add Post</a> | |
| </li> | |
| </ul> | |
| </nav> |