{"id":13778090,"url":"https://github.com/davydovanton/kan","last_synced_at":"2025-04-04T17:08:29.524Z","repository":{"id":55134153,"uuid":"115924646","full_name":"davydovanton/kan","owner":"davydovanton","description":"Simple, functional authorization library and role management for ruby","archived":false,"fork":false,"pushed_at":"2020-03-12T16:30:17.000Z","size":104,"stargazers_count":232,"open_issues_count":13,"forks_count":12,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-30T05:57:25.070Z","etag":null,"topics":["authorization","kan","roles","ruby"],"latest_commit_sha":null,"homepage":"http://www.kanrb.org","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davydovanton.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-01T14:34:36.000Z","updated_at":"2024-06-13T15:46:43.000Z","dependencies_parsed_at":"2022-08-14T13:00:42.930Z","dependency_job_id":null,"html_url":"https://github.com/davydovanton/kan","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fkan","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fkan/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fkan/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davydovanton%2Fkan/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davydovanton","download_url":"https://codeload.github.com/davydovanton/kan/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247217184,"owners_count":20903009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authorization","kan","roles","ruby"],"created_at":"2024-08-03T18:00:51.118Z","updated_at":"2025-04-04T17:08:29.502Z","avatar_url":"https://github.com/davydovanton.png","language":"Ruby","funding_links":["https://opencollective.com/kan"],"categories":["Hanami Gem List","Ruby"],"sub_categories":["Authorization"],"readme":"# Kan\n[![Build Status](https://travis-ci.org/davydovanton/kan.svg?branch=master)](https://travis-ci.org/davydovanton/kan)\n[![Backers on Open Collective](https://opencollective.com/kan/backers/badge.svg)](#backers)\n [![Sponsors on Open Collective](https://opencollective.com/kan/sponsors/badge.svg)](#sponsors)\n\nSimple functional authorization library for ruby. Inspired by [transproc](https://github.com/solnic/transproc) and [dry project](http://dry-rb.org)\n\n## Table of context\n\n* [Installation](#installation)\n* [Usage](#usage)\n* [Contributing](#contributing)\n* [License](#license)\n* [Code of Conduct](#code-of-conduct)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'kan'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install kan\n\n## Usage\n\nSee [User Documentation page](http://kanrb.org/)\n\n* [Basic Usage](http://kanrb.org/basic_usage)\n* [Roles](http://kanrb.org/roles)\n* [Testing](http://kanrb.org/testing)\n* [Dry integration](http://kanrb.org/working_with_dry)\n* [F.A.Q.](http://kanrb.org/faq)\n\n## Basic Usage\n\n### Register abilities\n\n```ruby\nclass Post::Abilities\n  include Kan::Abilities\n\n  register('read') { |_, _| true }\n  register('edit') { |user, post| user.id == post.user_id }\n  register('delete') { |_, _| false }\nend\n```\n\nAlso, you can register more than one ability in one place and use string or symbol keys:\n\n```ruby\nclass Post::AdminAbilities\n  include Kan::Abilities\n\n  register(:read, :edit, :delete) { |user, _| user.admin? }\nend\n\nclass Comments::Abilities\n  include Kan::Abilities\n\n  register('read') { |_, _| true }\n  register('edit') { |user, _| user.admin? }\n\n  register(:delete) do |user, comment|\n    user.id == comment.user_id \u0026\u0026 comment.created_at \u003c Time.now + TEN_MINUTES\n  end\nend\n```\n\n### Check abilities\n\n```ruby\nabilities = Kan::Application.new(\n  post: Post::Abilities.new,\n  comment: Comments::Abilities.new\n)\n\nabilities['post.read'].call(current_user, post) # =\u003e true\nabilities['post.delete'].call(current_user, post) # =\u003e false\nabilities['comment.delete'].call(current_user, post) # =\u003e false\n```\n\n#### Default ability block\n\nBy default Kan use `proc { true }` as a default ability block:\n\n```ruby\nabilities['comment.invalid'].call(current_user, post) # =\u003e true\n```\n\nBut you can rewrite it\n\n```ruby\nadmin_abilities = Kan::Application.new(\n  post: Post::AdminAbilities.new(default_ability_block: proc { false }),\n  comment: Comments::Abilities.new,\n)\n\nadmin_abilities['post.delete'].call(current_user, post)  # =\u003e false\nadmin_abilities['post.delete'].call(admin_user, post)    # =\u003e true\nadmin_abilities['post.invalid'].call(current_user, post) # =\u003e false\n```\n\n#### List of abilities\nYou can provide array of abilities for each scope and Kan will return `true` if at least one ability return `true`:\n\n```ruby\nglobal_abilities = Kan::Application.new(\n  post: [Post::Abilities.new, Post::AdminAbilities.new],\n  comment: Comments::Abilities.new\n)\n\nglobal_abilities['post.edit'].call(current_user, post) # =\u003e false\nglobal_abilities['post.edit'].call(owner_user, post)   # =\u003e true\nglobal_abilities['post.edit'].call(admin_user, post)   # =\u003e true\n```\n\n### Aliases\n\nYou can use strings or symbols and then use it as name of ability\n\n```ruby\nclass Post::Abilities\n  include Kan::Abilities\n\n  register(:edit) { |_, _| true }\n  register_alias(:correct, 'edit')\nend\n\nabilities = Kan::Application.new(\n  post: Post::Abilities.new\n)\n\nabilities['post.correct'].call(current_user, post) # =\u003e true\n```\n\n### Callback\n\nYou can provide callable object (that respond to #call) that accepts ability_name and payload params to `after_call_callback` param of your ability:\n```ruby\nadmin_abilities = Kan::Application.new(\n  post: Post::AdminAbilities.new(after_call_callback: -\u003e (ability_name, payload) { ... }),\n  comment: Comments::Abilities.new,\n)\n\nadmin_abilities['post.read'].call(current_user, post) # =\u003e false\n```\nYour object will be executed after calling ability.\n\n## Contributing\n\n### Code and features\n\nBug 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](http://contributor-covenant.org) code of conduct.\n\n### Docs\nJust send PR with changes in `docs/` folder.\n\n### How to instal the project\nJust clone repository and call:\n\n```\n$ bundle install\n$ bundle exec rspec\n```\n\n## Contributors\n\nThis project exists thanks to all the people who contribute.\n\u003ca href=\"https://github.com/davydovanton/kan/contributors\"\u003e\u003cimg src=\"https://opencollective.com/kan/contributors.svg?width=890\u0026button=false\" /\u003e\u003c/a\u003e\n\n\n## Backers\n\nThank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/kan#backer)]\n\n\u003ca href=\"https://opencollective.com/kan#backers\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/kan/backers.svg?width=890\"\u003e\u003c/a\u003e\n\n\n## Sponsors\n\nSupport this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/kan#sponsor)]\n\n\u003ca href=\"https://opencollective.com/kan/sponsor/0/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/kan/sponsor/0/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/kan/sponsor/1/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/kan/sponsor/1/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/kan/sponsor/2/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/kan/sponsor/2/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/kan/sponsor/3/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/kan/sponsor/3/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/kan/sponsor/4/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/kan/sponsor/4/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/kan/sponsor/5/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/kan/sponsor/5/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/kan/sponsor/6/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/kan/sponsor/6/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/kan/sponsor/7/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/kan/sponsor/7/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/kan/sponsor/8/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/kan/sponsor/8/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/kan/sponsor/9/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/kan/sponsor/9/avatar.svg\"\u003e\u003c/a\u003e\n\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Kan project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/davydovanton/kan/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavydovanton%2Fkan","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavydovanton%2Fkan","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavydovanton%2Fkan/lists"}