Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

ݖݶͰ͜Μͳܦݧɺ ͋Γ·ͤΜ͔ʁ

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

ݖݶ؅ཧͷΑ͋͘Δ໰୊ᶄ 2. `admin`ͱ͍͏ݴ༿ͷࢦ͍ͯ͠Δݖݶ͕Α͘Θ͔Βͳ ͘ͳΔ໰୊ - αʔϏε಺ͷ`admin`ͱ͍͏ݖݶ͸ຊདྷɺͲͷϦιʔεʹର ͯ͠΋ԿͰ΋ग़དྷΔ͕ɺαʔϏεͷ֦େͱڞʹத్൒୺ʹͳ Γ͕ͪɻ - ݁Ռతʹcurrent_user͕`admin`͔Ͳ͏͔ͷνΣοΫͰ͸ͳ ͘ɺΑΓηϯγςΟϒͳݖݶͰνΣοΫ͢ΔϩδοΫΛ࣋ͭ Ϧιʔε͕ੜ·Εɺʮadminͱ͸Կͩͬͨͷ͔…ʯͱͳͬͯ ͠·͏ɻ

Slide 11

Slide 11 text

ݖݶ؅ཧͷΑ͋͘Δ໰୊ᶄ ྫ͑͹ɺ͜Μͳঢ়گɻ - current_user͕`admin`ͩͬͨ৔߹͸ϦιʔεAͷॴ༗ऀͰͳ͘ ͯ΋ɺCRUD͸શͯՄೳɻ - current_user͕adminͩͬͨ৔߹͸ϦιʔεB͸ࢀরͷΈՄೳͩ ͕ɺͦΕҎ֎͸Ϧιʔεͷॴ༗ऀͰͳ͍ͱෆՄɻ - current_user͕adminͩͬͨ৔߹Ͱ΋ϦιʔεC͸ݸਓ৘ใؚ͕ ·ΕΔͷͰࢀরؚΊશͯෆՄɻ

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

͡Ό͋ɺͲ͏͠Α͏ʁ

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

PunditͷσϝϦοτᶄ - Modelຖʹඥͮ͘PolicyΫϥεʹ͸Controllerͷ Actionʹඥͮ͘ϝιουΛ࣮૷͢ΔͨΊɺ҉໧తʹ ModelͱPolicyͱController͸1:1:1ͱ͍͏੍໿ͱͳΔɻ - ΞϓϦέʔγϣϯ͕ෳࡶԽɾංେԽ͠ɺ1ͭͷModelΛѻ͏ Controller͕ෳ਺ଘࡏ͠ɺͦΕͧΕͰݖݶϩδοΫΛ੾Γସ͑ ͍ͨͱ͍͏έʔε͕ੜ·Εͨ࣌ɺࠔΔɻ - ͭ·Γɺ1ͭͷModelʹରͯ͠ɺෳ਺ͷPolicyΫϥε͕ඞཁͱ ͳͬͨ৔߹ʹԿΒ͔ͷ޻෉Λ͢Δ͜ͱͳΔɻ (ྑ͍ϓϥΫςΟε͕͋ͬͨΒɺڭ͑ͯԼ͍͞(o*Ň_Ň)oųƅŠŕ)

Slide 24

Slide 24 text

·ͱΊ 1. ΞϓϦέʔγϣϯ͕ݖݶͷ৚݅෼ذίʔυͰԚΕΔ໰୊ - PunditͷಋೖͰ֤ϦιʔεຖͷݖݶϩδοΫΛҰՕॴʹ·ͱΊ Δ͜ͱ͕ग़དྷΔͷͰController΍View͕৚݅෼ذίʔυͰԚΕ Δ໰୊͸ղܾग़དྷΔɻ 2. `admin`ͱ͍͏ݴ༿ͷࢦ͍ͯ͠Δݖݶ͕Α͘Θ͔Βͳ͘ͳΔ໰୊ - `admin`ͱ͍͏ᐆດͳ໾ׂΛͳ͘͠ɺ֤Ϧιʔεຖʹ໌֬ͳ໾ ׂ(role)ΛϢʔβʹׂΓ౰ͯɺPunditͰActionຖʹ࣮ߦ੍ޚ͢Δ ͜ͱͰᐆດ͞Λճආ͢Δ͜ͱ͸Մೳɻ - ͨͩɺ`admin`ͱ͍͏ݖݶ͸࡞Ζ͏ͱࢥ͑͹࡞ΕΔͷͰɺࠜຊ తͳ཈੍͸ग़དྷͳ͍ɻ

Slide 25

Slide 25 text

͓ΘΓʹ - ʮLT΍Γ·͢ʂʯͱݴͬͯௐ΂Δ·Ͱʮݖݶ؅ཧͷ޻ ෉ͱ͸…ʁʯͱײ͍ͯ͡·ͨ͠ɻ ͔͠͠ɺௐ΂Δ಺ʹ਎ʹ֮͑ͷ͋ΔΞϓϦέʔγϣϯ ͷ֦େͷӨڹͰͷϩδοΫͷෳࡶԽ໰୊΍ɺόά͕ൃ ੜͨ͠৔߹ʹக໋ইʹͳΔՄೳੑͷߴ͔͞ΒೝࣝΛվ Ί·ͨ͠ɻ(*´Д⊂ʋŠƄŶƃūšŘ - ·ͨɺॏཁ͔ͩΒͦ͜γϯϓϧʹ୭ʹͰ΋ѻ͑ΔɾΘ͔Γ΍ ͘͢อͭ͜ͱ͕ඞཁͱڧ͘ײ͡·ͨ͠ɻ(౰ͨΓલ)

Slide 26

Slide 26 text

͓·͚

Slide 27

Slide 27 text

͓·͚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ϑΝΠϧ಺ʹશͯͷݖݶ৘ใ͕ू·ΔͷͰݟ௨ ͠͸ྑ͍͕ɺΞϓϦέʔγϣϯͷ֦େͱڞʹංେԽͷҰ ్ΛḷΔɻ

Slide 28

Slide 28 text

͓·͚2ɿͦͷଞͷݖݶ؅ཧGem - rolifyɿϢʔβʹϩʔϧΛ෇༩ͨ͠Γɺࢦఆͨ͠ϩʔϧΛ͍࣋ͬͯ ΔϢʔβΛ୳ͨ͠ΓͱϩʔϧϕʔεͰͷݖݶ؅ཧΛߦ͏ɻ →ೝূGem(deviseͳͲ)΍ೝՄGem(CanCanCanɾPundit)ͱ؆୯ʹ ઀ଓग़དྷΔɻ - authorityɿORMʹґଘͤͣʹControllerͷActionຖʹ࣮ߦͰ͖Δ ݖݶΛఆٛͰ͖Δɻ2019೥ʹ։ൃఀࢭதɻ - bankenɿPunditϥΠΫͳAPIΛอͪͭͭModelʹґଘͤͣʹɺ ControllerͷActionʹඥͮ͘ϝιουͷݖݶΛఆٛग़དྷΔɻ →ΞϓϦ͕֦େʹͳΓɺModelɾController͕ෳࡶʹͳͬͯ΋Ұ؏ ͯ͠࢖͍ଓ͚Δ͜ͱՄೳɻ

Slide 29

Slide 29 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ