tl; dr
Webã¢ããªã±ãŒã·ã§ã³ã§æš©å©ã®å·®å¥åãæäŸããå¥ã®ïŒ1001stïŒæ¹æ³ã ãã®ãœãªã¥ãŒã·ã§ã³ã®æŠå¿µã¯ãPHPã§é·ãéå®è£ ãããåŸã«rubyã§æžãçŽãããŸããã å®è£ ãåçŽã§ããããã説æããã¢ãããŒãã¯Rails ã Laravelãªã©ã®MVCãã¬ãŒã ã¯ãŒã¯ã«é©çšã§ããŸãã
æ¬æã§ã¯ãã¢ããªã±ãŒã·ã§ã³ãžã®ãœãªã¥ãŒã·ã§ã³ã®æè¡ççµ±åã ãã§ãªããææ¡ãããå®è£ ã®çç±ã詳现ã«é瀺ããããšããŸããã
2015幎ã«ã¯ãRuby on Railsã®ãŠãŒã¶ãŒæš©å©ãé åžããããã®å¥ã®gemãæžãããšãæ¢ããŠã§ããã®ã¯ãç人ã ããããããŸããã çµå±ã®ãšããã誰ããé·ãé CanCan ã PunditããŸãã¯æçµçã«Acl9ã䜿çšããŠããŸãã ãã ãããã¹ãŠãããã»ã©åçŽã§ã¯ãããŸããã ãŸããèªåãæ£åœåããçç±ãããã€ããããŸãã
- ä»æ¥ã話ããã決å®ã¯ãã«ããŒã«ãªããã£ãšåã«çŸããŸããã æåã®äœæ¥ã³ã³ã»ããã¯ã2006幎ã«PHPã§åŠæ ¡ã®ãµã€ãçšã«äœæãããŸãã
- ç§ã«ãšã£ãŠå¹žéãªããšã«ãç§ã¯ä»ã®ã¢ãããŒããç¥ããªãã£ãã®ã§ãæ³ååãèªç±ã«é£ã°ãããšãã§ããŸããã ä»ã®äººã®ã¢ã€ãã¢ãç¹°ãè¿ããŠãããšéé£ãããããšã¯ãŸããããŸããããç§ã®çºæãå®è·µããããšã¯åžžã«èå³æ·±ãã§ã
- ç§ã«ãšã£ãŠããã®ãããªãããžã§ã¯ãã¯ããªã³ã©ã€ã³ãã¬ãŒãã³ã°ã®ããã«åè¬ãã人ã ãšååããããã®ãã©ãããã©ãŒã ã§ãã ãããŠãããã§ããTheRoleã¯ééã®äžçã«å°ããªããŒããŒããããããŸã
- ã©ããªgemã®éçºã§ããã£ãŒãããã¯ãåŸãããããã«ããå°ãè¯ããªãããšãã§ããŸãïŒç§ã¯Ivanovoã«é·ãäœãã§ããŸããïŒ
- TheRoleã§å®è£ ãããã¢ãããŒãã¯ãPHPãJSãPythonã§ç°¡åãç°¡åãè¿ éã«ç¹°ãè¿ãããšãã§ããŸãã çªç¶ãMVCã®èªèšŒåé¡ã解決ãããã®ãªãã·ã§ã³ã奜ãã«ãªãã Laravelã§ãããç¹°ãè¿ãããšã«ãªããŸãã ç¥ãæ¹æ³ïŒïŒ
第äºã«ã以äžã§ã¯ãRoRãããžã§ã¯ãã§åœ¹å²åæ ãæŽçããæ¹æ³ã詳现ã«èª¬æããã ãã§ãªãããã®ãœãªã¥ãŒã·ã§ã³ã®çç±ãšæŽå²ã«ã€ããŠã觊ããŸãã 解決ããããšããŠããã¿ã¹ã¯ãgemã®æ©èœãé©çšæ§ã®éçããŠãŒã¹ã±ãŒã¹ã«ã€ããŠïŒãããŠèªããæã£ãŠã§ã¯ãªãïŒãç§ã¯ããªãã®å€åã§æèœã®ãã
TheRoleãšã¯äœã§ããïŒ
ããªãŒã§èããåºããåã«ããããžã§ã¯ãã§TheRoleã䜿çšããæ¹æ³ã詳现ã«èª¬æããããã«ããã®ããŒã«ã®ããžã§ã³ãå®åŒåããããšããŸãã
TheRoleã¯ãRoRã¢ããªã±ãŒã·ã§ã³çšã®åœ¹å²ããŒã¹ã®å¢çèšå®ã·ã¹ãã ã§ãã
- ãããžã§ã¯ãã®ã¢ã¯ã»ã¹å¶åŸ¡ã«äºåå®çŸ©ããããã¹ãæäŸããŸã
- ã»ãšãã©ã®å Žåãè¿œå ã®ããã°ã©ãã³ã°ã¯å¿ èŠãããŸãã
- ã³ã³ãããŒã©ãŒã«çµ±åããŠã¢ã¯ã·ã§ã³ãžã®ã¢ã¯ã»ã¹ãå¶éããç°¡æœãªæ¹æ³ããããŸã
- ãå¥çŽvs. æ§æ»
- ã¢ã¯ã»ã¹ãªã¹ãã管çããããã®GUIããããŸã
- å®è£ ã®åçŽããå°éã®ã³ãŒããããã³ruby / rails / databaseã®ãã¹ãŠã®äž»èŠãªçµã¿åããã®ãã¹ãã«ããé©åãªã«ãã¬ããžã«ãããååãªä¿¡é Œæ§ãšååãªãµããŒã
ééããªãããã®æ±ºå®ã¯äžèœè¬ã§ã¯ãªããå€ãã®ãã©ã¹é¢ãšãã€ãã¹é¢ããããŸãã ç§ã®æèŠã§ã¯ãäœãéèŠã§ãããã«ã€ããŠè©±ãããããšããŸãããã®åé¡ã«é¢ããããªãã®æèŠãç¥ããããšæããŸãã
ã¡ãã£ãšããæŽå²
ç§ããŸã åŠçã ã£ã2006-2007幎ãç§ã¯æåã«åçšãããžã§ã¯ãã§PHPã䜿çšããªããã°ãªããŸããã§ããã ççŽã«èšã£ãŠãç§ãèŠããã®ã¯éåžžã«ã·ã§ãã¯ãåãããããPHPã³ãŒããæŽçããæ¹æ³ã暡玢ãå§ããŸããã çãæ€çŽ¢ã§ãMVCã¢ãŒããã¯ãã£ã«è³ããŸããã ãã®æç¹ã§PHPã§èš±å®¹ã§ãã解決çãèŠã€ããããªãã£ããããããã°ã©ãã³ã°ãžã®æ ç±ãšèªç±æéã®å©çšå¯èœæ§ã«ãããèªåã®MVCãã¬ãŒã ã¯ãŒã¯ãPHPã§èªå·±å®çµçã«æžãããšã«ãªããŸããã
ç§ã¯ãæåž«ãšããŠåããŠããåŠæ ¡ã®ãŠã§ããµã€ãã§ãæ°ããé ãããMVCãã€ã¯ã®å®è¡ã«æåããŸããã ããã«ããµã€ãäžã®ãŠãŒã¶ãŒã®åœ¹å²ã®ååžã«ã€ããŠçåã«æããŸããã èŠããã«ãåžæãã解決çãèŠã€ãããŸããã§ããã ãŸãã ã³ã³ãããŒã©ãŒ/ã¢ã¯ã·ã§ã³ã䜿çšããMVCã®èªäœã®å®è£ ãšãããŒã«ããŒã¹ã®ã¢ã¯ã»ã¹ããªã·ãŒã管çããããã®ç®¡çããã«ãäœæããããšããé¡æãšçžãŸã£ãŠãå¥ã®PHPãã€ã¯ãäœæããããã«ãªããŸããã
2008幎ãç§ã¯Ruby on Railsã® trapã«é¥ããç§ã®PHPå®éšã¯ãã¹ãŠéå»ã®ãã®ã«ãªããŸããã ããããRubyã³ãŒãã§ç§ã®æ±ºå®ã®1ã€ãç¹°ãè¿ããããšãã欲æ±ã«æ©ãŸãããŸããã 2011幎ã«äº¡ããªã£ãPHPã®æŠå¿µãã«ããŒã«å®è£ ãå§ãããšããååã¯çç±ãæããŠåŸ®ç¬ãã ã åœæã®CanCanã¯ãäºå®äžã®çç£ãœãªã¥ãŒã·ã§ã³ã§ããã ãããŠãè»èŒªãåçºæããå¿ èŠã¯ãŸã£ãããããŸããã§ããã ããããç§ã¯å€¢ããããããããšã«æ £ããŠããŸããã ããã§ãç§ã¯ãã®å®ç³ãæžãã®ã«ãã£ãããšããä»äºãå§ããŸããã
ãªãTheRoleãäœæãããã®ã§ããïŒ
ç§ãè¿œæ±ããäž»ãªç®æšã¯ãã°ã©ãã£ã«ã«ã€ã³ã¿ãŒãã§ã€ã¹ã䜿çšããŠã¢ã¯ã»ã¹æš©ãåºå¥ããããã®ãœãªã¥ãŒã·ã§ã³ãäœæããããšã§ããã
åé¡ã解決ããããã®ãããã°ã©ããŒã®ã¢ãããŒããã«æºè¶³ããŠããŸãããã¢ã¯ã»ã¹å¶åŸ¡ã®ããã«ããŸãäœãïŒ Abilityã¯ã©ã¹ãªã©ïŒãããã°ã©ã ããå¿ èŠãããã次ã«ã¢ã¯ã»ã¹ããªã·ãŒïŒæ¢åã®æ©èœããã·ã¹ãã ïŒãå€æŽããå¿ èŠãããå Žåãäœããåããã°ã©ãã³ã°ããããã°ã©ããŒãæåŸ ããå¿ èŠããããŸãïŒèªã¿åããééããç¯ãïŒããµãŒããŒãåã³ãŒãã£ã³ã°ããŸãã
åé¡ã解決ããããã®ããŠãŒã¶ãŒã¢ãããŒããã«æºè¶³ããŠããŸãããµã€ã管çè ãæå°éã®æºåã§ãæäŸãããã€ã³ã¿ãŒãã§ã€ã¹ãä»ããŠäžéšã®ãµã€ãæ©èœãžã®ã¢ã¯ã»ã¹ããªã·ãŒãåå¥ã«å€æŽã§ããŸãã ïŒç§ã¯ããã§ãšã©ãŒãèµ·ãã確çãå°ãªããªãããšãåŠå®ããŸããããå°ãªããšããšã©ãŒã®çµæã¯ããã°ã©ããŒã®è©ã«ã¯ãããŸããïŒ
ã©ã®ã¢ã¯ã»ã¹åºæºãååšããŸããïŒ
ããŸããŸãªã·ã¹ãã ã§ã®ã¢ã¯ã»ã¹å¶åŸ¡ã®ååã調æ»ããããšã§ãäœããã®æ¹æ³ã§æäœãžã®ã¢ã¯ã»ã¹åºæºã®ããã次ã®ãªã¹ãã«å°éããŸãã
- æææš© -éåžžããŠãŒã¶ãŒãéèŠãªæäœãå®è¡ã§ããã®ã¯ãèªåã«å±ãããªããžã§ã¯ãã®ã¿ã§ãã ã€ãŸã ãŠãŒã¶ãŒãšãªããžã§ã¯ãã®éã«ã¯ãæææš©ïŒææïŒã®å åããããŸãã ããã«ã®éšå±ã«è¡ãã«ã¯ãéµãå¿ èŠã§ã
- æäœäžã®å¯çšæ§ -ããã¯éåžžã ACLã«ãã£ãŠæ±ºå®ãããŸãã ã€ãŸã ç¹å®ã®ã¹ãã¬ãŒãžã«ã¯ãç¹å®ã®ãŠãŒã¶ãŒãååãšããŠäœããã®ã¢ã¯ã·ã§ã³ãå®è¡ã§ãããã©ããã«é¢ããæ å ±ããããŸãã åŠéšé·ã®ãªãã£ã¹ã«ã¯ãè©Šéšãåããããšãã§ããåŠçã®ãªã¹ãããããŸã
- æäœã®å¯çšæ§æé -ã¢ã¯ã·ã§ã³èªäœã¯äœ¿çšå¯èœã§ãããçŸåšã¯äœ¿çšã§ããŸããã åŠçã¯ã»ãã·ã§ã³äžã«ã®ã¿è©Šéšãåããããšãã§ããŸãããååãšããŠãè©Šéšã«åæ Œããæäœã¯å©çšã§ããŸãã
- ãªããžã§ã¯ãã«å¯Ÿããæäœã®å¯çšæ§ã¯ ACLã«é¡äŒŒããŠããŸãããããã§ã¯åæäœã¯ç¹å®ã®ãªããžã§ã¯ãã«é¢é£ä»ããããŠãããããã§ã¯æææš©ã®åºæºã¯å€§ããªåœ¹å²ãæãããŠããŸããã ãã£ãŒã³ã®ãªãã£ã¹ã«ã¯ãç¹å®ã®ç§ç®ã履修ã§ããåŠçã®ãªã¹ãããããŸã
- æéå¶éã®ãããªããžã§ã¯ãã®æäœã®å¯çšæ§ -ã»ãã®äžäŸãšããŠã åŠéšé·ã®ãªãã£ã¹ã«ã¯ãç¹å®ã®æ¥ã«ã®ã¿ïŒèªåèªèº«ã«å¯ŸããŠïŒããã€ãã®ç§ç®ãïŒããããã«ïŒååè¬ã§ããåŠçã®ãªã¹ãããããŸã
ããã§åæ¢ããŸãããå人ã®åºæºã§ã¯ãªãã°ã«ãŒãã®åºæºãæ¡çšããå Žåããã®ãªã¹ãã«ç°¡åã«2ãæããããšãã§ããããšã瀺ããŸãã ããããããã¯éåžžã«æ·±ãã§ãã ããªãã¯drããããšãã§ããŸãã
ä»æ³šç®ïŒ TheRoleã¯ãæåã®2ã€ã®ã¢ã¯ã»ã·ããªãã£åºæºã§ããæææš©ãšéçšäžã®å¯çšæ§ ïŒACLïŒã®ã¿ãæäŸããŸãã æ®ãã¯å€§èã«æšãŠãŸãã
1.5ã¢ã¯ã»ã¹åºæºã æææš©ã®æ±ºå®
TheRoleã¯ã ãªããžã§ã¯ãã®æææš©ãšæäœã®å¯çšæ§ãšãã 2ã€ã®ã¢ã¯ã»ã¹åºæºã®ã¿ã§äœæ¥ãæäŸããŸã ã ãã ãã ãªããžã§ã¯ããææãããšããåºæºã¯ãå³å¯ã«èšãã°ãTheRoleãŸãã¯ä»ã®æ¿èªã®ããã®gemã®ã¿ã¹ã¯ã§ã¯ãªãããšãèªããªããã°ãªããŸããã ãªããžã§ã¯ãéã®é¢ä¿ã誰ãšã©ã®ããã«æ§æãããŠãããããªããžã§ã¯ãã®æææš©ã®å åã¯èª°ã«ãããããŸããã ããã§æ®éçãªãœãªã¥ãŒã·ã§ã³ãäœæããããšã¯äžå¯èœã§ãã
ããã¯ã ææè ã®æææš©ãã¹ãæ¹æ³ãæå³ããŸããïŒ TheRoleã§æäŸãããŠãããã®ããã¯ã¹ã¯ããªããžã§ã¯ãéã®é¢ä¿ã®æãåçŽãªã±ãŒã¹ã«åºã¥ããŠãããããããã¢ããªã±ãŒã·ã§ã³ã®ãã¹ãŠã®ã±ãŒã¹ã«é©ããŠããããã§ã¯ãããŸããã
ownerã¡ãœããã¯äœãããŸããïŒ ããããã£ãŠããã®ãŠãŒã¶ãŒã®IDãæå®ããããªããžã§ã¯ãã®USER_IDãã£ãŒã«ããšäžèŽãããããšããŸãã ã€ãŸã 次ã®åŒã³åºãã§ïŒ
@user.owner?(@page)
å®éã«ãã§ãã¯ãããŸã
@user.id == @page.user_id
ããã ãã§ãã
å ¥åæã«ã¯ãã»ãšãã©ã®ãããžã§ã¯ãã§ãã»ãšãã©ã®å Žåããã®æ¹æ³ãæ©èœããŸãã ãã ãã ææè ãååŸããããã«è¿œå ã®æé ãå®è¡ããæºåãããŠãã ããã æãŸããçµæãè¿ããŸããã ãããè¡ãæ¹æ³ã¯ãgemã®ããã¥ã¡ã³ãã§èª¬æãããŠããŸã ã
ãªãã¢ã¯ã»ã¹å¶åŸ¡åºæºã1.5ã ããªã®ã
ãã©ã®ã¢ã¯ã»ã¹åºæºãååšããŸããïŒãã»ã¯ã·ã§ã³ã§ç€ºããå°ãªããšã5ã€ã®ã±ãŒã¹ãã«ããŒãããããªã¬ãã«ã®æš©å©ã®åé ã·ã¹ãã ãäœæããã®ãçŽ æŽããããšæãå Žåã¯ãããªããæãããããšãæããŸãã ãã®ãããªã·ã¹ãã ãåºã䜿çšããããã«äœæããè©Šã¿ã¯ãç¡æå³ãªè¡çºãšåããããè±éçã§ãã
- 第äžã«ããã®ãããªããšã¯èª°ã«ãšã£ãŠãå®è³ªçã«äžèŠã§ããããã®ãããªçµã¿åãããå®éã®ãªãªãŒã¹ãåãåãå¯èœæ§ã¯äœãã§ãã
- 第äºã«ããã®ãããªæš©å©åé ã·ã¹ãã ã¯ããã¹ãã§ã«ããŒããå¿ èŠãããèšå€§ãªæ°ã®ã±ãŒã¹ã«é¢é£ããŠããŸãã ãããŠãããã¯éåžžã«é¢åã§ãã
- 第äžã«ããã®ãããªã·ã¹ãã ã§ã¯ããšã³ããŠãŒã¶ãŒãã·ã¹ãã ãæèçã«å¶åŸ¡ã§ããããã«ããã¢ã¯ã»ã¹å¯èœãªã€ã³ã¿ãŒãã§ãŒã¹ãèæ¡ããããšã¯éåžžã«å°é£ã§ãã
- 第4ã«ããã®ãœãªã¥ãŒã·ã§ã³ãå®æããŠãããšã³ããŠãŒã¶ãŒã¯ãã®ãœãªã¥ãŒã·ã§ã³ãæäŸãããã¹ãŠã®æ©èœã䜿çšããªããšç¢ºä¿¡ããŠããŸãã
ãã®ãããTheRole ã¯æäœã®å¯çšæ§ã®åºæºãä¿èšŒããåé¡ã®ã¿ã解決ãããªããžã§ã¯ãã®æææš©ã®åºæºããã§ãã¯ããæåã®ã¹ã±ãããæäŸããããšããŸã ã 確ãã«99ïŒ ã®ã±ãŒã¹ã§ããã§ååã§ãã
ACLãšã¯äœã§ããïŒ
ããããæ確ãªèª¬æã¯ãããŸããã ãŠã£ãããã£ã¢ã§èŠã€ããããšãã§ããŸãã ããã«ä¹Ÿç¥ããŸãã ACLã¯ã¢ã¯ã»ã¹ã«ãŒã«ã®åãªããªããžããªã§ããããã©ãŒã ã®ããŒã«é¢æ°acl_checkãé©çšã§ããŸãã
acl_check(@user, @action_name)
ã§ããã®ã¯ãtrueãŸãã¯falseã redãŸãã¯blue ãgoodãŸãã¯evilã0ãŸãã¯1ãè¿ãããšã ãã§ãã
ä»ã®ACLã·ã¹ãã ãšåæ§ã«ãTheRoleã¯ã«ãŒã«ã¹ãã¢ã«å¯ŸããŠacl_checkãæäŸããŸãïŒã¢ã¯ã»ã¹ã«ãŒã«ãJSONæååãšããŠããŒã¿ããŒã¹ã«ä¿åããŸãïŒã ç¹å¥ãªããšã¯äœããããŸããã ãã ããTheRoleãã«ãŒã«ã¹ãã¬ãŒãžãæŽçããæ¹æ³ãšãã®çç±ãç¥ãããå ŽåããããŸãã
ACLãä¿åããããã®æè»ãªããŒã¿æ§é
æåãããããŒã¿ããŒã¹ã«ã¢ã¯ã»ã¹å¶åŸ¡ãªã¹ããä¿åãããã®ãªã¹ãã管çããæè»ãªæ段ãæäŸãããå Žåãã¹ããã¯ããŒãã«ã®åœ¢åŒã§ããŒã¿ãä¿åããã®ã¯ããŸã䟿å©ã§ã¯ãªããšããçµè«ã«éããŸããã ã¢ã¯ã»ã¹ãªã¹ãã®ååŸãè¡ããšã®æŽæ°ãããã³ãã®ä»ã®ãã¹ãŠã®æäœã¯ãéåžžã«ã³ã¹ããããããŸãïŒã°ã©ãã£ã«ã«ã€ã³ã¿ãŒãã§ã€ã¹ã®èŠ³ç¹ããã®ã¿ã§ãã£ãŠãïŒã
ãã€ãŠPHPã§ãé£æ³é åã«æ³šæãåããŸãããé£æ³é åã¯ããªããžã§ã¯ãããæååãžããŸãã¯ãã®éã«ç°¡åã«å€æã§ããŸãã ãã®ãããªé åãã¯ã©ã€ã¢ã³ãäžã§äœæããããšã¯ç°¡åã§ããµãŒããŒäžã§ã¯ããã©ãŒã ãéä¿¡ããåŸãã«ãŒã«ã®é åãå®éã«æºåã§ããŸããã ç§ãããªããã°ãªããªãã£ãã®ã¯ããããæååã«å€æããŠããŒã¿ããŒã¹ã«ä¿åããããšã ãã§ããã ã¯ã©ã€ã¢ã³ãã§ã«ãŒã«ã®é åãæç»ãããµãŒããŒã§ããããæäœããããšã¯éåžžã«ç°¡åã§ããããšãå€æããŸããã
PHPã§ã¯ãé£æ³é åãæäœããããã«serialize / unserializeã䜿çšããŠããŸããã ã«ããŒã§ã¯ãJSONãšããã·ã¥ã䜿çšããŠããŸãã
ãã¹ãŠã¯éåžžã«ã·ã³ãã«ãªã¢ã¯ã»ã¹ãªã¹ãããå§ãŸããŸããã ããšãã°ããŠãŒã¶ãŒã¯æçš¿ãäœæã§ããŸãããã³ã¡ã³ãã³ã³ãããŒã«ããã«ïŒæ瀺çã«å®çŸ©ïŒã«ã¢ã¯ã»ã¹ã§ããããã©ãã¢ã«ãã ãç·šéã§ããŸããïŒæ瀺çã«å®çŸ©ãããã«ãŒã«ãååšããªãå Žåã¯falseïŒã
{ post_create: true, post_delete: true, comments_panel: false }
ã¢ãã¬ãŒã¿ãŒã¯æçš¿ã®äœæãšåé€ãã³ã¡ã³ãã³ã³ãããŒã«ããã«ãžã®ã¢ã¯ã»ã¹ã¯ã§ããŸããããã©ãã¢ã«ãã ã®ç·šéãã§ããŸãã
{ post_create: true, post_delete: true, comments_panel: true }
MVCããã³ACLã 誰ããèŠãããã®ãèŠã
RORã®MVCå®è£ ã䜿çšããŠã ã³ã³ãããŒã©ãŒ/ã¢ã¯ã·ã§ã³ã®2ã¬ãã«æ§é ãæ¯æ¥ç¢ºèªããŸãïŒRORã®åã«ã³ã³ãããŒã©ãŒã®ã³ãŒãã¯åæ§ã«é 眮ãããŠããŸãããïŒãã³ã³ãããŒã©ãŒ/ã¢ã¯ã·ã§ã³ã®2ã¬ãã«æ§é ãã¢ã¯ã»ã¹å¶åŸ¡ãªã¹ãã«è»¢éããªãããšã¯éåžžã«å°é£ã§ãã èªæã¯ãšãŠã倧ããã®ã§ãæµæããããšã¯ã§ããŸããã§ããã ãã®ãããTheRoleã®æåã®å®è£ ã®ACLã¯ãã¹ãã¬ãŒãžçšã®æè»ãªããŒã¿åœ¢åŒã«å ããŠã2å±€æ§é ãåãåããŸããã
ããã¯ããŒãžãäœæã§ãããŠãŒã¶ãŒã®ããŒã«ã®ããã«èŠããŸãããäœããã®çç±ã§ããŒãžã®ç·šéãžã®ã¢ã¯ã»ã¹ã圌ã«å¶éãããŠããŸããã
pages: { index: true, show: true, new: true, create: true, edit: false, update: false, destroy: false }
TheRoleã2ã¬ãã«ã®ACLæ§é ãåãåããšããã«ãã¢ããªã±ãŒã·ã§ã³å ã®ã³ã³ãããŒã©ãŒã¢ã¯ã·ã§ã³ãžã®ã¢ã¯ã»ã¹ãå¶åŸ¡ããããšãéåžžã«ç°¡åã«ãªããŸããã ãããŠãããã¯ãååãšããŠãã¢ããªã±ãŒã·ã§ã³ã§æãæçšã§å¹æçãªãã§ãã¯ã®1ã€ã§ãã ãã®ãããªæ€èšŒã®ããã«ã¯ã before_filterã®ã¢ã¯ã»ã¹æ€èšŒã¡ãœãããåŒã³åºãã ãã§ååã§ããããã«ã¯ãã³ã³ãããŒã©ãŒã®ååãšã¢ã¯ã·ã§ã³ã®ååãæž¡ãããŸãã
return page_404 if not @user.has_role?(controller_name, action_name)
ã³ã³ãããŒã©ã®ã¢ã¯ã»ã¹ãã§ãã¯ãŠã©ãŒã¿ãŒãã©ãŒã«
ã ããã TheRoleã䜿çšãããšãç¹å®ã®ã¢ã¯ã·ã§ã³ã«å¯ŸãããŠãŒã¶ãŒã®ã¢ã¯ã»ã¹ã確èªã§ããŸãã ããããåé¡ãããæ éã«æ€èšãããšãããã¯ã³ã³ãããŒã©ãŒã«äžããããã¹ãã¢ã¯ã»ã¹ãã§ãã¯ã®1ã€ã«éããªãããšãããããŸãã
ã³ã³ãããŒã©ãŒã¢ã¯ã·ã§ã³ãžã®ã¢ã¯ã»ã¹ã®æåã®ãã§ãã¯ã¯ãèªèšŒgemã«ãã£ãŠå®è¡ãããŸãã ããšãã°ãDeviseãŸãã¯Sorceryã Devise gemã¯æ¬¡ã®ããã«æ©èœããŸãã
before_action :authenticate_user!, except: [:index, :show]
ã³ã³ãããŒã©ãŒã®ã¢ã¯ã·ã§ã³ãžã®ã¢ã¯ã»ã¹ã®2çªç®ã®ãã§ãã¯ã¯ãæš©éããã§ãã¯ãããŠãŒã¶ãŒãååšããããšã確å®ãªå Žåã«ã®ã¿å®è¡ããå¿ èŠããããŸãã ãã®ãããããšãã°ã æŽæ°ã¢ã¯ã·ã§ã³ã«ã¢ã¯ã»ã¹ããããšãããšãæåã®before_actionïŒauthenticate_userïŒ ãããŠããã®ãã£ã«ã¿ãŒãæåããå ŽåïŒã€ãŸãããŠãŒã¶ãŒãååšããå ŽåïŒãããã§æš©éãTheRole gemã«æ¢ã«è»¢éã§ããŸãã
before_action :role_required, except: [:index, :show]
role_requiredã¯ãå éšçã«current_user.has_roleïŒïŒcontroller_nameãaction_nameïŒã®åœ¢åŒã®ãã§ãã¯ãåŒã³åºãããŠãŒã¶ãŒã«å¿ èŠãªæš©éããªãå Žåã«ã¢ã¯ã»ã¹ãšã©ãŒã®ããããŒãžã衚瀺ããã¡ãœããã§ã ã
ãªããžã§ã¯ãã®æææš©ã®3çªç®ã®ãã§ã㯠ã ãªããžã§ã¯ããææããªããšããã®ãªããžã§ã¯ããå€æŽïŒåé€ãŸãã¯ç·šéïŒã§ããã³ã³ãããŒã©ãŒã¢ã¯ã·ã§ã³ã«ã¢ã¯ã»ã¹ã§ããŸããã ãã ãããªããžã§ã¯ãããããŸã§ãã®ãã§ãã¯ãå®è¡ã§ããŸããã ã€ãŸããæåã«ãªããžã§ã¯ããèŠã€ããå¿ èŠããããŸãã
before_action :set_page, only: [:edit, :update, :destroy]
ãªããžã§ã¯ãæ€çŽ¢ã¯ãéãããæ°ã®ã³ã³ãããŒã©ãŒã¢ã¯ã·ã§ã³ã§å®è¡ãããããšãããããŸãã TheRole gemãä»ããŠæææš©ãã§ãã¯ãè¡ãã®ãçã«ããªã£ãŠããã®ã¯ããããã®ã¢ã¯ã·ã§ã³ã®ã¿ã§ãã
before_action :owner_required, only: [:edit, :update, :destroy]
set_pageã¡ãœãããããèŠã€ãã£ããªããžã§ã¯ããowner_requiredã¡ãœããã«æž¡ããŠæææš©ã確èªããå¿ èŠãããããšã«æ³šæããŠãã ããã ããã¯for_ownership_checkã¡ãœããã䜿çšããŠè¡ãããŸãã
ãã®çµæãããªãä¿¡é Œæ§ã®é«ãã¢ã¯ã»ã¹å¶éã·ã¹ãã ãåãã次ã®ã³ã³ãããŒã©ãŒãã³ãã¬ãŒããååŸããŸãã
class PagesController < ApplicationController before_action :authenticate_user!, except: [:index, :show] before_action :role_required, except: [:index, :show] before_action :set_page, only: [:edit, :update, :destroy] before_action :owner_required, only: [:edit, :update, :destroy] # ... code ... private def set_page @page = Page.find params[:id] for_ownership_check(@page) end end
ä»®æ³ã»ã¯ã·ã§ã³ãšã«ãŒã«
ACLã2ã¬ãã«ã®é åãšããŠæ瀺ããããšã§ãæåã®ã¬ãã«ã¯ã«ãŒã«ã®ã»ã¯ã·ã§ã³ïŒã°ã«ãŒãïŒãè¡šãã2çªç®ã®ã«ãŒã«ã¯å¯Ÿå¿ããããŒã«å€ãæã€ã«ãŒã«ã§ãããããŒã«ã·ã¹ãã ãã¢ããªã±ãŒã·ã§ã³ã³ã³ãããŒã©ãŒã«éåžžã«æ£ç¢ºã«çµ±åããããšãã§ããŸããã ããããã³ã³ãããŒã©ã®ã¢ã¯ã·ã§ã³ãžã®ã¢ã¯ã»ã¹ãå¶åŸ¡ããããšã«ãã£ãŠã®ã¿å¶éããããšã¯ã§ããŸããã
ACLããã€ã¹ã¯ã¢ããªã±ãŒã·ã§ã³ã³ã³ãããŒã©ãŒã®å®éã®ããã€ã¹ãéåžžã«æ£ç¢ºã«åæ ã§ãããšããäºå®ã«ãããããããããã¯ACLã®ãã¹ãŠã®ã»ã¯ã·ã§ã³ãšã«ãŒã«ãã¢ããªã±ãŒã·ã§ã³ã®ããã€ã¹ãšæ£ç¢ºã«äžèŽããå¿ èŠããããšããæå³ã§ã¯ãããŸããã ACLã«äŸ¿å©ãªã«ãŒã«ã°ã«ãŒãã絶察ã«äœæããèªç±ã«äœ¿çšã§ããŸãã ãã®ãããªã«ãŒã«ã®ã°ã«ãŒãã¯ãã³ãŒãã®å®éã®æ§é ãåæ ããŠããªããšããäºå®ã«åºã¥ããŠã ä»®æ³ãšåŒã³ãŸãããè«ççãªæå³ã®ã¿ãä»äžãããŠããŸãã
以äžã¯ãã³ã³ãããŒã©ãŒã¢ã¯ã·ã§ã³ãžã®ã¢ã¯ã»ã¹ãå¶åŸ¡ããããŒãžäžã®ãœãŒã·ã£ã«ãã¿ã³ã®è¡šç€ºãå¶åŸ¡ããããã«äœ¿çšã§ãããŠãŒã¶ãŒããŒã«ã®äŸã§ãã
pages: { index: true, show: true, new: true, create: true, edit: true, update: true, destroy: true }, social_buttons: { vk: false, twitter: true, facebook: true },
ãã®ãããªã¢ã¯ã»ã¹ãªã¹ãã®èªã¿åãã¯ãã»ã¯ã·ã§ã³ãšã«ãŒã«ã«åå¥ã®ååãä»ããããšã«æ³šæããã°ãéåžžã«ç°¡åã§ãã ããã§ããã®ããŒã«ãæã€ãŠãŒã¶ãŒã¯Pagesã³ã³ãããŒã©ãŒã§åºæ¬çãªã¢ã¯ã·ã§ã³ãå®è¡ã§ããããã«TwitterãšFacebookã®ãœãŒã·ã£ã«ãã¿ã³ãæäœã§ããããšãããããŸãã ããããäœããã®çç±ã§ããŠãŒã¶ãŒã¯Vkontakteãã¿ã³ã䜿çšã§ããŸããã
TheRoleã®ã³ã³ãããŒã©ãŒãžã®çµ±åãããã£ãå ŽåãViewãšã®çµ±åã¯ããã«ç°¡åã§ãã
- if current_user - if current_user.has_role?(:social_buttons, :vk) = link_to "Like with VK", "#" - if current_user.has_role?(:social_buttons, :twitter) = link_to "Like with TW", "#" - if current_user.has_role?(:social_buttons, :facebook) = link_to "Like with Fb", "#"
ç¹å¥ãªä»®æ³ã»ã¯ã·ã§ã³ïŒã·ã¹ãã ãšã¢ãã¬ãŒã¿ãŒ
ã¹ãŒããŒãŠãŒã¶ãŒãšã¢ãã¬ãŒã¿ãŒã圹å²ã·ã¹ãã ã«è¿ éãã€ç°¡åã«å°å ¥ããæ¹æ³ã«ã€ããŠã®è³ªåã«å°æããã«ã¯ããããŸããã§ããã TheRole 2ã§ç¹ã«éèŠãªä»®æ³ã»ã¯ã·ã§ã³ã玹ä»ããŸããã ããæå³ã§ã¯ããã®æ±ºå®ã¯æŸèæãšèŠãªãããšãã§ããŸãããæªãããšã¯èŠåœãããŸããã ããã¯äžè¬çãªèãæ¹ã®çµ±äžã«éåããŸããã
ã¢ã¯ã»ã¹ã«ãŒã«ã®ãªã¹ãã«ã·ã¹ãã ã»ã¯ã·ã§ã³ãšç®¡çè ããããŠãŒã¶ãŒïŒçã®ã«ãŒã«ã¯ãªããžã§ã¯ãã®ææè ãšèŠãªãããã¢ã¯ã»ã¹ãªã¯ãšã¹ãã§åžžã«çãåãåããŸãã
system: { administrator: true }
ã¢ãã¬ãŒã¿ãŒã»ã¯ã·ã§ã³ãæã€ãŠãŒã¶ãŒã¯ãèªåã®ã«ãŒã«ã»ããã§æå®ãããŠããtrueã®ã»ã¯ã·ã§ã³ã«ãŒã«ãžã®ãã¹ãŠã®èŠæ±ã«å¿ããŠtrueãåãåããŸãã
moderator: { pages: true, blogs: false, twitter: true }
ã€ãŸã user.has_roleïŒïŒïŒpages ,: blablaïŒãuser.has_roleïŒïŒïŒtwitter , : blablaïŒãªã©ã®ã¯ãšãªã§ã¯ããã®ãŠãŒã¶ãŒã¯åžžã«trueã«ãªããŸãã ãããã user.has_roleïŒïŒïŒBlogs :: blablaïŒã®ãããªãªã¯ãšã¹ãã¯ããã®ãŠãŒã¶ãŒãããã°ã»ã¯ã·ã§ã³ã§æã€èš±å¯ãäžããŸãã ã€ãŸã ããã°ã§äœæ¥ããå Žåããã®ãŠãŒã¶ãŒã«ã¯ç¹æš©ããããŸããã
ACLã³ã³ãããŒã«ããã«
ããã§ãå®ç³å šäœã®æ©èœã®æ¬è³ªãæããã«ãªããã³ã³ãããŒã«ããã«ãèŠãããšãã§ããŸãã
ããŒã«ç®¡çããã«
ã³ã³ãããŒã«ããã«ã¯å¥ã®gemã«ãã£ãŠå®è£ ãããå¿ èŠã«å¿ããŠã¢ããªã±ãŒã·ã§ã³ã«ã€ã³ã¹ããŒã«ãããªãå ŽåããããŸãã ããããäžè¬çãªã±ãŒã¹ã§ã¯ãã€ã³ã¹ããŒã«ããã®ãçã«ããªã£ãŠãããšæããŸãã
ã³ã³ãããŒã«ããã«ã«ã¯æ¬¡ã®ãã®ããããŸãã
- æ°ãã空ã®åœ¹å²ãäœæãã
- æ¢åã®åœ¹å²ã«åºã¥ããŠæ°ãã圹å²ãäœæãã
- ãã®åœ¹å²ã«é¢ããæ å ±ã®ç·šé
- ç¹å®ã®ããŒã«å ã§æ°ããã»ã¯ã·ã§ã³ãäœæããã³åé€ããŸã
- ç¹å®ã®åœ¹å²ã»ã¯ã·ã§ã³å ã§æ°ããã«ãŒã«ãäœæããã³åé€ããŸã
- 1ã€ãŸãã¯ãã¹ãŠã®ã·ã¹ãã ããŒã«ãJSONãã¡ã€ã«ã«ã¢ã³ããŒããã
- ããŒã«ãå«ãJSONãã¡ã€ã«ã®èªã¿èŸŒã¿
ACLãããã¯ã¢ããããå¿ èŠãããå Žåã圹å²ã®ã€ã³ããŒã/ãšã¯ã¹ããŒãã圹ç«ã¡ãŸãã ãŸãã¯ãããšãã°ãTheRoleã䜿çšããŠãã«ã¹ã¿ãã€ãºããã圹å²ãè€æ°ã®ãããžã§ã¯ãéã§ç§»åããŸãã
éãããæè»æ§
äžæ¹ã§ãTheRoleã䜿çšãããšãã¢ã¯ã»ã¹ãã§ãã¯ã§äœ¿çšããã«ãŒã«ãäœæã§ããŸããäžè²«æ§ãããå Žåããããã®ã«ãŒã«ã¯ãã¢ããªã±ãŒã·ã§ã³ã§çºçããŠããããšãæå³çã«åæ ããŸãã äžæ¹ãTheRoleã«ã¯ãŸã 泚æãã¹ãå€ãã®å¶éããããŸãã
- äºåã«å®çŸ©ãããã¢ã¯ã»ã¹å¶åŸ¡ãã¹ã¯ãéãããAPIãä»ããŠã®ã¿äœ¿çšã§ããŸãïŒããã¯è¯ãããšã ãšæããŸãïŒ
- ãŠãŒã¶ãŒã®åœ¹å²ã¯1ã€ã®ã¿ã§ã ïŒããã¯ç§ã®åºæ¬çãªç«å Žã§ãããæè¡çãªå®è£ ã¯å€æŽãããŠããŸããïŒ
- TheRoleã¯Userã¢ãã«ã§ã®ã¿æ©èœããŸã ã ïŒæéã®åé¡ãšç©æ¥µçãªè²¢ç®è ïŒ
- TheRoleã¯current_userãã«ããŒã§ã®ã¿æ©èœããŸã ã ïŒæéã®åé¡ãšç©æ¥µçãªè²¢ç®è ïŒ
- mongoããµããŒãããŠããŸããã ïŒæéã®åé¡ãšç©æ¥µçãªè²¢ç®è ïŒ
- 3ã€ã®SQLã©ã€ã¯ãªããŒã¿ããŒã¹ïŒsqliteãmysqlãpsqlïŒã®ã¿ããµããŒã
- ãã€ãã£ãã®jsonããŒã¿ããŒã¹åã®äœ¿çšãæå³ãããã®ã§ã¯ãªããJSONãããã¹ããšããŠã®ã¿ããŒã¿ããŒã¹ã«ä¿åããŸãã ïŒã¯ããPSQLçšã®ãããã¯ãããŸãããç¹å®ã®åäœã§ã³ãŒããè©°ãŸãããããªããšããé¡æãèæ ®ããŠãgemã®æšæºé ä¿¡ã«ã¯ãããå«ããŸããïŒ
ããã§ããã ããããå°ãªããšããŠãŒã¶ãŒã«è€æ°ã®åœ¹å²ãåæã«æãããããšã¯å¯èœã§ããïŒ ç³ãèš³ãããŸãããããããŸããã ããã¯æªè³ªãªã¢ãããŒãã§ãã å€æ°ã®è«ççãªæåŸ ã«é¢é£ä»ããããŠããã人ã«ãã£ãŠãŸã£ããç°ãªãå ŽåããããŸãã
1人ã®ãŠãŒã¶ãŒã«å€ãã®ããŒã«ãæäŸããã·ã¹ãã ãå¿ èŠãªå Žåãããã¯ãããªããã²ã©ãééããŠããããTheRoleãéåžžã«äžé©åã§ããããšãæå³ããŸãã
è¬èŸ
æšå¹Ž2014幎ã¯ç§ã«ãšã£ãŠéåžžã«æåããŸããããªã³ã©ã€ã³åŠç¿ãšããå£å®ã®äžã§ãããã°ã©ãã³ã°ã«æ ç±ãåŸããæèœã®ãã人ã ãšåºäŒããåéã«ãªããŸããã ãŠã©ãžãªã¹ããã¯ããããšããŸã§ãå°çã¯åºå€§ã§ãã ãããŠãåé¡ã解決ããã¢ã€ãã¢ãå®è£ ããããã«ã人ã ãæèçã«ã«ããŒæè¡ãéžæããããšãå¿ããå¬ããæããŸãã ç§ãã¡ããªãŒãã³ãœãŒã¹ãããžã§ã¯ãã®äžéšãšããŠäœããäžç·ã«ããããšãã§ããŠãšãŠãå¬ããã§ãã
1ïŒãžã§ã ã®3åç®ã®ãªãªãŒã¹ãè¡ãããããã«ãããå€ãã®åªåãšåªåã泚ãã ãããã®äººã ã®1人ã«æè¬ããããšæããŸãã ãã®äººã®ååã¯ã€ãªã€ã»ãã³ãã¬ã³ã³ã§ ã圌ã¯ãã«ãã«äœãã§ããŠãç§ã®ç¥ãéããã¹ã¿ãŒãšããŠåããŠããŸãã ççŽã«èšã£ãŠãç§ã¯ã€ãªã€ã圌ã瀺ããä»äºãšç±æã®çšåºŠãçŽ æ©ã解決ã§ããããšã«å¬ããé©ããŸããã ã³ã©ãã¬ãŒã·ã§ã³ã®äžç°ãšããŠãIlyaã¯ãã¹ãã®å®å šãªåèšèšãã³ãŒãæ§é ã®éèŠãªå€æã®å®è¡ãããã€ãã®éèŠãªãã°ã®ä¿®æ£ãããã¥ã¡ã³ãã®æ¹åãããŒããããgemã§ã®å€æ°ã®ææ¡ãæ¯æŽããŸããã ã€ãªã€ ããã®ã¬ãã¥ãŒãããªãã®ãã£ãªã¢ã®èŠ³ç¹ããããªãã«åœ¹ç«ã€ãã©ããã¯ããããŸããããããã§ããé«å質ãªæ¹æ³ã§ã¿ã¹ã¯ãå®è¡ããåªããçµæãéæããæ¹æ³ãç¥ã£ãŠãã人ãšããŠäžè¬ã«æšèŠããããšãã§ããŸãã ã©ããããããšãïŒ
2ïŒããã«ã Sergey Fuchsmanã«ãæè¬ããŸãã ã©ããããSergeyã¯ãTheRole 2ããTheRole 3ã«ç§»è¡ããå°ããªåé¡ã«ééããæåã®ãŠãŒã¶ãŒã®1人ã§ããã ã»ã«ã²ã€ã貎éãªãã£ãŒãããã¯ãšTheRoleãžã®ä¿¡é Œã«æè¬ããŸãã
å®äº
ããã«ã€ããŠã¯ãç§ãæãã§ãããã¹ãŠã®ããšã«ã€ããŠè©±ããŠããããã§ãã ææ決å®ã®åŠ¥åœæ§ãšæçšæ§ã«é¢ããçµè«ã ããããå°ãªããšãããªãã¯ãMVCæ§é ãæã€ãããžã§ã¯ãã®èªèšŒåé¡ã解決ããããã®ãã1ã€ã®ïŒ1001stïŒãªãã·ã§ã³ãç¥ã£ãŠããŸãã
éçºã«é 匵ã£ãŠãã ããïŒ