Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Authorization

Rails Authorization

Ginza.rb 第31回 ユーザの権限管理どうしてます?発表資料
https://ginzarb.doorkeeper.jp/events/36898

kyuden/banken
https://github.com/kyuden/banken

Kyuden Masahiro

January 19, 2016
Tweet

More Decks by Kyuden Masahiro

Other Decks in Programming

Transcript

  1. RAILS
    AUTHORIZATION
    KYUDEN MASAHIRO
    kyuden_ kyuden

    View full-size slide

  2. ೔ຊޠTutorialΛWIKIʹ༻ҙ͠·ͨ͠


    View full-size slide

  3. ೔ຊޠTutorialΛWIKIʹ༻ҙ͠·ͨ͠


    View full-size slide

  4. ݖݶ؅ཧ
    Talking Theme

    View full-size slide

  5. ݖݶ؅ཧ͸ίʔυ͕༰қ͘ߥΕΔ

    View full-size slide

  6. ݖݶ؅ཧͷDBઃܭʹ͍ͭͯ͸
    ࿩͠·ͤΜ

    View full-size slide

  7. Why
    ݖݶ؅ཧͷDBઃܭ͸
    ཁ݅ʹΑͬͯ͞·͟·͔ͩΒ

    View full-size slide

  8. ຊ୊ɿݖݶʹΑΔController΍Viewͷ੍ޚ

    View full-size slide

  9. ControllerͱView͕ if ͰߥΕΔ

    View full-size slide

  10. ‣ ࣅͨΑ͏ͳೝՄ৚͕݅Controller΍ViewͳͲ޿͍ൣғʹࢄࡏ

    ‣ Modelʹݖݶ൑ఆΛدͤͯ΋ݖݶ͕૿͑ΔͱFat Modelʹ

    View full-size slide

  11. ʨ›`≡ŋŐŋ≡´ʩ›ớ ᵲᴸᴸᴸᵲ

    View full-size slide

  12. Choose CanCan/CanCanCan gem ?

    View full-size slide

  13. Architecture with CanCan/CanCanCan
    Controller A
    Controller B
    Controller C
    Ability
    ‣ Controller͝ͱʹఆ͍ٛͯͨ͠ೝՄ৚݅Λ෼཭͠

    1ͭͷAbilityΫϥεʹूதͤ͞Δ

    View full-size slide

  14. Design concept of CanCan/CanCanCan
    ‣ ݖݶ͝ͱʹ

    ֤ϦιʔεͱActionʹର͢ΔೝՄ৚݅Λఆٛ

    View full-size slide

  15. How to use CanCan/CanCanCan
    class HogesController < ApplicationController
    def index
    authorize! :index, Hoge

    View full-size slide

  16. Problem of CanCan/CanCanCan
    ‣ ݖݶ؅ཧ͕ෳࡶͳΞϓϦͰ͸AbilityΫϥε͕ංେԽ͠
    खʹෛ͑ͳ͘ͳΔ

    ‣ ͢΂ͯͷೝՄ৚͕݅ఆٛ͞ΕͨAbilityΫϥε͕ϦΫΤ
    ετຖʹධՁ͞ΕΔͨΊϨεϙϯε͕ଟগॏ͘ͳΔ

    (க໋తͰ͸ͳ͍͕ແବ͕ଟ͍)

    ‣ CanCanCan͕ఏڙ͢ΔೝՄ৚݅Λఆٛ͢ΔͨΊͷಠ
    ࣗDSL͕ෳࡶͰਏ͍֮͑ͨ͘͠ͳ͍


    View full-size slide

  17. ʨ΁ `≡ŋŐŋ≡´ʩ΁ ᵪᴸᴸᴸᵪ

    View full-size slide

  18. ʨ›`≡ŋŐŋ≡´ʩ›ớ ᵲᴸᴸᴸᵲ

    View full-size slide

  19. Choose Pundit gem ?

    View full-size slide

  20. Design concept of Pundit
    ‣ Ϧιʔεຖʹ

    Actionʹର͢ΔೝՄ৚݅Λఆٛ

    View full-size slide

  21. Architecture with Pundit
    ‣ ModelຖʹPolicyΫϥεΛ࡞੒͠Actionʹ
    ର͢ΔೝՄ৚݅Λఆ͍ٛͯ͘͠
    Model A

    A Policy

    Controller A
    update
    destroy
    update?
    destroy?

    View full-size slide

  22. How to use Pundit
    v

    View full-size slide

  23. Impression of Pundit (1)
    ‣ Punditͷ಺෦࣮૷͸RailsͷϞϯΩʔύον
    ͳͲͷ֦ுΛߦ͍ͬͯͳ͍ͷͰRailsͷόʔ
    δϣϯΞοϓʹΑΔӨڹΛड͚ʹ͍͘

    View full-size slide

  24. Impression of Pundit (2)
    ‣ CanCanCanͷΑ͏ͳೝՄ৚݅Λఆٛ͢Δಠ
    ࣗDSLΛ࢖ΘͣೝՄ৚݅Λఆٛ͢Δࣄ͕Ͱ
    ͖ΔɻPunditΛ࢖͏্Ͱඞཁͳͷ͸࠷খݶ
    ͷ࢖͍ํͱRubyྗ

    ‣ PolicyΫϥε͸୯ͳΔRubyͷΫϥεͰ͋Δ
    ͨΊϞδϡʔϧԽͨ͠Γܧঝͤͨ͞Γཁ݅
    ʹԠͯ͡ॊೈʹ࣮૷Ͱ͖Δ

    View full-size slide

  25. Impression of Pundit (3)
    ‣ ຊདྷɺݖݶ؅ཧͱ͸ʮAdminͳΒArticleϦ
    ιʔεͷߋ৽ΛೝՄ͢ΔʯͷΑ͏ʹϦιʔε
    ϕʔεͷཁ݅Ͱ͋ΔͨΊݖݶ؅ཧΛϞσϧد
    Γͷ੹຿ͱͯ͠ଊ͑ɺϦιʔε͝ͱʹೝՄ৚
    ݅Λఆ͍ٛͯ͘͠Ξϓϩʔν͸ͱͯ΋ࣗવɻ

    View full-size slide

  26. ʨ`≡ŋŐŋ≡´ʩὑ

    View full-size slide

  27. Problem of Pundit (1)
    ‣ Modelຖʹ࡞੒͢ΔPolicyΫϥεʹ͸Controllerͷ
    Actionʹඥͮ͘ϝιου(action + ?)Λ࣮૷͢Δͨ
    ΊɺPunditΛ࢖͏্Ͱ͸Model, Policy, Controller
    ͕1ର1ର1ͱ͍͏੍໿͕҉໧తʹੜ·ΕΔ
    Model A

    A Policy

    Controller A
    update
    destroy
    update?
    destroy?

    View full-size slide

  28. Problem of Pundit (2)
    ‣ جຊతʹ͸ArticleModelΛѻ͏ͷ͸
    ArticlesControllerͳͷͰArticlePolicy͕͋Ε͹ࣄ
    ଍ΓΔ

    ‣ ͕͔͠͠ɺΞϓϦ͕ෳࡶʹͳ͍ͬͯ͘ͱಛఆͷϞ
    σϧΛѻ͏Controller͕ෳ਺ଘࡏ͢Δ৔߹ɺͭ·Γ
    1ͭͷϞσϧʹର͠ෳ਺ͷPolicy͕ඞཁʹͳΔ৔߹
    ͕͋Γɺ͜ͷ࣌Punditར༻ऀ͸ԿΒ͔ͷ޻෉͢Δ
    ඞཁ͕͋Δɻ(ࠇຐज़ͰPunditΛὃ͢ɺϞϯΩʔ
    ύονΛ౰ͯΔɺPolicyΫϥεΛ޻෉͢ΔͳͲ)

    View full-size slide

  29. Problem of Pundit (3)

    UserPolicy
    show?

    UsersController
    show

    Admin::UsersController
    show
    User
    ‣ UsersControllerͱAdmin::UsersControllerͷshowͷೝՄ৚݅
    ͕ҧ͏৔߹ɺUserPolicyͱAdmin::UserPolicy͕ཉ͘͠
    ͳΔ͕1ͭͷModelʹରͯ͠͸1ͭͷPolicy͔͠ඥ෇
    ͚Δ͜ͱ͸Ͱ͖ͳ͍

    View full-size slide

  30. ʨ΁ `≡ŋŐŋ≡´ʩ΁ ᵪᴸᴸᴸᵪ

    View full-size slide

  31. ʨ›`≡ŋŐŋ≡´ʩ›ớ ᵲᴸᴸᴸᵲ

    View full-size slide

  32. ʨ΁ `≡ŋŐŋ≡´ʩ΁ ᵪᴸᴸᴸᵪ

    View full-size slide

  33. Make a hypothesis for authorization
    ‣ ݖݶ؅ཧ͕ϞσϧدΓͷ੹຿ͳΒɺຊདྷೝՄର
    ৅͸ControllerͷActionͰͳ͘ActiveRecordͷ
    ૢ࡞ܥϝιου(create, updateͳͲ)Ͱ͸ͳ͍͔

    ‣ ͔͠͠ɺͦΕͰ͸ొ࿥ɺߋ৽ɺ࡟আ͸ೝՄՄೳ
    ͕ͩࢀরΛೝՄͰ͖ͦ͏ʹͳ͍
    ‣ ΍͸Γݖݶ؅ཧ͸ControllerدΓͷ੹຿ͳͷͰ
    ͸ͳ͍͔

    View full-size slide

  34. https://github.com/kyuden/banken

    View full-size slide

  35. Choose banken gem

    View full-size slide

  36. Design concept of Banken
    ‣ Controllerຖʹ

    Actionʹର͢ΔೝՄ৚݅Λఆٛ

    View full-size slide

  37. Architecture with Banken
    ‣ ControllerຖʹLoyaltyΫϥεΛ࡞੒͠Action
    ʹର͢ΔೝՄ৚݅Λఆ͍ٛͯ͘͠

    A Loyalty

    Controller A
    update
    destroy
    update?
    destroy?

    View full-size slide

  38. How to use Banken
    v

    View full-size slide

  39. Description of Banken (1)
    ‣ Pundit likeͳAPIΛอͪͳ͕ΒPunditͷ
    ModelدΓͷ੹຿ΛControllerدΓʹॻ͖
    ׵͑ͨgem


    View full-size slide

  40. Description of Banken (2)

    Admin::UsersLoyalty
    show?

    UsersController

    Admin::UsersController
    show

    UsersLoyalty
    show?
    show
    ‣ ControllerຖʹLoyaltyΫϥεΛ࡞੒͠
    Modelʹ͸ґଘ͠ͳ͍ͷͰɺલड़ͨ͠
    Punditͷऑ఺΋ճආͰ͖Δ

    View full-size slide

  41. Description of Banken (3)
    ‣ Punditಉ༷ʹ಺෦࣮૷͸RailsͷϞϯΩʔ
    ύονͳͲͷ֦ுΛߦ͍ͬͯͳ͍ͷͰRails
    ͷόʔδϣϯΞοϓʹΑΔӨڹΛड͚ʹ͘ɺ
    ࠓ·Ͱ௨Γݩؾʹ৲ඌΛৼͬͯಈ͍ͯ͘Ε
    Δ

    ‣ LoyaltyΫϥε͸୯ͳΔRubyͷΫϥεͰ͋
    ΔͨΊϞδϡʔϧԽͨ͠Γܧঝͤͨ͞Γཁ
    ݅ʹԠͯ͡ॊೈʹ࣮૷Ͱ͖Δ

    View full-size slide

  42. Problem of Banken
    ‣ Bankenʹ͸੹຿ΛModel͔ΒControllerدΓʹ
    Ҡͨ͠ࣄͰPunditͰ͍͏ॴͷScopeػೳ͕ͳ͍

    ‣ Scopeػೳͱ͍͏ͷ͸ɺݖݶ͝ͱʹ࢖༻͢Δ
    named scopeΛ੾Γସ͑Δػೳ
    ‣ ͕͔͠͠ɺnamed scopeͷ੾Γସ͕͑ඞཁͳ৔
    ߹͸୯७ʹModelʹ࣮૷͢Ε͹ྑ͍ͷͰɺඞͣ͠
    ΋Scopeػೳ͕ඞཁ͔ͱ͍͑͹ͦΜͳ͜ͱ͸ͳ͍

    View full-size slide

  43. Difference between Banken and Pundit
    Banken
    ‣ ControllerدΓͷݖݶ؅ཧ
    ‣ Scopeػೳ͕࢖༻Ͱ͖ͳ͍
    ‣ ModelɺControllerͷؔ܎͕ෳࡶʹͳͬͯ΋ɺҰ؏͠
    ͯBankenຊདྷͷ࢖͍ํΛଓ͚Δ͜ͱ͕Ͱ͖Δ
    Pundit
    ‣ ModelدΓͷݖݶ؅ཧ
    ‣ Scopeػೳ͕࢖༻Ͱ͖Δ
    ‣ ModelɺControllerͷؔ܎͕ෳࡶʹͳΔͱɺ։ൃऀࣗ਎Ͱ͜ͷ໰
    ୊Λղܾ͢Δඞཁ͕͋ΓɺPunditຊདྷͷ࢖͍ํ͔Β֎ΕΔ৔
    ߹͕͋Δ

    View full-size slide

  44. ΋͏ҰͭBankenʹ͸଍Γͳ͍ࣄ͕

    View full-size slide

  45. ࡞ͬͨ͹͔ΓͳͷͰར༻ऀ͕গͳ͍͜ͱ

    (github star΍࣮ࡍͷϓϩδΣΫτͰ࢖ͬͯ΋Β͑Δͱخ͍͠Ͱ͢
    )

    View full-size slide

  46. Tutorial (ӳޠ &೔ຊޠ)΋WIKIʹ͋Γ·͢


    View full-size slide

  47. 1ΞϓϦʹ1ඖ

    BankenΛࣂͬͯΈͯ͸͍͔͔͕Ͱ͔͢

    View full-size slide

  48. ·ͱΊ
    ‣ ݖݶ؅ཧͱ͸
    ‣ ݖݶ؅ཧܥGemͷൺֱ
    ‣ CanCanCan
    ‣ Pundit
    ‣ Banken

    ‣ ίʔυͷඒ͚ͩ͠͞Ͱͳ͘ΑΓઃ
    ܭతͳࢹ఺(Architecture, Design concept)Ͱ
    GemΛධՁ͢Δͱ৽͍͠ൃݟ͕͋Δ

    View full-size slide