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

Gotanda.rb#46 権限管理のつらみとPundit

Gotanda.rb#46 権限管理のつらみとPundit

2020/09/29のGotanda.rb#46LT発表した『権限管理のつらみとPundit』です。

963393f2d643a54c41db3812c679f361?s=128

Hiroki Tanaka

September 29, 2020
Tweet

Transcript

  1. Gotanda.rb#46 ݖݶ؅ཧͷͭΒΈͱPundit 2020/09/29 גࣜձࣾτϨλ @hiroki_tanaka

  2. ࣗݾ঺հ ɾాத ޺جɿ@hiroki_tanaka ɾSIerɿϑϡʔνϟʔΞʔΩςΫτ(4೥ؒ) →WebܥϕϯνϟʔɿגࣜձࣾτϨλ(1೥ؒ) ɾRubyɿ2೥൒ɾJavaɿ2೥൒ɾSQLɿ5೥ ɾझຯɿεϓϥτΡʔϯɾમ౬८Γ ɾॳΊͯͷ֎෦ษڧձͰͷLTɺŪƄŝŪƄŝ…(((*´>ω<`)))…ŪƄŝŪƄŝ…

  3. ࠓ೔࿩͢͜ͱɾ࿩͞ͳ͍͜ͱ ʲ࿩͢͜ͱʳ - ݖݶ؅ཧͷΑ͋͘Δ໰୊ͱͲ͏ཱͪ޲͔͍͔ͬͯ͘ - ݖݶ؅ཧͷGemͷ঺հ (→ͪͳΈʹɺࢲ͕࢖ͬͨ͜ͱ͕͋Δͷ͸CanCanͱPundit) ʲ࿩͞ͳ͍͜ͱʳ - ؅ཧը໘ߏஙͷGemͷ࿩

    →rails_admin/active_admin/administrateͳͲ
  4. ݖݶͰ͜Μͳܦݧɺ ͋Γ·ͤΜ͔ʁ

  5. ͜ͷػೳ͸ॏཁͳػೳ͔ͩΒɺ ໾৬͕෦௕Ҏ্͔ͭ౦ژຊࣾͷਓʹ͔͠ ࣮ߦݖݶΛ༩͑ͳ͍Ͱཉ͍͠ɻ

  6. ސ٬ͷݸਓ৘ใ͸γεςϜ؅ཧऀݖݶͩ ͚Ͱ͸ͳ͘ɺݸਓ৘ใ؅ཧऀݖݶͷ͋ΔϢʔβ ͷΈͷӾཡͱͯ͠ཉ͍͠ɻ

  7. ݖݶ؅ཧͷΑ͋͘Δ໰୊ 1. ΞϓϦέʔγϣϯ͕ݖݶͷ৚݅෼ذίʔυͰԚΕΔ ໰୊ 2. `admin`ͱ͍͏ݴ༿ͷࢦ͍ͯ͠Δݖݶ͕Α͘Θ͔Βͳ ͘ͳΔ໰୊

  8. ݖݶ؅ཧͷΑ͋͘Δ໰୊ᶃ 1. ΞϓϦέʔγϣϯ͕ݖݶͷ৚݅෼ذίʔυͰԚΕΔ ໰୊ - ݖݶʹؔ͢ΔϏδωεϩδοΫΛ࣮૷͢ΔͨΊʹController ΍Viewͷϝιου͕ifจ΍caseจͩΒ͚ʹͳΔɻ - ࣅͨΑ͏ͳೝՄϩδοΫ͕༷ʑͳॴʹίϐϖ͞Εͯɺमਖ਼͢ ΔࡍͷӨڹൣғ͕େ͖͘ͳͬͯ͠·͏ɻ

    - Fat ControllerͷҰཁҼʹͳΔɻ
  9. ݖݶ؅ཧͷΑ͋͘Δ໰୊ᶃ ྫ͑͹ɺ͜Μͳײ͡ͷίʔυɻ - Controller - View

  10. ݖݶ؅ཧͷΑ͋͘Δ໰୊ᶄ 2. `admin`ͱ͍͏ݴ༿ͷࢦ͍ͯ͠Δݖݶ͕Α͘Θ͔Βͳ ͘ͳΔ໰୊ - αʔϏε಺ͷ`admin`ͱ͍͏ݖݶ͸ຊདྷɺͲͷϦιʔεʹର ͯ͠΋ԿͰ΋ग़དྷΔ͕ɺαʔϏεͷ֦େͱڞʹத్൒୺ʹͳ Γ͕ͪɻ - ݁Ռతʹcurrent_user͕`admin`͔Ͳ͏͔ͷνΣοΫͰ͸ͳ

    ͘ɺΑΓηϯγςΟϒͳݖݶͰνΣοΫ͢ΔϩδοΫΛ࣋ͭ Ϧιʔε͕ੜ·Εɺʮadminͱ͸Կͩͬͨͷ͔…ʯͱͳͬͯ ͠·͏ɻ
  11. ݖݶ؅ཧͷΑ͋͘Δ໰୊ᶄ ྫ͑͹ɺ͜Μͳঢ়گɻ - current_user͕`admin`ͩͬͨ৔߹͸ϦιʔεAͷॴ༗ऀͰͳ͘ ͯ΋ɺCRUD͸શͯՄೳɻ - current_user͕adminͩͬͨ৔߹͸ϦιʔεB͸ࢀরͷΈՄೳͩ ͕ɺͦΕҎ֎͸Ϧιʔεͷॴ༗ऀͰͳ͍ͱෆՄɻ - current_user͕adminͩͬͨ৔߹Ͱ΋ϦιʔεC͸ݸਓ৘ใؚ͕

    ·ΕΔͷͰࢀরؚΊશͯෆՄɻ
  12. ݖݶ؅ཧͷΑ͋͘Δ໰୊ᶃɾᶄ ᶃͱᶄͷ໰୊Λ์ஔ͢ΔͱͲ͏ͳΔͷ͔ʁ - ݖݶϩδοΫ͸αʔϏεͷ֦େͱڞʹංେԽ͍ͯͨ͘͠Ίɺ ࠷ऴతʹ͸௒ෳࡶͳ෼ذϩδοΫ͕޿ൣғʹര஀͢Δɻ 㱺୹ظతʹ͸ɺݖݶϩδοΫ͕֤Ϧιʔεʹ෼ࢄ͢Δ͜ͱͰ मਖ਼࿙Ε͕͋ͬͨ৔߹ɺக໋తͳόάͷԹচʹͳΔɻ - ຊདྷݟ͑ͯ͸͍͚ͳ͍ϖʔδ͕ݟ͑ͯ͠·͏ɾॲཧ͕Ͱ͖ͯ͠· ͏ɻٯʹɺຊདྷݟ͑Δ͸ͣͷϖʔδ͕ݟ͑ͳ͍ɻ౳

  13. ݖݶ؅ཧͷΑ͋͘Δ໰୊ᶃɾᶄ ᶃͱᶄͷ໰୊Λ์ஔ͢ΔͱͲ͏ͳΔͷ͔ʁ - ݖݶϩδοΫ͸αʔϏεͷ֦େͱڞʹංେԽ͍ͯͨ͘͠Ίɺ ࠷ऴతʹ͸௒ෳࡶͳ෼ذϩδοΫ͕޿ൣғʹര஀͢Δɻ 㱺த௕ظతʹ͸ɺίʔυͷՄಡੑɾอकੑ͕௿Լ͠ɺϝϯς φϯείετͷ૿େͱͳٕͬͯज़తͳෛͷҨ࢈ʹͳΔɻ

  14. Ͳ͏ཱͪ޲͔͏͔ɿݪҼ௥ٻ ͦ΋ͦ΋ͷ໰୊͸ʰϢʔβͱϦιʔε΁ͷݖݶʹؔ͢ ΔϩδοΫͷ࣮૷͕෼ࢄɾ෼཭͍ͯ͠Δʱ͜ͱɻ 㱺ਅҼ͸ʰຊདྷ͸ڞ௨Խ࣮ͯ͠૷͞ΕΔ΂͖Ϣʔβͷ ݖݶʹؔ͢ΔϩδοΫ͕֤ػೳɾ֤Ϣʔεέʔεຖʹϕ λͰ࣮૷͞Ε͍ͯΔɾͤ͟ΔΛಘͳ͍ঢ়گʹͳ͍ͬͯ Δʱ͜ͱɻ

  15. ͡Ό͋ɺͲ͏͠Α͏ʁ

  16. ͦ͏ͩɺ PunditΛ࢖ͬͯΈΑ͏

  17. Punditͱ͸ - ֤ϦιʔεͷActionʹରͯ͠ೝՄ৚݅Λઃఆ͢Δɻ - ֤ModelຖʹݖݶઃఆΛߦ͏PolicyΫϥεΛ࡞੒͠ɺ Actionʹର͢ΔೝՄ৚݅Λఆٛ͢Δɻ(Modelґଘ) - PolicyΫϥε͸ී௨ͷRubyΫϥεͰ͋ΔͨΊɺRuby ͷجૅ஌͕ࣝ͋Ε͹୭Ͱ΋ѻ͑Δɻ

  18. PunditΛಋೖͨ݁͠Ռ - PolicyΫϥε

  19. PunditΛಋೖͨ݁͠Ռ - ControllerΫϥε ControllerଆͰauthorizeΛ࣮ߦͨ͠ࡍʹɺModel໊+Policyͷن ଇͰPolicyΫϥε͕ಛఆ͞Ε্ͨͰΠϯελϯεԽ͞Εͯɺ֘౰ ͢Δpolicyͷϝιου͕ݺ͹ΕͯͦͷΞΫγϣϯ͕࣮ߦՄೳ͔Ͳ ͏͔൑ఆ͢Δɻ

  20. PunditͷϝϦοτᶃ - ݖݶ؅ཧͷϩδοΫ͕1Օॴʹू໿͞ΕΔ - ݖݶ؅ཧ͸ʮXXݖݶͳΒYYϦιʔεͷCRUDΛڐՄ͢Δʯ ͱ͍͏Α͏ʹϦιʔεϕʔεͷཁ݅Ͱ͋ΓɺModelͷ੹຿ͱ͠ ͯଊ͑Δ͜ͱͰݖݶϩδοΫͷ࣮૷Օॴ͕໌֬ʹͳΔɻ - ݖݶϩδοΫ͕PolicyΫϥεʹू໿͢Δ͜ͱͰɺController ͷ֤Action͕ෳࡶͳݖݶϩδοΫͰԚΕΔ͜ͱΛ๷͛Δɻ

  21. PunditͷϝϦοτᶄ - PolicyΫϥε͸ී௨ͷRubyΫϥεͷͨΊॊೈੑ͕ߴ͍ - PolicyΫϥε͸RubyͰग़དྷΔॲཧ͸ԿͰ΋ߦ͏ࣄ͕Ͱ͖Δͨ Ίɺࣗ༝౓͕ͱͯ΋ߴ͘ෳࡶͳۀ຿ϩδοΫʹॊೈʹରԠՄ ೳɻ - ϞδϡʔϧԽ΍ܧঝͱ͍ͬͨ͜ͱ΋Մೳɻ

  22. PunditͷσϝϦοτᶃ - ݸʑͷPolicyϑΝΠϧ͕Modelຖʹ࡞੒͞Ε͍ͯΔͷ Ͱݖݶશମͷݟ௨͕͠ѱ͍ɻ - ΞϓϦέʔγϣϯͷݖݶ؅ཧશମΛ֬ೝ͠Α͏ͱͨ͠৔߹ɺ ݸʑͷPolicyϑΝΠϧΛ1ͭͣͭݟ͍ͯ͘ඞཁ͕͋Δɻ - ΞϓϦέʔγϣϯ͕ෳࡶԽɾංେԽͨ͠৔߹ʹ͸ݟ௨͠ͷѱ ͞͸க໋తͳٕज़తෛ࠴ʹͳΓ͔Ͷͳ͍ɻ

  23. PunditͷσϝϦοτᶄ - Modelຖʹඥͮ͘PolicyΫϥεʹ͸Controllerͷ Actionʹඥͮ͘ϝιουΛ࣮૷͢ΔͨΊɺ҉໧తʹ ModelͱPolicyͱController͸1:1:1ͱ͍͏੍໿ͱͳΔɻ - ΞϓϦέʔγϣϯ͕ෳࡶԽɾංେԽ͠ɺ1ͭͷModelΛѻ͏ Controller͕ෳ਺ଘࡏ͠ɺͦΕͧΕͰݖݶϩδοΫΛ੾Γସ͑ ͍ͨͱ͍͏έʔε͕ੜ·Εͨ࣌ɺࠔΔɻ -

    ͭ·Γɺ1ͭͷModelʹରͯ͠ɺෳ਺ͷPolicyΫϥε͕ඞཁͱ ͳͬͨ৔߹ʹԿΒ͔ͷ޻෉Λ͢Δ͜ͱͳΔɻ (ྑ͍ϓϥΫςΟε͕͋ͬͨΒɺڭ͑ͯԼ͍͞(o*Ň_Ň)oųƅŠŕ)
  24. ·ͱΊ 1. ΞϓϦέʔγϣϯ͕ݖݶͷ৚݅෼ذίʔυͰԚΕΔ໰୊ - PunditͷಋೖͰ֤ϦιʔεຖͷݖݶϩδοΫΛҰՕॴʹ·ͱΊ Δ͜ͱ͕ग़དྷΔͷͰController΍View͕৚݅෼ذίʔυͰԚΕ Δ໰୊͸ղܾग़དྷΔɻ 2. `admin`ͱ͍͏ݴ༿ͷࢦ͍ͯ͠Δݖݶ͕Α͘Θ͔Βͳ͘ͳΔ໰୊ -

    `admin`ͱ͍͏ᐆດͳ໾ׂΛͳ͘͠ɺ֤Ϧιʔεຖʹ໌֬ͳ໾ ׂ(role)ΛϢʔβʹׂΓ౰ͯɺPunditͰActionຖʹ࣮ߦ੍ޚ͢Δ ͜ͱͰᐆດ͞Λճආ͢Δ͜ͱ͸Մೳɻ - ͨͩɺ`admin`ͱ͍͏ݖݶ͸࡞Ζ͏ͱࢥ͑͹࡞ΕΔͷͰɺࠜຊ తͳ཈੍͸ग़དྷͳ͍ɻ
  25. ͓ΘΓʹ - ʮLT΍Γ·͢ʂʯͱݴͬͯௐ΂Δ·Ͱʮݖݶ؅ཧͷ޻ ෉ͱ͸…ʁʯͱײ͍ͯ͡·ͨ͠ɻ ͔͠͠ɺௐ΂Δ಺ʹ਎ʹ֮͑ͷ͋ΔΞϓϦέʔγϣϯ ͷ֦େͷӨڹͰͷϩδοΫͷෳࡶԽ໰୊΍ɺόά͕ൃ ੜͨ͠৔߹ʹக໋ইʹͳΔՄೳੑͷߴ͔͞ΒೝࣝΛվ Ί·ͨ͠ɻ(*´Д⊂ʋŠƄŶƃūšŘ - ·ͨɺॏཁ͔ͩΒͦ͜γϯϓϧʹ୭ʹͰ΋ѻ͑ΔɾΘ͔Γ΍

    ͘͢อͭ͜ͱ͕ඞཁͱڧ͘ײ͡·ͨ͠ɻ(౰ͨΓલ)
  26. ͓·͚

  27. ͓·͚1ɿPunditɾCanCanCanͷൺֱ Pundit CanCanCan ಋೖ GemΛΠϯετʔϧ͠ɺBaseContoller౳ͰPunditΛ include͢Ε͹࢖༻Մೳɻ GemΛΠϯετʔϧ͠ɺAbilityΫϥεΛ࡞Ε͹࢖༻Մ ೳɻControllerͰͷinclude͸ෆཁɻ ݖݶઃఆ ֤ModelʹରԠͨ͠PolicyΫϥεΛ࡞੒͠ɺݖݶຖͷઃ

    ఆΛهड़͢Δɻ 1ͭͷAbilityΫϥεʹϩʔϧຖͷ֤Ϟσϧʹର͢Δશͯͷ ݖݶΛهࡌ͍ͯ͘͠ɻ ControllerͰͷೝՄ authorizeϝιουΛݺͼग़͢͜ͱͰϞσϧʹରԠ͢Δ PolicyΫϥε͕ࣗಈతʹࢀর͞ΕΔɻControllerͷΞΫ γϣϯ໊ͱϚονͨ͠PolicyΫϥεͷϝιου͕ݺ͹ Εɺ࣮ߦՄೳ͔൑ఆ͢Δɻ authorize!ϝιουͰݖݶͷೝՄ͕Ͱ͖ɺAbilityΫϥεʹ ఆٛͨ͠ݖݶઃఆΛࢀর͠ɺ࣮ߦՄೳ͔൑ఆ͢Δɻ ViewͰͷೝՄ policyϝιουΛ࢖༻͢Δ͜ͱͰ൑ఆͰ͖Δɻ can?΍cannot?ϝιουͰ൑ఆͰ͖Δɻ Ϩίʔυ΁ͷ੍໿ scopeͱ͍͏ػೳΛ༻͍ͯɺPolicyϑΝΠϧ಺ʹΠϯφ ʔΫϥεͱͯ͠ScopeΫϥεΛఆٛ͢Δ͜ͱͰϢʔβ ͷϨίʔυ΁ͷΞΫηεΛ੍ݶͰ͖Δɻ policy_scopeϝιουͰݺͼग़͠Մೳɻ Hash of Conditionsͱ͍͏ػೳΛ༻͍ͯɺ৚݅Λ౉͢ͱऔ ಘ͢ΔϨίʔυΛ੍ݶग़དྷΔɻ accessible_byϝιουͰݺͼग़͠Մೳɻ ૯߹ ֤ϞσϧຖʹରԠͨ͠PolicyϑΝΠϧΛఆٛ͢Δɻ σʔλϞσϧ͕ଟ͍৔߹Ͱ΋ɺPolicyϑΝΠϧ͸γϯ ϓϧʹอͯΔ͕ɺݸʑͷPolicyϑΝΠϧʹݖݶ͕ݸผ ࣮૷͞Ε͍ͯΔͷͰશମͷݟ௨͕͠ѱ͍ɻ 1ͭͷAbilityΫϥε಺ʹ֤ϩʔϧͷ֤Ϟσϧ΁ͷݖݶΛఆ ٛ͢Δɻ1ϑΝΠϧ಺ʹશͯͷݖݶ৘ใ͕ू·ΔͷͰݟ௨ ͠͸ྑ͍͕ɺΞϓϦέʔγϣϯͷ֦େͱڞʹංେԽͷҰ ్ΛḷΔɻ
  28. ͓·͚2ɿͦͷଞͷݖݶ؅ཧGem - rolifyɿϢʔβʹϩʔϧΛ෇༩ͨ͠Γɺࢦఆͨ͠ϩʔϧΛ͍࣋ͬͯ ΔϢʔβΛ୳ͨ͠ΓͱϩʔϧϕʔεͰͷݖݶ؅ཧΛߦ͏ɻ →ೝূGem(deviseͳͲ)΍ೝՄGem(CanCanCanɾPundit)ͱ؆୯ʹ ઀ଓग़དྷΔɻ - authorityɿORMʹґଘͤͣʹControllerͷActionຖʹ࣮ߦͰ͖Δ ݖݶΛఆٛͰ͖Δɻ2019೥ʹ։ൃఀࢭதɻ -

    bankenɿPunditϥΠΫͳAPIΛอͪͭͭModelʹґଘͤͣʹɺ ControllerͷActionʹඥͮ͘ϝιουͷݖݶΛఆٛग़དྷΔɻ →ΞϓϦ͕֦େʹͳΓɺModelɾController͕ෳࡶʹͳͬͯ΋Ұ؏ ͯ͠࢖͍ଓ͚Δ͜ͱՄೳɻ
  29. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ