thinkphp RBAC How to automatically get functions of all modules

Source: Internet
Author: User
Tags foreach empty

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/

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.