LoginSignup
8
8

More than 5 years have passed since last update.

authority gem の Authozier メソッドの優先順位について

Posted at

基本的には下記のページの "The flow of Authority" に書かれている通り、ApplicationAuthorizer を基底クラスとして継承した下位のクラスから該当するメソッドがあるかどうか判断される
https://github.com/nathanl/authority

しかし、authority gem で拡張される can_xxxx? メソッドには instance を渡せるケースがあり、その場合は上記の判定順とは少し違う順番に該当メソッドがあるかどうか判定される。

具体的には、ドキュメントのページの例で Article クラスそのものではなく Article.first などのインスタンスを渡した場合、下記のような順番でメソッドが探索される

current_user.can_create?(Article.first)
                           +
                           |
                           v
Article#creatable_by?(current_user)
                           +
                           |
                           v
AdminAuthorizer#creatable_by?(current_user)
                           +
                           |
                           v
ApplicationAuthorizer#creatable_by?(current_user) # 元の図には無いけれど
                           +
                           |
                           v
Article.creatable_by?(current_user)
                           +
                           |
                           v
AdminAuthorizer.creatable_by?(current_user)
                           +
                           |
                           v
AdminAuthorizer.default(:creatable, current_user)
                           +
                           |
                           v
ApplicationAuthorizer.creatable_by?(current_user) # こっちも元の図には無い

まとめてみれば、どうということはなく、単純に instance => class メソッドの順に探索されるようである。
が、このルールを把握していなければ、少し直感的ではない判定順になってしまうので、まとめてみました。
(authority に関する情報なさそうだし

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8