ThinkPHP6ÖÐÔõÑù¾ÙÐÐRBACȨÏÞÖÎÀí£¿
Ëæ×Å»¥ÁªÍøµÄÒ»Ö±Éú³¤ºÍÓ¦ÓõÄÆÕ±éÔËÓã¬Ô½À´Ô½¶àµÄÍøÕ¾ºÍÓ¦ÓóÌÐòÒªÇóʵÏÖ»á¼ûȨÏ޵ĿØÖÆ£¬ÒÔÈ·±£Ãô¸ÐÐÅÏ¢ºÍ×ÊÔ´µÄÇå¾²ÐÔ¡£¶øËæ×ÅÏîÄ¿µÄÒ»Ö±Éú³¤ºÍ¹¦Ð§µÄÒ»Ö±ÔöÌí£¬rbacȨÏÞÖÎÀíϵͳÒѾ³ÉΪÁËÒ»ÖÖºÜÊÇÊ¢ÐкͳÉÊìµÄ½â¾ö¼Æ»®¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùÔÚthinkphp6¿ò¼ÜÖÐʹÓÃrbac¾ÙÐÐȨÏÞÖÎÀí¡£
ʲôÊÇRBACȨÏÞÖÎÀí£¿
RBAC£¨Role-Based Access Control£©È¨ÏÞÖÎÀíÄ£×ÓÊÇÒ»ÖÖÒÔ½ÇɫΪÖÐÐĵĻá¼û¿ØÖÆÄ£×Ó¡£Ëü½«Óû§·ÖÅɵ½²î±ðµÄ½ÇÉ«ÖУ¬²¢Í¨¹ý½ÇÉ«À´¿ØÖÆÓû§¶Ô×ÊÔ´µÄ»á¼û¡£Í¨¹ýÕâÖÖ·½·¨£¬ÖÎÀíÔ±¿ÉÒÔ¸üÀû±ãµØÖÎÆÊÎö¼ûȨÏÞ£¬¶ø²»±ØÕë¶Ôÿ¸öÓû§¾ÙÐÐȨÏÞµÄÉèÖúÍÖÎÀí¡£
RBACȨÏÞÖÎÀíµÄ×é³É²¿·Ö
RBACȨÏÞÖÎÀíÄ£×ÓÖ÷ÒªÓÉÒÔÏÂËĸö×é³É²¿·Ö×é³É£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
1.½ÇÉ«£¨Role£©£º½ÇÉ«ÊÇÒ»×éÓû§È¨ÏÞµÄÜöÝÍ£¬¿ÉÒÔ½«Ò»Ð©ÏàËƵÄȨÏÞ¹éΪһ¸ö½ÇÉ«£¬²¢Í¨¹ý¸Ã½ÇÉ«À´¿ØÖÆÓû§¶Ô×ÊÔ´µÄ»á¼û¡£
2.Óû§£¨User£©£ºÓû§ÊÇָʹÓÃϵͳµÄÖ°Ô±£¬Ã¿¸öÓû§¶¼¿ÉÒÔÓµÓÐÒ»¸ö»ò¶à¸ö½ÇÉ«¡£
3.ȨÏÞ£¨Permission£©£ºÈ¨ÏÞÖ¸µÄÊÇÓû§»ò½ÇÉ«¶Ô×ÊÔ´µÄ²Ù×÷ÐÐΪ£¬¿ÉÒÔÊÇϵͳÄÚÖõIJÙ×÷£¬Ò²¿ÉÒÔÊÇÓû§×Ô½ç˵µÄ²Ù×÷¡£
4.×ÊÔ´£¨Resource£©£º×ÊÔ´ÊÇÖ¸ÐèÒªÊܵ½»á¼û¿ØÖƵŤ¾ß£¬ÀýÈçϵͳÖеÄij¸öÒ³Ãæ¡¢Îļþ»òÊý¾Ý¿â¼Í¼µÈ¡£
ÔÚRBACȨÏÞÖÎÀíÄ£×ÓÖУ¬Óû§Í¨¹ý½ÇÉ«À´»ñȡȨÏÞ£¬Í¨¹ýȨÏÞÀ´»á¼û×ÊÔ´£¬¶øÖÎÀíÔ±¿ÉÒÔͨ¹ý½ÇÉ«ºÍȨÏÞµÄÉèÖÃÀ´¿ØÖÆÓû§¶Ô×ÊÔ´µÄ»á¼û¡£
ÔÚThinkPHP6¿ò¼ÜÖÐʵÏÖRBACȨÏÞÖÎÀí
ÔÚThinkPHP6¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ý´î½¨Ò»¸ö¼òÆÓµÄRBACȨÏÞÖÎÀíϵͳÀ´Ñ§Ï°ºÍʹÓÃRBACȨÏÞÖÎÀíÄ£×Ó¡£
ÒÔÏÂÊÇÔÚThinkPHP6¿ò¼ÜÖÐʵÏÖRBACȨÏÞÖÎÀíµÄ´óÖ°취£º
1.½¨ÉèÊý¾Ý¿â
½¨ÉèÊý¾Ý±í²¢ÌîÈëһЩÊý¾Ý£¬°üÀ¨½ÇÉ«¡¢Óû§¡¢È¨ÏÞºÍ×ÊÔ´µÈÐÅÏ¢¡£ÀýÈç¿ÉÒÔ½¨ÉèÒÔÏÂËĸö±í£º
½ÇÉ«±í£¨role£©£º°üÀ¨½ÇÉ«IDºÍ½ÇÉ«Ãû³ÆµÈ×ֶΡ£
Óû§±í£¨user£©£º°üÀ¨Óû§ID¡¢Óû§ÃûºÍÃÜÂëµÈ×ֶΡ£
ȨÏÞ±í£¨permission£©£º°üÀ¨È¨ÏÞIDºÍȨÏÞÃû³ÆµÈ×ֶΡ£
×ÊÔ´±í£¨resource£©£º°üÀ¨×ÊÔ´IDºÍ×ÊԴ·¾¶µÈ×ֶΡ£
2.×°ÖÃRBACȨÏÞÖÎÀí×é¼þ
ʹÓÃcomposer×°ÖÃRBACȨÏÞÖÎÀí×é¼þ£¬ÔÚÖÕ¶ËÖÐÊäÈëÒÔÏÂÏÂÁ
composer require jiaminglu/rbac
3.¾ÙÐÐÉèÖÃ
¾ÙÐÐÏà¹ØȨÏÞ¿ØÖÆ¡¢Â·ÓɺÍÖÐÐļþµÄÉèÖá£
ÔÚconfig/auth.phpÎļþÖоÙÐÐÈÏÖ¤µÄÉèÖ᣶Ô×ÊÔ´µÄ¿ØÖÆ¿ÉÒÔʹÓÃÖÐÐļþµÄ·½·¨£¬ÔÚconfig/middleware.phpÎļþÖоÙÐÐÏà¹ØÉèÖá£
4.±àдÏìÓ¦µÄÄ£×Ó¡¢¿ØÖÆÆ÷ºÍÊÓͼ
±àд½ÇÉ«¡¢Óû§¡¢È¨ÏÞºÍ×ÊÔ´µÈÄ£×Ó£¬ÔÚ¿ØÖÆÆ÷Öд¦Öóͷ£È¨ÏÞ¿ØÖƵÄÏà¹ØÂß¼£¬ÔÚÊÓͼÖÐÕ¹Êլû§²Ù×÷µÄЧ¹ûºÍ״̬¡£
ÆäÖУ¬ÎªÁËÀû±ãÑÝʾ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ¿ØÖÆÆ÷£º
RoleController£¨½ÇÉ«ÖÎÀí£©£º°üÀ¨Éó²é½ÇÉ«¡¢Ìí¼Ó½ÇÉ«¡¢Ð޸ĽÇÉ«ºÍɾ³ý½ÇÉ«µÈ²Ù×÷¡£ÔÚÐ޸ĽÇɫʱ¿ÉÒÔÉèÖÃÆäÓµÓеÄÓû§ºÍȨÏÞ¡£
UserController£¨Óû§ÖÎÀí£©£º°üÀ¨Éó²éÓû§¡¢Ìí¼ÓÓû§¡¢ÐÞ¸ÄÓû§ºÍɾ³ýÓû§µÈ²Ù×÷¡£ÔÚÐÞ¸ÄÓû§Ê±¿ÉÒÔΪÆä·ÖÅɽÇÉ«¡£
PermissionController£¨È¨ÏÞÖÎÀí£©£º°üÀ¨Éó²éȨÏÞ¡¢Ìí¼ÓȨÏÞ¡¢ÐÞ¸ÄȨÏÞºÍɾ³ýȨÏ޵ȲÙ×÷¡£
ResourceController£¨×ÊÔ´ÖÎÀí£©£º°üÀ¨Éó²é×ÊÔ´¡¢Ìí¼Ó×ÊÔ´¡¢ÐÞ¸Ä×ÊÔ´ºÍɾ³ý×ÊÔ´µÈ²Ù×÷¡£
ÒÔÉÏÊÇÔÚThinkPHP6¿ò¼ÜÖоÙÐÐRBACȨÏÞÖÎÀíµÄÖ÷Òª°ì·¨¡£ÔÚÏÖʵ¿ª·¢Àú³ÌÖУ¬ÎÒÃÇÐèҪƾ֤ÏÖʵÐèÇóÀ´¾ÙÐÐ×é¼þµÄÉèÖúʹúÂëµÄ±àд£¬ÒÔʵÏÖÔ½·¢ÍêÕûºÍÇå¾²µÄȨÏÞÖÎÀíϵͳ¡£
×ܽá
RBACȨÏÞÖÎÀíÄ£×ÓÊÇÒ»ÖÖºÜÊÇÊ¢ÐкͳÉÊìµÄȨÏÞÖÎÃ÷È·¾ö¼Æ»®£¬¿ÉÒÔ×ÊÖúÖÎÀíÔ±¸üÀû±ãµØÖÎÆÊÎö¼ûȨÏÞ£¬ÔÚÈ·±£ÏµÍ³Çå¾²ÐÔµÄͬʱ£¬Ìá¸ßϵͳʹÓõÄЧÂʺͱã½ÝÐÔ¡£
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔÚThinkPHP6¿ò¼ÜÖÐʵÏÖRBACȨÏÞÖÎÀíµÄÖ÷Òª°ì·¨ºÍÏà¹ØÄ£×Ó¡¢¿ØÖÆÆ÷ºÍÊÓͼµÄ±àд¡£Í¨¹ý±¾ÎĵÄÏÈÈݺÍÏÖʵ²Ù×÷£¬ÐÅÍжÁÕß¿ÉÒÔ¸üÇåÎúµØÃ÷È·ºÍʹÓÃRBACȨÏÞÖÎÀíÄ£×Ó£¬´Ó¶øΪ×Ô¼ºµÄÏîÄ¿¿ª·¢ÖÐÔöÌí¸ü¶àµÄÇå¾²ÐԺ͸ßЧÐÔ¡£
ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑù¾ÙÐÐRBACȨÏÞÖÎÀí£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡