How does ThinkPHP RBAC automatically obtain functions of all modules?

Source: Internet
Author: User

I have previously written an example about ThinkPHP RBAC permission control. Later I studied ThinkPHP and found that in actual situations, many crm, cms, etc, you need to obtain all RBAC modules, perform permission assignment, or add or delete modules.

So I thought of an idea to implement automatic acquisition of all ThinkPHP modules.

The general idea is:

1. Get the group according to the configuration file
2. Traverse * Action. class. php In the Action folder of the group.
3. instantiate an Action, obtain all its methods, and filter out the underlying functions of tp,
4. With a small hand, you can get the data.

The following is the specific code. In fact, the idea is more important, and I cannot be sure that I am the best. It is also impossible for me to explain in one line. You have time to study it.

// Generate Module structure information app/group/Module/method public function fetch_module () {$ M = M ('module '); $ M-> query ("truncate table module"); $ app = $ this-> getAppName (); $ groups = $ this-> getGroup (); $ n = 0; foreach ($ groups as $ group) {$ modules = $ this-> getModule ($ group); foreach ($ modules as $ module) {$ module_name = $ app. '://'. $ group. '/'. $ module; $ functions = $ this-> getFunction ($ module_name); foreach ($ functions as $ function) {$ d Ata [$ n] ['app'] = $ app; $ data [$ n] ['group'] = $ group; $ data [$ n] ['module'] = $ module; $ data [$ n] ['function'] = $ function; ++ $ n ;}}} $ M-> addAll ($ data); $ this-> success ('all groups/modules/methods have been successfully read to the module table. ');} protected function getAppName () {return APP_NAME;} protected function getGroup () {$ result = explode (', ', C ('app _ GROUP_LIST ')); return $ result;} protected function getModule ($ group) {if (empty ($ group) retu Rn null; $ group_path = LIB_PATH. 'Action/'. $ group; if (! Is_dir ($ group_path) return null; $ group_path. = '/*. class. php '; $ ary_files = glob ($ group_path); foreach ($ ary_files as $ file) {if (is_dir ($ file) {continue ;} else {$ files [] = basename ($ file, 'Action. class. php ') ;}} return $ files;} protected function getFunction ($ module) {if (empty ($ module) return null; $ action = A ($ module ); $ functions = get_class_methods ($ action); $ inherents_functions = array ('_ initi Alize', '_ construct', 'getactionname', 'isajax ', 'display', 'show', 'fetch ', 'buildhtml', 'assign ', '_ set', 'get',' _ get', '_ isset',' _ call', 'error', 'success', 'ajaxreturn ', 'redirect', '_ destruct'); foreach ($ functions as $ func) {if (! In_array ($ func, $ inherents_functions) {$ customer_functions [] = $ func ;}return $ customer_functions ;}

Structure of the Model Table

Create table 'module' ('id' int (10) unsigned not null AUTO_INCREMENT, 'name' varchar (45) default null comment 'name', 'app' varchar (45) default null comment 'project', 'group' varchar (45) default null comment' group', 'module' varchar (45) default null comment' module ', 'function' varchar (45) default null comment 'method', 'status' varchar (45) default null comment 'status', primary key ('id ')) ENGINE = InnoDB default charset = utf8 $


Friends who like to discuss can join a group of 252799167

This article is from the "Thunder" blog, please be sure to keep this source http://a3147972.blog.51cto.com/2366547/1218183

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.