{"id":12027,"date":"2020-10-13T11:23:35","date_gmt":"2020-10-13T02:23:35","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=12027"},"modified":"2023-05-27T22:46:36","modified_gmt":"2023-05-27T13:46:36","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%80%8cpundit%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=12027","title":{"rendered":"\u3010Ruby on Rails\u3011\u300cpundit\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>pundit\u3068\u306f\uff1f<\/h2>\n<p>Rails\u306b\u304a\u3051\u308b\u8a8d\u53ef\u306e\u4ed5\u7d44\u307f\u3067\u3059\u3002<\/p>\n<h2>cancancan\u3068\u306e\u9055\u3044<\/h2>\n<p>cancancan\u306f\u30e6\u30fc\u30b6\u306b\u5bfe\u3057\u3066\u3001\u3069\u3093\u306a\u30a2\u30af\u30b7\u30e7\u30f3\u304c\u8a31\u53ef\u3059\u308b\u304b\u3092\u5b9a\u7fa9\u3059\u308b(\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u5bc4\u308a)\u306e\u306b\u5bfe\u3057\u3066\u3001Pundit\u3067\u306f\u30ea\u30bd\u30fc\u30b9\u306b\u5bfe\u3057\u3066\u8ab0\u304c\u8a31\u53ef\u3055\u308c\u308b\u306e\u304b\u3092\u5b9a\u7fa9\u3059\u308b(\u30e2\u30c7\u30eb\u5bc4\u308a)\u3002<\/p>\n<h2>\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9<\/h2>\n<h3>1.Pundit\u3092include\u3059\u308b\u3002<\/h3>\n<p>pundit\u3092\u4f7f\u3044\u305f\u3044\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3067pubdit\u3092include\u3057\u307e\u3059\u3002\u57fa\u672c\u7684\u306b\u306f<span style=\"color: #ff0000;\"><strong>base\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9<\/strong><\/span>\u307f\u305f\u3044\u306a\u7269\u3092\u4f5c\u3063\u3066\u305d\u3053\u306b\u5ba3\u8a00\u3059\u308b\u5f62\u304c\u591a\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true\">class \u4f7f\u3044\u305f\u3044\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u540d &lt; ActionController::Base\r\n  include Pundit\r\nend<\/pre>\n<h3>2.ApplicationPolicy\u3092\u4f5c\u6210\u3059\u308b\u3002<\/h3>\n<p>\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308c\u3070\u3001\u300capp\/policies\/\u300d<span class=\"s1\">\u914d\u4e0b\u306b\u300c<\/span>application_policy.rb\u300d\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">rails g pundit:install<\/pre>\n<p>\u751f\u6210\u3055\u308c\u308b\u5185\u5bb9\u306f\u4e0b\u8a18\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">class ApplicationPolicy\r\n  attr_reader :user, :record\r\n\r\n  def initialize(user, record)\r\n    @user = user\r\n    @record = record\r\n  end\r\n\r\n  def index?\r\n    false\r\n  end\r\nend<\/pre>\n<p>\u3053\u306eApplicationPolicy\u3092\u7d99\u627f\u3057\u3066\u500b\u3005\u306ePolicy\u3092\u4f5c\u6210\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<h4>initialize\u306e@user<\/h4>\n<p>current_user(\u30a2\u30af\u30bb\u30b9\u3057\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fc)\u304c\u5272\u308a\u5f53\u3066\u3089\u308c\u307e\u3059\u3002\u9006\u306b\u8a00\u3048\u3070\u3001\u30e2\u30c7\u30eb\u306b<span style=\"color: #ff0000;\"><strong>current_user<\/strong><\/span>\u3068\u3044\u3046\u5909\u6570\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u30e6\u30fc\u30b6\u30fc\u304c\u5272\u308a\u5f53\u3066\u3089\u308c\u3066\u3044\u306a\u3044\u3068\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u5272\u308a\u5f53\u3066\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<span style=\"color: #ff0000;\"><strong>\uff08\u57fa\u672c\u7684\u306b\u306fdevise\u3067\u4f7f\u308f\u308c\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fc\u5909\u6570\u304c\u30c7\u30d5\u30a9\u30eb\u30c8\u3067current_user\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u305d\u308c\u3067\u81ea\u52d5\u5bfe\u5fdc\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002\uff09<\/strong><\/span><\/p>\n<h4>initialize\u306e@record<\/h4>\n<p>\u5bfe\u5fdc\u3059\u308b\u30e2\u30c7\u30eb\uff08\u30ea\u30bd\u30fc\u30b9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u5272\u308a\u5f53\u3066\u307e\u3059\u3002<\/p>\n<h3>3.\u30dd\u30ea\u30b7\u30fc\u30d5\u30a1\u30a4\u30eb(\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u540dPolicy)\u3092\u4f5c\u6210\u3059\u308b\u3002<\/h3>\n<p>app\/policy\u30d5\u30a9\u30eb\u30c0\u306e\u76f4\u4e0b\u306b\u300c\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u540dPolicy.rb\u300d\u3068\u3044\u3046\u30dd\u30ea\u30b7\u30fc\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<h4>\u30a2\u30af\u30b7\u30e7\u30f3\u540d + ?<\/h4>\n<p>\u623b\u308a\u5024\u304ctrue\u306a\u3089\u8a8d\u53ef\u3001\u623b\u308a\u5024\u304cfalse\u306a\u3089\u62d2\u5426\u3068\u306a\u308a\u307e\u3059\u3002\u62d2\u5426\u3057\u305f\u5834\u5408\u306f\u3001\u300cPundit::NotAuthorizedError\u300d\u3068\u3044\u3046\u4f8b\u5916\u304c\u6295\u3052\u3089\u308c\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u300crecord.user_id == user.id\u300d<\/p>\n<div>\n<h3>4.\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3067\u5bfe\u8c61\u306e\u6a29\u9650\u304c\u3042\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3002<\/h3>\n<h4>authorize \u30e2\u30c7\u30eb\uff08\u30ea\u30bd\u30fc\u30b9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09<\/h4>\n<\/div>\n<p>\u5bfe\u8c61\u306e\u30ea\u30bd\u30fc\u30b9\u306b\u5bfe\u3057\u3066\u6a29\u9650\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\u3057\u3066\u304f\u308c\u307e\u3059\u3002\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3067\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<h2>Scope<\/h2>\n<p>ApplicationPolicy\u306e\u4e2d\u306b\u30c7\u30d5\u30a9\u30eb\u30c8\u3067<span style=\"color: #ff0000;\"><strong>Scope<\/strong><\/span>\u3068\u3044\u3046\u30af\u30e9\u30b9\u3082\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002\u7528\u9014\u3068\u3057\u3066\u306f\u5bfe\u8c61\u306e\u30e2\u30c7\u30eb\u3060\u3051\u3067\u306a\u304f<span style=\"color: #ff0000;\"><strong>\u30e2\u30c7\u30eb\u306e\u4e2d\u306b\u542b\u307e\u308c\u3066\u3044\u308b\u5c5e\u6027\u307e\u3067\u52a0\u5473\u3057\u3066\u8868\u793a\u3055\u305b\u308b\u30ea\u30bd\u30fc\u30b9\u3092\u5236\u5fa1\u3057\u305f\u3044\u5834\u5408<\/strong><\/span>\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002\uff08\u4f8b\u3048\u3070\u3001\u7ba1\u7406\u8005\u6a29\u9650\u3092\u6301\u3063\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fc\u306b\u5bfe\u3057\u3066\u306f\u7ba1\u7406\u8005\u60c5\u5831\u3082\u8868\u793a\u3055\u305b\u308b\u7b49\uff09<\/p>\n<pre class=\"lang:default decode:true\">  class Scope\r\n    attr_reader :user, :scope\r\n\r\n    def initialize(user, scope)\r\n      @user = user\r\n      @scope = scope\r\n    end\r\n\r\n    def resolve\r\n      scope.all\r\n    end\r\n  end<\/pre>\n<p>\u5177\u4f53\u7684\u306b\u306fresolve\u30e1\u30bd\u30c3\u30c9\u306b\u3066\u30e6\u30fc\u30b6\u30fc\u306e\u5c5e\u6027\u306b\u3088\u308b\u5206\u5c90\u3092\u3055\u305b\u307e\u3059\u3002(scope\u3068\u3044\u3046\u306e\u304c\u5168\u4f53\u306e\u60c5\u5831\u3067\u3059\u3002)<\/p>\n<pre class=\"lang:default decode:true\">def resolve\r\n  if \u7ba1\u7406\u8005\r\n    scope.all\r\n  else\r\n    scope.all.find(1)\r\n  end\r\nend<\/pre>\n<p>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u5074\u304b\u3089policy_scope\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u547c\u3073\u51fa\u3057\u307e\u3059\u3002\u5f15\u304d\u6e21\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u306b\u3088\u3063\u3066resolve\u30e1\u30bd\u30c3\u30c9\u306e\u7d50\u679c\u3092\u8fd4\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true  \">  def index\r\n    @users = policy_scope(User)\r\n  end<\/pre>\n","protected":false},"excerpt":{"rendered":"pundit\u3068\u306f\uff1f Rails\u306b\u304a\u3051\u308b\u8a8d\u53ef\u306e\u4ed5\u7d44\u307f\u3067\u3059\u3002 cancancan\u3068\u306e\u9055\u3044 cancancan\u306f\u30e6\u30fc\u30b6\u306b\u5bfe\u3057\u3066\u3001\u3069\u3093\u306a\u30a2\u30af\u30b7\u30e7\u30f3\u304c\u8a31\u53ef\u3059\u308b\u304b\u3092\u5b9a\u7fa9\u3059\u308b(\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u5bc4\u308a)\u306e\u306b\u5bfe\u3057\u3066\u3001Pundit\u3067\u306f\u30ea\u30bd\u30fc\u30b9\u306b\u5bfe [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[36],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12027"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12027"}],"version-history":[{"count":6,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12027\/revisions"}],"predecessor-version":[{"id":20508,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12027\/revisions\/20508"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12027"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}