Before I wrote an example, about thinkphp RBAC rights control, and then studied the next, found in the real situation, a lot of crm,cms, there is a need to acquire RBAC all the modules, and then the rights to assign operations, or to increase the deletion module.
So I thought of a way to achieve the automatic acquisition of thinkphp all modules
The general idea is:
1, according to the configuration file to get the group
2, traverse the group under the Action folder in the *action.class.php
3. Instantiate the action, get all of its methods, filter out the underlying function of the TP itself,
4, a flick of a small hand, the data hands.
The following is the specific code, in fact, thinking more important, I am not sure that my is the best. I can not go to a line of explanation. We have time to study.
Generate module structure information app/grouping/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) {$data [$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 into 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)) return 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)) return null;
$action =a ($module);
$functions =get_class_methods ($action);
$inherents _functions = Array (' _initialize ', ' __construct ', ' getactionname ', ' isajax ', ' Display ', ' show ', ' fetch ', ' buildhtml ', ' Assign ', ' __set ', ' get ', ' __get ', ' __isset ', ' __call ', ' Error ', ' success ', ' Ajaxreturn ', ' Redi ReCT ', ' __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 ' unsigned NOT null auto_increment, ' name
' varchar () DEFAULT null COMMENT ' name ',
' app ' varchar () default NULL COMMENT ' project ',
' group ' varchar () default null COMMENT ' group ',
' module ' varch AR (=) default null COMMENT ' module ',
' function ' varchar (r) default null COMMENT ' method ',
' status ' varchar (r) Default N ull COMMENT ' state ',
PRIMARY KEY (' id ')
) Engine=innodb DEFAULT charset=utf8$$
This article is from the "尛 lei" blog, please be sure to keep this source http://a3147972.blog.51cto.com/2366547/1218183
See more highlights of this column: http://www.bianceng.cn/webkf/PHP/