This file contains 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
describe('Invoice', function() { | |
var invoice, user; | |
beforeEach(function() { | |
user = User.create({ role: 'member' }); | |
invoice = user.invoices.create({ price: 10, currency: 'USD' }); | |
}); | |
it('has status "fraud" if amount does not equal to invoice amount', function() { | |
invoice.paid(1, 'USD'); |
This file contains 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/ability' | |
export const ability = new Ability() | |
export const abilityPlugin = (store) => { | |
ability.update(store.state.rules) | |
return store.subscribe((mutation) => { | |
switch (mutation.type) { | |
case 'createSession': |
This file contains 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
class Ability | |
include CanCan::Ability | |
# .... | |
def to_list | |
rules.map do |rule| | |
object = { actions: rule.actions, subject: rule.subjects.map{ |s| s.is_a?(Symbol) ? s : s.name } } | |
object[:conditions] = rule.conditions unless rule.conditions.blank? | |
object[:inverted] = true unless rule.base_behavior |
This file contains 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
{ | |
"token": "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoyfQ.3MA5pz-JXuSs3YHdIEJcokTpharBLjUmfzXGp1dyYY8", | |
"rules": [ | |
{ | |
"actions": ["read"], | |
"subject": ["all"] | |
}, | |
{ | |
"actions": ["manage"], | |
"subject": ["Article"], |
This file contains 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 { permittedFieldsOf } = require('@casl/ability/extra') | |
const pick = require('lodash.pick') | |
const ability = require('./ability') | |
const app = require('./app') | |
const Product = require('./models/Product') | |
// See https://stalniy.github.io/casl/abilities/2017/07/21/check-abilities.html#checking-fields for details | |
app.patch('/products/:id', (req, res, next) => { | |
Product.findById(req.params.id) |
This file contains 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
<template> | |
<form novalidate> | |
<input v-model="product.title" :readonly="!$can('update', product, 'title')"> | |
<textarea v-model="product.description" :readonly="!$can('update', product, 'description')"></textarea> | |
<input v-model="product.price" :readonly="!$can('update', product, 'description')"> | |
</form> | |
</template> | |
<script> | |
.... |
This file contains 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/ability' | |
const user = .... | |
const ability = AbilityBuidler.define(can => { | |
can('read', 'all') | |
if (user.isAdmin) { | |
can('update', 'Product') | |
} else if (user.isEditor) { | |
can('update', 'Product', ['title', 'description']) |
This file contains 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
#!/bin/sh | |
MYSQLADMIN_CFG="/etc/mysql/mariadb.conf.d/90-mysqladmin.cnf" | |
# generate password | |
PASS=$(perl -e 'print map{("a".."z","A".."Z",0..9)[int(rand(62))]}(1..16)'); | |
# adjust /etc/mysql/debian.cnf (used as defaults file by system scripts) | |
sed -i "s/^password =.*$/password = ${PASS}/" /etc/mysql/debian.cnf | |
sed -i "s/^user =.*$/user = debian-sys-maint/" /etc/mysql/debian.cnf | |
# create config file for mysqladmin itself (maybe not needed) | |
umask 066 | |
cat > ${MYSQLADMIN_CFG} <<EOF |
This file contains 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
class Can extends PureComponent { | |
//... | |
componentWillMount() { | |
this.unsubscribeFromAbility = ability.on('update', () => { | |
setTimeout(() => this.recheck(), 0) | |
}); | |
this.recheck(); | |
} |