thinkphpÔõôʹÓÃȨÏÞÈÏÖ¤
Ëæ×Å»¥ÁªÍøµÄÒ»Ö±Éú³¤£¬ÍøÕ¾Óû§ÊýÄ¿Ô½À´Ô½¶à£¬ÎªÁ˸üºÃµØÖÎÀíºÍ°ü¹ÜÓû§Êý¾ÝµÄÇå¾²£¬È¨ÏÞÈÏÖ¤³ÉΪÁËÿ¸öÍøÕ¾±Ø±¸µÄ¹¦Ð§Ö®Ò»¡£ÔÚphp¿ò¼ÜÖУ¬thinkphpÊÇÒ»¸öÊ®·ÖÊ¢ÐеĿò¼Ü£¬Ò²ÌṩÁËÍêÉƵÄȨÏÞÈÏÖ¤¹¦Ð§¡£ÄÇô£¬±¾ÎľÍÀ´ÏêϸÏÈÈÝÒ»ÏÂthinkphpÔõÑùʹÓÃȨÏÞÈÏÖ¤¡£
Ò»¡¢È¨ÏÞÈÏÖ¤µÄ×÷ÓÃ
ȨÏÞÈÏÖ¤Ö÷ÒªÊÇΪÁ˶Բî±ð½ÇÉ«µÄÓû§¾ÙÐÐȨÏÞ¿ØÖÆ£¬È·±£Óû§Ö»ÄÜ»á¼û×Ô¼ºÓÐȨ»á¼ûµÄ×ÊÔ´£¬¶ø²»¿ÉԽȨʹÓá£ÀýÈ磬ÔÚÒ»¸öµçÉÌÍøÕ¾ÖУ¬ÖÎÀíÔ±¿ÉÒÔÉó²éºÍÖÎÀíËùÓÐÉÌÆ·ÐÅÏ¢£¬¶øͨË×Óû§Ö»ÄÜä¯ÀÀÉÌÆ·ÐÅÏ¢£¬²»¿É¾ÙÐÐÐ޸ĺÍɾ³ýµÈ²Ù×÷¡£
¶þ¡¢È¨ÏÞÈÏÖ¤µÄʵÏÖ·½·¨
ThinkPHP¿ò¼ÜÌṩÁËÁ½ÖÖȨÏÞÈÏÖ¤µÄʵÏÖ·½·¨£ºRBACºÍ»ùÓÚ½ÚµãµÄȨÏÞÈÏÖ¤¡£RBAC£¨Role-Based Access Control£©£¬¼´»ùÓÚ½ÇÉ«µÄ»á¼û¿ØÖÆ£¬½«²î±ðµÄÓû§Æ¾Ö¤Æä½ÇÉ«¾ÙÐзÖÀà¡£¶ø»ùÓÚ½ÚµãµÄȨÏÞÈÏÖ¤£¬ÔòÊÇͨ¹ý½Úµã¾ÙÐÐȨÏÞ¿ØÖÆ£¬½Úµã¿ÉÒÔÊÇ¿ØÖÆÆ÷¡¢²Ù×÷ÒªÁìµÈ¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
RBAC
ÔÚ¿ò¼ÜÖÐͨ¹ýRBACʵÏÖȨÏÞÈÏÖ¤ÐèҪʹÓõ½AuthÀ࣬¸ÃÀàλÓÚThinkPHPLibraryThinkÖС£Ïêϸ²Ù×÷°ì·¨ÈçÏ£º
£¨1£©½¨Éè½Úµã±íºÍ½ÇÉ«±í£¬²¢½«È¨ÏÞ½ÚµãºÍ½ÇÉ«¾ÙÐйØÁª¡£½¨Éè½Úµã±í¿ÉÒÔ¶ÔÓ¦¿ØÖÆÆ÷ºÍ²Ù×÷ÒªÁ죬½ÇÉ«±íÔòÊÇÉúÑĽÇÉ«Ãû³ÆºÍ¶ÔÓ¦µÄ½ÚµãID£¬ÈçÏÂËùʾ£º
½Úµã±í£¨think_node£©£º
id | name | module | controller | action | pid |
---|---|---|---|---|---|
1 | index | home | index | index | 0 |
2 | add | home | index | add | 1 |
3 | edit | home | index | edit | 1 |
4 | delete | home | index | delete | 1 |
½ÇÉ«±í£¨think_role£©£º
id | name | node_ids |
---|---|---|
1 | admin | 1,2,3,4 |
2 | user | 1 |
£¨2£©ÔÚ¿ØÖÆÆ÷ÖÐʹÓÃAuthÀà¾ÙÐÐȨÏÞÈÏÖ¤£¬Ïêϸ´úÂëÈçÏ£º
class IndexController extends Controller
{
public function __construct() { parent::__construct(); //ʵÀý»¯AuthÀà $auth = new ThinkAuth(); //»ñÈ¡Ä¿½ñÓû§µÄ½ÇÉ«ID $uid = session('user_id'); //»ñÈ¡Ä¿½ñÇëÇóµÄ¿ØÖÆÆ÷ºÍÒªÁì $url = MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME; //¾ÙÐÐȨÏÞÈÏÖ¤ if (!$auth->check($url, $uid)) { $this->error('ÄúûÓлá¼û¸ÃÒ³ÃæµÄȨÏÞ£¡'); } }
µÇ¼ºó¸´ÖÆ
}
ÉÏÊö´úÂëÖУ¬Í¨¹ýʵÀý»¯AuthÀ࣬»ñÈ¡Ä¿½ñÓû§µÄ½ÇÉ«IDºÍÇëÇóµÄ¿ØÖÆÆ÷ºÍÒªÁ죬ÔÙʹÓÃ$auth->check()ÒªÁì¾ÙÐÐȨÏÞÈÏÖ¤¡£ÈôÊÇÑé֤Ƿºà¹ý£¬ÔòÊäÍÉ»¯ÎóÌáÐÑÐÅÏ¢¡£
»ùÓÚ½ÚµãµÄȨÏÞÈÏÖ¤
»ùÓÚ½ÚµãµÄȨÏÞÈÏÖ¤¿ÉÒÔʹÓÿò¼ÜÌṩµÄAccessÀàÀ´ÊµÏÖ£¬¸ÃÀàͬÑùλÓÚThinkPHPLibraryThinkÖС£Ïêϸ²Ù×÷°ì·¨ÈçÏ£º
£¨1£©ÔÚ¿ØÖÆÆ÷ÖÐʹÓÃAccessÀà¾ÙÐÐȨÏÞÈÏÖ¤£¬Ïêϸ´úÂëÈçÏ£º
class IndexController extends Controller
{
public function __construct() { parent::__construct(); //ʵÀý»¯AccessÀà $access = new ThinkAccess(); //»ñÈ¡Ä¿½ñÓû§µÄ½ÇÉ«ID $uid = session('user_id'); //»ñÈ¡Ä¿½ñÇëÇóµÄ¿ØÖÆÆ÷ºÍÒªÁì $url = MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME; //½ç˵ȨÏÞ½ÚµãÁбí $nodes = array( 'Index/index',//Ê×Ò³ 'Index/add',//Ìí¼ÓÒ³Ãæ 'Index/edit',//±à¼Ò³Ãæ 'Index/delete',//ɾ³ý²Ù×÷ ); //¾ÙÐÐȨÏÞÈÏÖ¤ if (!$access->check($nodes, $uid, $url)) { $this->error('ÄúûÓлá¼û¸ÃÒ³ÃæµÄȨÏÞ£¡'); } }
µÇ¼ºó¸´ÖÆ
}
ÉÏÊö´úÂëÖУ¬Í¨¹ýʵÀý»¯AccessÀ࣬»ñÈ¡Ä¿½ñÓû§µÄ½ÇÉ«IDºÍÇëÇóµÄ¿ØÖÆÆ÷ºÍÒªÁ죬ÔÙʹÓÃ$access->check()ÒªÁì¾ÙÐÐȨÏÞÈÏÖ¤¡£È¨ÏÞ½ÚµãÁбíÖеÄÿһÏî¶ÔÓ¦Ò»¸ö½Úµã£¬¼´¿ØÖÆÆ÷ºÍÒªÁì¡£ÈôÊÇÑé֤Ƿºà¹ý£¬ÔòÊäÍÉ»¯ÎóÌáÐÑÐÅÏ¢¡£
Èý¡¢È¨ÏÞÈÏÖ¤µÄÓÅ»¯
ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃÇ»¹ÐèÒª¶ÔȨÏÞÈÏÖ¤¾ÙÐÐһЩÓÅ»¯£¬ÒÔÌá¸ß´úÂëµÄ¸´ÓÃÐÔºÍÇå¾²ÐÔ¡£ÏêϸÓÅ»¯µãÈçÏ£º
½¨É蹫¹²¿ØÖÆÆ÷
¿ÉÒÔÔÚ¿ò¼ÜµÄCommonÄ£¿éÖУ¬½¨ÉèÒ»¸ö¹«¹²¿ØÖÆÆ÷BaseController£¬½«È¨ÏÞÑéÖ¤Âß¼°²ÅÅÔڸÿØÖÆÆ÷ÖС£ÆäËû¿ØÖÆÆ÷¼ÌÐø¸Ã¿ØÖÆÆ÷ºó£¬¼´¿ÉÖØÓøò¿·Ö´úÂë¡£
»º´æ½ÚµãÐÅÏ¢
ÔÚȨÏÞÈÏÖ¤ÖУ¬Ã¿´Î¶¼ÐèÒªÅÌÎʽڵã±íºÍ½ÇÉ«±í£¬»áÓ°ÏìϵͳÐÔÄÜ¡£Òò´Ë£¬¿ÉÒÔ½«ÅÌÎÊЧ¹û»º´æÆðÀ´£¬Ìá¸ßϵͳÐÔÄÜ¡£¿ÉÒÔʹÓÿò¼ÜÌṩµÄ»º´æÀ࣬½«ÅÌÎÊЧ¹û»º´æÆðÀ´£¬Ï´ÎÅÌÎÊʱ£¬Ö±½Ó»ñÈ¡»º´æЧ¹û¼´¿É¡£
¼ÓÃܽڵãÐÅÏ¢
ΪÁËÌá¸ßÇå¾²ÐÔ£¬ÎÒÃÇ¿ÉÒÔ¶Ô½ÚµãÐÅÏ¢¾ÙÐмÓÃÜ´¦Àí£¬×èÖ¹Ö±½Ó̻¶½ÚµãÐÅÏ¢¡£¿ÉÒÔʹÓÿò¼ÜÌṩµÄ¼ÓÃÜÀ࣬½«½ÚµãID¾ÙÐмÓÃÜ´¦Àí£¬²¢ÔÚ½ÇÉ«±íÖÐÉúÑļÓÃܺóµÄ½ÚµãÐÅÏ¢¡£
ËÄ¡¢×ܽá
ȨÏÞÈÏÖ¤ÊÇÍøÕ¾¿ª·¢²»¿Éȱ°±ÉÄÒ»²¿·Ö£¬ËüÄܹ»°ü¹ÜÓû§Êý¾ÝµÄÇå¾²£¬Ìá¸ß¶ÔÓû§µÄÖÎÀí¡£¶øÔÚThinkPHP¿ò¼ÜÖУ¬Ê¹ÓÃAuthÀàºÍAccessÀ࣬¿ÉÒÔºÜÊÇÀû±ãµØʵÏÖȨÏÞÈÏÖ¤¹¦Ð§¡£Í¨¹ý±¾ÎĵÄÏÈÈÝ£¬¸÷ÈË¿ÉÒÔÏàʶµ½ThinkPHPʹÓÃȨÏÞÈÏÖ¤µÄ°ì·¨ºÍÓÅ»¯ÒªÁ죬ÐÅÍÐÄܹ»¶Ô¸÷È˵Ŀª·¢ÊÂÇéÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇthinkphpÔõôʹÓÃȨÏÞÈÏÖ¤µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡