{"id":13427673,"url":"https://github.com/RolifyCommunity/rolify","last_synced_at":"2025-03-16T00:31:51.636Z","repository":{"id":1539609,"uuid":"1844503","full_name":"RolifyCommunity/rolify","owner":"RolifyCommunity","description":"Role management library with resource scoping","archived":false,"fork":false,"pushed_at":"2023-10-18T20:50:00.000Z","size":729,"stargazers_count":3151,"open_issues_count":149,"forks_count":402,"subscribers_count":56,"default_branch":"master","last_synced_at":"2024-09-07T00:15:14.947Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://rolifycommunity.github.io/rolify/","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"wansti/StratumsphereStatusWidget","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RolifyCommunity.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.rdoc","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2011-06-03T20:54:40.000Z","updated_at":"2024-09-03T23:42:35.000Z","dependencies_parsed_at":"2023-07-05T15:46:21.464Z","dependency_job_id":"0a581dd9-782b-4a1c-8a61-2e402a2b2e92","html_url":"https://github.com/RolifyCommunity/rolify","commit_stats":{"total_commits":654,"total_committers":92,"mean_commits":7.108695652173913,"dds":0.3379204892966361,"last_synced_commit":"55261dcc26ae0bbca90a657e9449c65095924465"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RolifyCommunity%2Frolify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RolifyCommunity%2Frolify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RolifyCommunity%2Frolify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RolifyCommunity%2Frolify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RolifyCommunity","download_url":"https://codeload.github.com/RolifyCommunity/rolify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221631805,"owners_count":16855012,"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-07-31T01:00:36.615Z","updated_at":"2024-10-27T05:30:18.565Z","avatar_url":"https://github.com/RolifyCommunity.png","language":"Ruby","readme":"# rolify [![Gem Version](https://badge.fury.io/rb/rolify.svg)](http://badge.fury.io/rb/rolify) [![build status](https://travis-ci.org/RolifyCommunity/rolify.svg)](http://travis-ci.org/RolifyCommunity/rolify) [![Code Climate](https://codeclimate.com/github/RolifyCommunity/rolify.svg)](https://codeclimate.com/github/RolifyCommunity/rolify) [![Coverage Status](https://coveralls.io/repos/RolifyCommunity/rolify/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/RolifyCommunity/rolify?branch=master)\n\nVery simple Roles library without any authorization enforcement supporting scope on resource object.\n\nLet's see an example:\n\n```ruby\nuser.has_role?(:moderator, @forum)\n=\u003e false # if user is moderator of another Forum\n```\n\nThis library can be easily integrated with any authentication gem ([devise](https://github.com/plataformatec/devise), [Authlogic](https://github.com/binarylogic/authlogic), [Clearance](https://github.com/thoughtbot/clearance)) and authorization gem\u003cspan style=\"color: red\"\u003e\u003cstrong\u003e*\u003c/strong\u003e\u003c/span\u003e ([CanCanCan](https://github.com/CanCanCommunity/cancancan), [authority](https://github.com/nathanl/authority), [Pundit](https://github.com/elabs/pundit))\n\n\u003cspan style=\"color: red\"\u003e\u003cstrong\u003e*\u003c/strong\u003e\u003c/span\u003e: authorization gem that doesn't provide a role class\n\n## Requirements\n\n* Rails \u003e= 4.2\n* ActiveRecord \u003e= 4.2 \u003cb\u003eor\u003c/b\u003e Mongoid \u003e= 4.0\n* supports ruby 2.2+, JRuby 1.6.0+ (in 1.9 mode) and Rubinius 2.0.0dev (in 1.9 mode)\n* support of ruby 1.8 has been dropped due to Mongoid \u003e=3.0 that only supports 1.9 new hash syntax\n\n## Installation\n\nAdd this to your Gemfile and run the `bundle` command.\n\n```ruby\ngem \"rolify\"\n```\n\n## Getting Started\n\n### 1. Generate Role Model\n\nFirst, use the generator to setup Rolify. Role and User class are the default names. However, you can specify any class name you want. For the User class name, you would probably use the one provided by your authentication solution.\n\nIf you want to use Mongoid instead of ActiveRecord, just add `--orm=mongoid` argument, and skip to step #3.\n\n```\nrails g rolify Role User\n```\n\n**NB** for versions of Rolify prior to 3.3, use:\n\n```\nrails g rolify:role Role User\n```\n\nThe generator will create your Role model, add a migration file, and update your User class with new class methods.\n\n### 2. Run the migration (only required when using ActiveRecord)\n\nLet's migrate!\n\n```\nrake db:migrate\n```\n\n### 3.1 Configure your user model\n\nThis gem adds the `rolify` method to your User class. You can also specify optional callbacks on the User class for when roles are added or removed:\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  rolify :before_add =\u003e :before_add_method\n\n  def before_add_method(role)\n    # do something before it gets added\n  end\nend\n```\n\nThe `rolify` method accepts the following callback options:\n\n- `before_add`\n- `after_add`\n- `before_remove`\n- `after_remove`\n\nMongoid callbacks are also supported and works the same way.\n\nThe `rolify` method also accepts the `inverse_of` option if you need to disambiguate the relationship.\n\n### 3.2 Configure your resource models\n\nIn the resource models you want to apply roles on, just add ``resourcify`` method.\nFor example, on this ActiveRecord class:\n\n```ruby\nclass Forum \u003c ActiveRecord::Base\n  resourcify\nend\n```\n\n### 3.3 Assign default role\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  after_create :assign_default_role\n\n  def assign_default_role\n    self.add_role(:newuser) if self.roles.blank?\n  end\nend\n```\n\n### 4. Add a role to a user\n\nTo define a global role:\n\n```ruby\nuser = User.find(1)\nuser.add_role :admin\n```\n\nTo define a role scoped to a resource instance:\n\n```ruby\nuser = User.find(2)\nuser.add_role :moderator, Forum.first\n```\n\nTo define a role scoped to a resource class:\n\n```ruby\nuser = User.find(3)\nuser.add_role :moderator, Forum\n```\n\nRemove role:\n```ruby\nuser = User.find(3)\nuser.remove_role :moderator\n```\n\nThat's it!\n\n### 5. Role queries\n\nTo check if a user has a global role:\n\n```ruby\nuser = User.find(1)\nuser.add_role :admin # sets a global role\nuser.has_role? :admin\n=\u003e true\n```\n\nTo check if a user has a role scoped to a resource instance:\n\n```ruby\nuser = User.find(2)\nuser.add_role :moderator, Forum.first # sets a role scoped to a resource instance\nuser.has_role? :moderator, Forum.first\n=\u003e true\nuser.has_role? :moderator, Forum.last\n=\u003e false\n```\n\nTo check if a user has a role scoped to a resource class:\n\n```ruby\nuser = User.find(3)\nuser.add_role :moderator, Forum # sets a role scoped to a resource class\nuser.has_role? :moderator, Forum\n=\u003e true\nuser.has_role? :moderator, Forum.first\n=\u003e true\nuser.has_role? :moderator, Forum.last\n=\u003e true\n```\n\nA global role overrides resource role request:\n\n```ruby\nuser = User.find(4)\nuser.add_role :moderator # sets a global role\nuser.has_role? :moderator, Forum.first\n=\u003e true\nuser.has_role? :moderator, Forum.last\n=\u003e true\n```\n\nTo check if a user has the exact role scoped to a resource class:\n\n```ruby\nuser = User.find(5)\nuser.add_role :moderator # sets a global role\nuser.has_role? :moderator, Forum.first\n=\u003e true\nuser.has_strict_role? :moderator, Forum.last\n=\u003e false\n```\n\n### 6. Resource roles querying\n\nStarting from rolify 3.0, you can search roles on instance level or class level resources.\n\n#### Instance level\n\n```ruby\nforum = Forum.first\nforum.roles\n# =\u003e [ list of roles that are only bound to forum instance ]\nforum.applied_roles\n# =\u003e [ list of roles bound to forum instance and to the Forum class ]\n```\n\n#### Class level\n\n```ruby\nForum.with_role(:admin)\n# =\u003e [ list of Forum instances that have role \"admin\" bound to them ]\nForum.without_role(:admin)\n# =\u003e [ list of Forum instances that do NOT have role \"admin\" bound to them ]\nForum.with_role(:admin, current_user)\n# =\u003e [ list of Forum instances that have role \"admin\" bound to them and belong to current_user roles ]\nForum.with_roles([:admin, :user], current_user)\n# =\u003e [ list of Forum instances that have role \"admin\" or \"user\" bound to them and belong to current_user roles ]\n\nUser.with_any_role(:user, :admin)\n# =\u003e [ list of User instances that have role \"admin\" or \"user\" bound to them ]\nUser.with_role(:site_admin, current_site)\n# =\u003e [ list of User instances that have a scoped role of \"site_admin\" to a site instance ]\nUser.with_role(:site_admin, :any)\n# =\u003e [ list of User instances that have a scoped role of \"site_admin\" for any site instances ]\nUser.with_all_roles(:site_admin, :admin)\n# =\u003e [ list of User instances that have a role of \"site_admin\" and a role of \"admin\" bound to it ]\n\nForum.find_roles\n# =\u003e [ list of roles that are bound to any Forum instance or to the Forum class ]\nForum.find_roles(:admin)\n# =\u003e [ list of roles that are bound to any Forum instance or to the Forum class, with \"admin\" as a role name ]\nForum.find_roles(:admin, current_user)\n# =\u003e [ list of roles that are bound to any Forum instance, or to the Forum class with \"admin\" as a role name, and belongs to current_user ]\n```\n\n### Strict Mode\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  rolify strict: true\nend\n\n@user = User.first\n\n@user.add_role(:forum, Forum)\n@user.add_role(:forum, Forum.first)\n\n@user.has_role?(:forum, Forum) #=\u003e true\n@user.has_role?(:forum, Forum.first) #=\u003e true\n@user.has_role?(:forum, Forum.last) #=\u003e false\n```\nI.e. you get true only on a role that you manually add.\n\n### Cached Roles (to avoid N+1 issue)\n\n```ruby\n@user.add_role :admin, Forum\n@user.add_role :member, Forum\n\nusers = User.with_role(:admin, Forum).preload(:roles)\nusers.each do |user|\n  user.has_cached_role?(:member, Forum) # no extra queries\nend\n```\n\nThis method should be used with caution. If you don't preload the roles, the `has_cached_role?` might return `false`. In the above example, it would return `false` for `@user.has_cached_role?(:member, Forum)`, because `User.with_role(:admin, Forum)` will load only the `:admin` roles.\n\n## Resources\n\n* [Wiki](https://github.com/RolifyCommunity/rolify/wiki)\n* [Usage](https://github.com/RolifyCommunity/rolify/wiki/Usage): all the available commands\n* [Tutorials](https://github.com/RolifyCommunity/rolify/wiki#wiki-tutorials):\n  * [How-To use rolify with Devise and CanCanCan](https://github.com/RolifyCommunity/rolify/wiki/Devise---CanCanCan---rolify-Tutorial)\n  * [Using rolify with Devise and Authority](https://github.com/RolifyCommunity/rolify/wiki/Using-rolify-with-Devise-and-Authority)\n  * [Step-by-step tutorial](http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html) provided by [RailsApps](http://railsapps.github.com/)\n\n## Upgrade from previous versions\n\nPlease read the [upgrade instructions](UPGRADE.rdoc).\n\n## Known issues\n\n* If you are using Mongoid and/or less-rails gem, please read [this](https://github.com/RolifyCommunity/rolify/wiki/FAQ#when-i-start-rails-using-server-console-whatever-i-get-this-error)\n* Moped library (ruby driver for Mongodb used by Mongoid) doesn't support rubinius 2.2 yet (see https://github.com/mongoid/moped/issues/231)\n* If you use Rails 4 and Mongoid, use Mongoid ~\u003e 4. rolify is fully tested with Rails 4 and Mongoid 4.\n\n## Questions or Problems?\n\nIf you have any issue or feature request with/for rolify, please create an new [issue on GitHub](https://github.com/RolifyCommunity/rolify/issues) **specifying the ruby runtime, rails and rolify versions you're using and the gems listed in your Gemfile**, or fork the project and send a pull request.\n\nTo get the specs running you should call `bundle` and then `rake`. See the spec/README for more information.\n","funding_links":[],"categories":["User","Security","Ruby","用户","Authorization"],"sub_categories":["Authorization","Rails Authorization","授权"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRolifyCommunity%2Frolify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRolifyCommunity%2Frolify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRolifyCommunity%2Frolify/lists"}