• Stars
    star
    234
  • Rank 171,630 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple, functional authorization library and role management for ruby

Kan

Build Status Backers on Open Collective Sponsors on Open Collective

Simple functional authorization library for ruby. Inspired by transproc and dry project

Table of context

Installation

Add this line to your application's Gemfile:

gem 'kan'

And then execute:

$ bundle

Or install it yourself as:

$ gem install kan

Usage

See User Documentation page

Basic Usage

Register abilities

class Post::Abilities
  include Kan::Abilities

  register('read') { |_, _| true }
  register('edit') { |user, post| user.id == post.user_id }
  register('delete') { |_, _| false }
end

Also, you can register more than one ability in one place and use string or symbol keys:

class Post::AdminAbilities
  include Kan::Abilities

  register(:read, :edit, :delete) { |user, _| user.admin? }
end

class Comments::Abilities
  include Kan::Abilities

  register('read') { |_, _| true }
  register('edit') { |user, _| user.admin? }

  register(:delete) do |user, comment|
    user.id == comment.user_id && comment.created_at < Time.now + TEN_MINUTES
  end
end

Check abilities

abilities = Kan::Application.new(
  post: Post::Abilities.new,
  comment: Comments::Abilities.new
)

abilities['post.read'].call(current_user, post) # => true
abilities['post.delete'].call(current_user, post) # => false
abilities['comment.delete'].call(current_user, post) # => false

Default ability block

By default Kan use proc { true } as a default ability block:

abilities['comment.invalid'].call(current_user, post) # => true

But you can rewrite it

admin_abilities = Kan::Application.new(
  post: Post::AdminAbilities.new(default_ability_block: proc { false }),
  comment: Comments::Abilities.new,
)

admin_abilities['post.delete'].call(current_user, post)  # => false
admin_abilities['post.delete'].call(admin_user, post)    # => true
admin_abilities['post.invalid'].call(current_user, post) # => false

List of abilities

You can provide array of abilities for each scope and Kan will return true if at least one ability return true:

global_abilities = Kan::Application.new(
  post: [Post::Abilities.new, Post::AdminAbilities.new],
  comment: Comments::Abilities.new
)

global_abilities['post.edit'].call(current_user, post) # => false
global_abilities['post.edit'].call(owner_user, post)   # => true
global_abilities['post.edit'].call(admin_user, post)   # => true

Aliases

You can use strings or symbols and then use it as name of ability

class Post::Abilities
  include Kan::Abilities

  register(:edit) { |_, _| true }
  register_alias(:correct, 'edit')
end

abilities = Kan::Application.new(
  post: Post::Abilities.new
)

abilities['post.correct'].call(current_user, post) # => true

Callback

You can provide callable object (that respond to #call) that accepts ability_name and payload params to after_call_callback param of your ability:

admin_abilities = Kan::Application.new(
  post: Post::AdminAbilities.new(after_call_callback: -> (ability_name, payload) { ... }),
  comment: Comments::Abilities.new,
)

admin_abilities['post.read'].call(current_user, post) # => false

Your object will be executed after calling ability.

Contributing

Code and features

Bug reports and pull requests are welcome on GitHub at https://github.com/davydovanton/kan. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Docs

Just send PR with changes in docs/ folder.

How to instal the project

Just clone repository and call:

$ bundle install
$ bundle exec rspec

Contributors

This project exists thanks to all the people who contribute.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Kan project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

More Repositories

1

sidekiq-statistic

See statistic about your workers
Ruby
797
star
2

awesome-hanami

A collection of awesome Hanami Gems and projects
Ruby
410
star
3

stop_active_support_anywhere

Stop use Active Support in not rails related projects
Ruby
146
star
4

shallow_attributes

Simple and lightweight Virtus analog.
Ruby
99
star
5

rubyjobs.dev

Job boards for Russian-speaking Ruby developers
Ruby
80
star
6

novel

Orchestration SAGA builder for ruby
Ruby
47
star
7

hanami-architecture

Ideas and suggestions about architecture for hanami projects
47
star
8

rspec-hanami

RSpec Matchers for Hanami
Ruby
46
star
9

cqrs-ruby-example

Example of using CQRS with ruby, hanami-api, rom, dry-system and kafka
Ruby
42
star
10

AnyBar_rb

Ruby wrapper for AnyBar.app
Ruby
37
star
11

ruby-service-template

Simple (Micro)Service template based on dry-system, hanami-api, hanami 2.0, rom, rabbitmq, kafka, psql, and other microlibraries.
Ruby
31
star
12

relative_time

Micro lib without any dependency for getting relative time
Ruby
22
star
13

hanami-serializer

Serializer library for hanami applications
Ruby
22
star
14

dotfiles

My dotfiles
Vim Script
21
star
15

rodauth_hanami

Example app for integrate rodauth server to hanami app
Ruby
16
star
16

web_bouncer

Simple and module auth lib for any rack projects
Ruby
16
star
17

popug-inventory

HTML
16
star
18

layerd-arch-style-with-dry-example

Example of layered architecture style builded with dry-rb libs
Ruby
16
star
19

hanami_event_example

Simple hanami app with hanami events
Ruby
15
star
20

hanami-pagination

Ruby
14
star
21

hanami-bootstrap

Bootstrap wrapper for hanami framework.
Ruby
14
star
22

cookie_box

Follow and controll issues from several repositories from one place
Ruby
12
star
23

hanami-scaffold

Make hanami scaffolds faster
Ruby
12
star
24

octostar

Improved search for your github stars
Ruby
11
star
25

event_schema_registry

Simple implantation of schema registry for JSON schema events
Ruby
11
star
26

rom_sql_graph

Display your DB (sql) association graph
Ruby
9
star
27

soa-readiness-checklist

Check your system for SOA readiness
9
star
28

state_changer

The state machine for change your data between states.
Ruby
9
star
29

dry-system-hanami

Folder resolver for dry-system in hanami projects
Ruby
8
star
30

hanami-zsh

Zsh plugin for hanami.
7
star
31

hanami-rodauth

This repository is no longer maintain: Roudauth wrapper for hanami apps
Ruby
7
star
32

ivento

Simple event sourcing framework in functional style
Ruby
7
star
33

rlisp

Simple scheme interpreter written on ruby
Ruby
7
star
34

hanami-bench

Benchmarks for hanami
Ruby
6
star
35

link-shortener

Simple hanami link shortener application
Ruby
6
star
36

ruby-job-task

Тестовое задание на позицию ruby разработчика
Ruby
6
star
37

vim-html2slim

Vim plugin for convert HTML(erb) to Slim
Vim Script
6
star
38

hanami-project-template

Template repository for all new project
Ruby
5
star
39

AnyBar_cr

Simple crystal wrapper for AnyBar
Crystal
5
star
40

igoods-service-template

Igoods service template
Ruby
4
star
41

excess.zsh-theme

Simple zsh color theme
4
star
42

davydovanton.github.io

HTML
4
star
43

grape-rodauth

Simple grape app with rodauth
Ruby
4
star
44

kaminari-hanami

This repository is no longer maintain: Kaminari integration for @hanami
Ruby
4
star
45

yeelight-lamp-client

Ruby client for yeelight lamps
Ruby
4
star
46

pepe

Pepe for you ruby console
Ruby
3
star
47

dry-http-client

This repository is no longer maintain: Fundctional HTTP client based on dry stack.
Ruby
3
star
48

rubyunderhood

Коллективный твиттер-аккаунт для ruby разработчиков с новым автором каждую неделю.
3
star
49

hanami-action-documentation

Automatically generate documentation for you actions
Ruby
2
star
50

system-analysis-example-system

2
star
51

event_sourcing_ruby

Simple repository for playing with event sourcing conceptions from F#
Ruby
2
star
52

hanami-workshop

Simple hanami app for workshop
Ruby
2
star
53

lotus-webpack-reactjs

Simple lotus app with webpack and react js
JavaScript
2
star
54

hanami-graphql-example

Simple example of GQL in hanami application
Ruby
2
star
55

arch-katas-solution

Personal repository for arch kata solutions (rus)
1
star
56

hanami-auth

This repository is no longer maintain. Prototype for hanami auth (bad way)
Ruby
1
star
57

hanami-interactor-matcher

This repository is no longer maintain: Simple dry-matcher for hanami interactor
Ruby
1
star
58

service-separation-checklist

Simple checklist for service separation
1
star
59

bruevich

Ruby
1
star
60

momiji

Post framework
Ruby
1
star
61

hanami-operation-generator

Simple library for generating operations for hanami-dry-system project
Ruby
1
star
62

data_matrix

simple way for creating grids for any data objects and for any frameworks
Ruby
1
star