Skip to content

Instantly share code, notes, and snippets.

View stalniy's full-sized avatar
🏠
Working from home

Serhii Stotskyi stalniy

🏠
Working from home
View GitHub Profile
@stalniy
stalniy / submit-google-form.js
Created August 16, 2017 08:12
Submit google form
function c(name) {
return $x(`//div[@role="listitem" and @jscontroller and .//*[@role="heading" and text()[contains(., "${name}")]]]`)[0];
}
function report(attrs) {
const name = c('Name');
name.click();
setTimeout(() => {
@stalniy
stalniy / fix-browserify-wkwebview.js
Created August 5, 2017 20:28
Cordova hook to fix WK WebView and --browserify option (temporary workaround)
/**
* Just add this script as `after_build` and `after_prepare` hook in config.xml for ios platform.
*/
const { join } = require('path')
const fs = require('fs')
// TODO: Remove this after https://issues.apache.org/jira/browse/CB-11311 is fixed
module.exports = function(ctx) {
if (!ctx.opts.browserify) {
@stalniy
stalniy / abilities.js
Last active July 28, 2017 15:07
CASL feathers service name
const { Ability, AbilityBuilder, toMongoQuery } = require('casl')
const { Forbidden } = require('feathers-errors')
const TYPE_KEY = Symbol.for('type') // <--- added
// the rest of the logic
function subjectName(subject) { // <--- added
if (!subject || typeof subject === 'string') {
return subject
}
@stalniy
stalniy / authenticate.js
Last active July 28, 2017 14:45
Feathers authentication with silent errors
const { authenticate } = require('feathers-authentication').hooks
const { NotAuthenticated } = require('feathers-errors')
const verifyIdentity = authenticate('jwt')
function hasToken(hook) {
return hook.params.headers.authorization || hook.data.accessToken
}
module.exports = async function authenticate(hook) {
try {
@stalniy
stalniy / hooks.js
Last active July 28, 2017 14:49
CASL before all hook in feathersjs app
const { when } = require('feathers-hooks-common')
const authorize = require('./hooks/abilities')
const authenticate = require('./hooks/authenticate')
module.exports = {
before: {
all: [
when(
hook => hook.params.provider && `/${hook.path}` !== hook.app.get('authentication').path,
authenticate,
@stalniy
stalniy / abilities.js
Last active July 28, 2017 15:07
CASL hook in feathersjs
const { Ability, AbilityBuilder, toMongoQuery } = require('casl')
const { Forbidden } = require('feathers-errors')
// the rest of the logic
module.exports = function authorize(name = null) {
return async function(hook) {
const action = hook.method
const service = name ? hook.app.service(name) : hook.service
const serviceName = name || hook.path
@stalniy
stalniy / abilities.js
Created July 28, 2017 13:09
CASL aliases for feathersjs
const { Ability, AbilityBuilder } = require('casl')
Ability.addAlias('update', 'patch')
Ability.addAlias('read', ['get', 'find'])
Ability.addAlias('remove', 'delete')
// the rest of the logic
module.exports = function authorize(serviceName) {
return async (hook) => {
@stalniy
stalniy / abilities.js
Last active July 28, 2017 13:13
CASL abilities in feathersjs
const { AbilityBuilder, Ability } = require('casl')
function defineAbilitiesFor(user) {
const { rules, can } = AbilityBuilder.extract()
can('read', ['posts', 'comments'])
if (user) {
can('manage', ['posts', 'comments'], { author: user._id })
can(['read', 'update'], 'users', { _id: user._id })
@stalniy
stalniy / can-behavior.html
Created July 27, 2017 13:40
CASL can binding behavior in template
<nav class="menu">
<h3>Menu</h3>
<ul>
<li if.bind="'Post' & can: 'create'">
<a route-href="route: newPost">Add Post</a>
</li>
</ul>
</nav>
@stalniy
stalniy / can-behavior.js
Created July 27, 2017 13:38
CASL can binding behavior
import { SignalBindingBehavior } from 'aurelia-templating-resources'
import { ValueConverter } from 'aurelia-binding'
export class CanBindingBehavior {
static inject = [SignalBindingBehavior]
constructor(signalBindingBehavior) {
this.signalBindingBehavior = signalBindingBehavior
}