{"id":15018967,"url":"https://github.com/soundcloud/cando","last_synced_at":"2025-10-19T16:32:15.423Z","repository":{"id":17017799,"uuid":"19781763","full_name":"soundcloud/cando","owner":"soundcloud","description":"A simple access rights gem with users, roles and capabilities","archived":false,"fork":false,"pushed_at":"2023-06-09T21:57:26.000Z","size":67,"stargazers_count":22,"open_issues_count":5,"forks_count":4,"subscribers_count":122,"default_branch":"master","last_synced_at":"2025-01-29T21:23:45.356Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soundcloud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-05-14T14:07:12.000Z","updated_at":"2023-10-30T15:18:15.000Z","dependencies_parsed_at":"2023-02-10T19:15:24.358Z","dependency_job_id":null,"html_url":"https://github.com/soundcloud/cando","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soundcloud%2Fcando","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soundcloud%2Fcando/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soundcloud%2Fcando/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soundcloud%2Fcando/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soundcloud","download_url":"https://codeload.github.com/soundcloud/cando/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237172115,"owners_count":19266614,"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":[],"created_at":"2024-09-24T19:52:40.873Z","updated_at":"2025-10-19T16:32:10.146Z","avatar_url":"https://github.com/soundcloud.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CanDo\n\n[![Build Status](https://travis-ci.org/soundcloud/cando.svg?branch=master)](https://travis-ci.org/soundcloud/cando)\n[![Dependency Status](https://gemnasium.com/soundcloud/cando.svg)](https://gemnasium.com/soundcloud/cando)\n[![Gem Version](https://badge.fury.io/rb/cando.svg)](http://badge.fury.io/rb/cando)\n\nCanDo is a small gem to implement a simple user access system based on users, roles \u0026\ncapabilites, where:\n\n  - each user can have 0, 1 or many roles\n  - each role can have 0, 1 or many capabilites\n\nUsers have capabilities by getting roles assigned (role == collection of\ncapabilities). Within the code, the `can` helper method can be used to test\nwhether a user has a certain capability or not (see below for a working code example).\n\n  - [Usage example](#usage-example)\n  - [Installation](#installation)\n  - [Configuration and usage](#configuration-and-usage)\n    - [Database setup \u0026 configuration](#database-setup--configuration)\n    - [Init CanDo](#init-cando)\n    - [Using CanDo rake tasks](#using-rake-tasks)\n  - [Using CanDo in your project's code](#using-cando-in-your-projects-code)\n    - [Api](#api)\n  - [Versioning](#versioning)\n  - [Licensing](#licensing)\n\n#### Usage example\nUsing the CanDo in your code (working code with an empty database):\n\n    require 'cando'\n    include CanDo\n\n    CanDo.init do\n      # if passed, this will be executed if the user does not have the\n      # asked-for capability (only applies if 'can' is passed a block)\n      cannot_block do |user_urn, capability|\n        raise \"#{user_urn} can not #{capability} .. user capabilities are: #{capabilities(user_urn)}\"\n      end\n\n      connect \"mysql://cando_user:cando_passwd@host:port/database\"\n    end\n\n    # if the role or a capability does not exist, it'll be created\n    define_role(\"r1\", [\"capability1\", \"capability3\"])\n    define_role(\"r2\", [\"capability2\"])\n    define_role(\"r3\", [\"capability3\"])\n\n    # if the user does not exist, he'll be created -- the roles must be available\n    assign_roles(\"user1\", [\"r1\"])\n    # add other roles by unifying arrays with '|'\n    assign_roles(\"user1\", roles(\"user1\") | [\"r2\",\"r3\"])\n\n    assign_roles(\"user2\", [\"r1\"])\n\n    # use 'can' block syntax\n    can(\"user1\", :capability1) do\n      puts \"user has capability1\"\n    end\n\n    # this will raise an exception as declared in the init block\n    can(\"user1\", :super_admin) do\n      puts \"hey hoh\" # this will not be printed\n    end\n\n    # when no block is given, 'can' returns true or false/nil\n    unless can(\"user2\", :capability2)\n      puts \"user does not have capability1\"\n    end\n\n## Installation\n\nDownload and install rake with the following.\n\n        gem install cando\n\n## Configuration and usage\n\n### Database setup \u0026 configuration\nIf you want to use an individual database for cando, create the db + credentials (adjust values):\n\n    CREATE DATABASE IF NOT EXISTS cando  DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;\n    GRANT ALL ON `cando`.* to 'cando_user'@'localhost' identified by 'cando_passwd';\n\nWhenever you want to use a CanDo rake task, you need to set the database config via the env var `$CANDO_DB`:\n\n     export CANDO_DB=mysql://cando_user:cando_passwd@localhost/cando\n\nfor other dbs, [see the sequel\ndocumentation](http://sequel.jeremyevans.net/rdoc/classes/Sequel.html#method-c-connect).\n**Note that you will have to require the gem for your respective dbms, i.e. the\n`mysql`-gem for mysql, the `sqlite3`-gem for sqlite, etc. **\n\n### Init cando\nCando provides a rake task to get you started. This will setup the necessary\ntables (they are all prefixed with `cando` and thus should not interfere with\nyour database.\n\n    rake cando:init\n\n### Using rake tasks\nCando provides several useful rake tasks for easy cli-based operations. In order\nto use those edit (or create) the `Rakefile` and include\n\n    require 'cando'\n\n To get an overview, execute:\n\n     $ rake -T cando\n     rake cando:init     # Initialize cando (creates schema and runs migration)\n\n     rake cando:list     # List roles\n     rake cando:add      # Add a new role (pass in role name and capabilities with role=\u003cname\u003e capabilities=\u003ccap1\u003e,\u003ccap2\u003e,...\n     rake cando:update   # Update role (pass in role name and capabilities with role=\u003cname\u003e capabilities=\u003ccap1\u003e,\u003ccap2\u003e,...\n     rake cando:remove   # Remove role (pass in role name with role=\u003cname\u003e)\n\n     rake cando:assign   # Assign role to user (args: roles=\u003cr1\u003e,\u003cr2\u003e,\u003crn\u003e user=\u003cuser_urn\u003e)\n     rake cando:users    # List users and their roles\n\n### Using CanDo in your project's code\n\n#### Api\nPlease see [the api documentation](http://rubydoc.info/gems/cando/CanDo) for up to date documentation.\n\n connect to db (usually called within init block):\n\n    CanDo.connect \"mysql://user:passwd@host:port/database\"\n\n create a role; capabilities will be created if they don't exist yet:\n\n    define_role(\"role_name\", [\"capability1\",\"capability2\", \"capability3\", ...])\n\n assign role(s) to a user: if no user with that id exist, a new one will\n be created; if a role does not exist, an exception is raised. You can pass\n in a role object or just role names; pass in empty array to remove all roles:\n\n    role3 = CanDo::Role.first\n    assign_roles(\"user1\", [\"role1\", \"role2\", role3])\n\n get user's capabilities; returns an array of strings:\n\n    capabilities(\"user1\")\n\n get user's roles; returns an array of strings:\n\n    roles(\"user1\")\n\n set default handler if `can(\"u\",\"c\"){ ... }` fails (usually called within init block):\n\n    CanDo.cannot_block { |user_urn, capability| raise \"#{user_urn} is missing #{capability}\" }\n\n guard block by `can` function; will execute `cannot_block` if user is missing\n this capability (see example below):\n\n    can(\"user1\", :capability1) do\n      puts \"woohoo\"\n    end\n\n use `can` function as an expression -- `cannot_block` will not be executed if\n expressions resolves to `false`:\n\n    if can(\"user1\", :capability1)\n       puts \"woohoo\"\n    else\n       puts \":(\"\n    end\n\n\n## Versioning\nCanDo adheres to Semantic Versioning 2.0.0. If there is a violation of\nthis scheme, report it as a bug.Specifically, if a patch or minor version is\nreleased and breaks backward compatibility, that version should be immediately\nyanked and/or a new version should be immediately released that restores\ncompatibility. Any change that breaks the public API will only be introduced at\na major-version release. As a result of this policy, you can (and should)\nspecify any dependency on \u003cproject name\u003e by using the Pessimistic Version\nConstraint with two digits of precision.\n\n## Licensing\n\nSee the [LICENSE](LICENSE.md) file for details.\n\nThe MIT License (MIT)\n\nCopyright \u0026copy; 2014 Daniel Bornkessel\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoundcloud%2Fcando","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoundcloud%2Fcando","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoundcloud%2Fcando/lists"}