{"id":20252753,"url":"https://github.com/pragmarb/pragma-policy","last_synced_at":"2025-04-10T23:23:49.673Z","repository":{"id":56888639,"uuid":"73838482","full_name":"pragmarb/pragma-policy","owner":"pragmarb","description":"Resource authorization as simple as POROs.","archived":false,"fork":false,"pushed_at":"2020-01-08T22:46:21.000Z","size":51,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-24T12:25:26.260Z","etag":null,"topics":["api","authorization","ecosystem","policy","pragma","ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":"","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/pragmarb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2016-11-15T17:30:14.000Z","updated_at":"2020-01-08T22:46:23.000Z","dependencies_parsed_at":"2022-08-20T23:40:50.848Z","dependency_job_id":null,"html_url":"https://github.com/pragmarb/pragma-policy","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pragmarb%2Fpragma-policy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pragmarb%2Fpragma-policy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pragmarb%2Fpragma-policy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pragmarb%2Fpragma-policy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pragmarb","download_url":"https://codeload.github.com/pragmarb/pragma-policy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248313354,"owners_count":21082845,"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":["api","authorization","ecosystem","policy","pragma","ruby","ruby-on-rails"],"created_at":"2024-11-14T10:19:17.554Z","updated_at":"2025-04-10T23:23:49.646Z","avatar_url":"https://github.com/pragmarb.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pragma::Policy\n\n[![Build Status](https://travis-ci.org/pragmarb/pragma-policy.svg?branch=master)](https://travis-ci.org/pragmarb/pragma-policy)\n[![Coverage Status](https://coveralls.io/repos/github/pragmarb/pragma-policy/badge.svg?branch=master)](https://coveralls.io/github/pragmarb/pragma-policy?branch=master)\n[![Maintainability](https://api.codeclimate.com/v1/badges/e51e8d7489eb72ab97ba/maintainability)](https://codeclimate.com/github/pragmarb/pragma-policy/maintainability)\n\nPolicies provide fine-grained access control for your API resources.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'pragma-policy'\n```\n\nAnd then execute:\n\n```console\n$ bundle\n```\n\nOr install it yourself as:\n\n```console\n$ gem install pragma-policy\n```\n\n## Usage\n\nTo create a policy, simply inherit from `Pragma::Policy::Base`:\n\n```ruby\nmodule API\n  module V1\n    module Article\n      class Policy \u003c Pragma::Policy::Base\n      end\n    end\n  end\nend\n```\n\nBy default, the policy does not return any objects when scoping and forbids all operations.\n\nYou can start customizing your policy by defining a scope and operation predicates:\n\n```ruby\nmodule API\n  module V1\n    module Article\n      class Policy \u003c Pragma::Policy::Base\n        class Scope \u003c Pragma::Policy::Base::Scope\n          def resolve\n            scope.where('published = ? OR author_id = ?', true, user.id)\n          end\n        end\n\n        def show?\n          record.published? || record.author_id == user.id\n        end\n\n        def update?\n          record.author_id == user.id\n        end\n\n        def destroy?\n          record.author_id == user.id\n        end\n      end\n    end\n  end\nend\n```\n\nYou are ready to use your policy!\n\n### Retrieving records\n\nTo retrieve all the records accessible by a user, use the `.accessible_by` class method:\n\n```ruby\nposts = API::V1::Article::Policy::Scope.new(user, Article.all).resolve\n```\n\n### Authorizing operations\n\nTo authorize an operation, first instantiate the policy, then use the predicate methods:\n\n```ruby\npolicy = API::V1::Article::Policy.new(user, post)\nfail 'You cannot update this post!' unless policy.update?\n```\n\nSince raising when the operation is forbidden is so common, we provide bang methods a shorthand\nsyntax. `Pragma::Policy::NotAuthorizedError` is raised if the predicate method returns `false`:\n\n```ruby\npolicy = API::V1::Article::Policy.new(user, post)\npolicy.update! # raises if the user cannot update the post\n```\n\n### Reusing Pundit policies\n\nIf you already use [Pundit](https://github.com/varvet/pundit), there's no need to copy-paste\npolicies for your API. You can use `Pragma::Policy::Pundit` to delegate to your existing policies\nand scopes:\n\n```ruby\nmodule API\n  module V1\n    module Article\n      class Policy \u003c Pragma::Pundit::Policy\n        # This is optional: the inferred default would be ArticlePolicy.\n        self.pundit_klass = CustomArticlePolicy\n      end\n    end\n  end\nend\n```\n\nNote that you can still override specific methods if you want, and we'll keep delegating the rest\nto Pundit:\n\n```ruby\nmodule API\n  module V1\n    module Article\n      class Policy \u003c Pragma::Pundit::Policy\n        def create?\n          # Your custom create policy here\n        end\n      end\n    end\n  end\nend\n```\n\n### Passing additional context\n\nIf you want to pass additional context to the policy, just pass it instead of the user object.\nPragma::Policy never uses your context in any way, so you can pass whatever you want:\n\n```ruby\npolicy = API::V1::Article::Policy.new(OpenStruct.new(ip: request.remote_ip, user: user), post)\npolicy.update!\n```\n\nIn your policy, you can use `#context` as an alias for `#user` for convenience:\n\n```ruby\nmodule API\n  module V1\n    module Article\n      class Policy \u003c Pragma::Pundit::Policy\n        def update?\n          record.author_id == context.user.id || context.ip == '127.0.0.1'\n        end\n      end\n    end\n  end\nend\n```\n\nIf you are using [pragma-rails](https://github.com/pragmarb/pragma-rails), you may change the\ncontext passed to the policy by defining a `#policy_context` method on your controller. This way you\nare not forced to override `#current_user` or `#pragma_user`:\n\n```ruby\nmodule API\n  module V1\n    class PostsController \u003c ApplicationController\n      # ...\n\n      private\n\n      def policy_context\n        OpenStruct.new(ip: request.remote_ip, user: current_user)\n      end\n    end\n  end\nend\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/pragmarb/pragma-policy.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpragmarb%2Fpragma-policy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpragmarb%2Fpragma-policy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpragmarb%2Fpragma-policy/lists"}