起因:
thinkcmf的项目,权限认证是方法级的,每个方法必须先添加权限并配置,其他管理员才能访问这个方法。
于是:
想了想决定先获取当前所有的方法,和所有已经权限的方法看看。
环境:
thinkphp3.2,thinkcmf,php5.4
上代码
1 <?php 2 namespace Admin\Controller; 3 use Common\Controller\AdminbaseController; 4 echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> "; 5 class TestController extends AdminbaseController { 6 7 public function index() 8 { 9 10 $rule=M("")->where("")->field("name,title")->select(); //取出所有权限 11 12 $rule = $this->array_column($rule, 'title','name'); 13 14 $this->tree("当前文件夹地址",$rule); 15 16 } 17 public function tree($directory,$rule) 18 { 19 20 $mydir = dir($directory); 21 22 //计数,为了去掉 . 和 .. 地址 23 $i=1; 24 25 // echo "<br>不计入权限的方法"; 26 $actionarray=array('__construct', 27 '...................' 28 ,'_lists'); 29 30 // "不计入权限的控制器用于手动筛选同目录下不同命名空间的控制器; 31 $controllerarray=array('控制器名','控制器名'); 32 33 34 $j=0; 35 $k=0; 36 $p=0; 37 38 //开始读取数据 39 while($file = $mydir->read()) { 40 $i++; 41 if ($i<=3) { 42 continue; 43 } 44 45 //不要文件名带数字的副本 46 if(preg_match('/\d/is', $file)){ 47 continue; 48 } 49 50 //截取类名和控制器名字 TestController.class.php 51 $files=substr($file,0,strlen($file)-10); //TestController 52 $classname=strtolower(substr($file,0,strlen($file)-20)); //Test 53 54 //只取Admin\Controller下的控制器 55 if(in_array($files,$controllerarray)){ 56 continue; 57 } 58 59 $class="\\Admin\\Controller\\".$files; 60 61 $parentclass=get_parent_class($class);//获取父类 62 63 //只取继承了权限判断基类的类 64 if($parentclass!=='Common\Controller\AdminbaseController'){ 65 continue; 66 } 67 68 echo $files."<br>~~~~~~~~~~"."<br>"; 69 70 $b=get_class_methods($class);//获取类中所有public的方法 71 72 foreach ($b as $vaule) { 73 if(in_array($vaule,$actionarray)){ 74 continue; 75 } 76 $j++; 77 $action = "admin/$classname/" . $vaule; 78 echo $action . "=>" . $rule[$action] . "<br>"; 79 if($rule[$action]){ 80 $k++; 81 }else{ 82 $p++; 83 } 84 } 85 86 echo "-------------------------------------<br>"; 87 88 } 89 echo "<br>*********************************<br>"; 90 echo "共有".$j."个方法需要添加权限,已添加".$k."个,还有".$p."个方法需要添加权限\n"; 91 $mydir->close(); 92 } 93 /** 94 * 低于PHP 5.5版本的array_column()函数 95 **/ 96 function array_column($input, $columnKey, $indexKey = NULL) 97 { 98 $columnKeyIsNumber = (is_numeric($columnKey)) ? TRUE : FALSE; 99 $indexKeyIsNull = (is_null($indexKey)) ? TRUE : FALSE; 100 $indexKeyIsNumber = (is_numeric($indexKey)) ? TRUE : FALSE; 101 $result = array(); 102 103 foreach ((array)$input AS $key => $row) 104 { 105 if ($columnKeyIsNumber) 106 { 107 $tmp = array_slice($row, $columnKey, 1); 108 $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : NULL; 109 } 110 else 111 { 112 $tmp = isset($row[$columnKey]) ? $row[$columnKey] : NULL; 113 } 114 if ( ! $indexKeyIsNull) 115 { 116 if ($indexKeyIsNumber) 117 { 118 $key = array_slice($row, $indexKey, 1); 119 $key = (is_array($key) && ! empty($key)) ? current($key) : NULL; 120 $key = is_null($key) ? 0 : $key; 121 } 122 else 123 { 124 $key = isset($row[$indexKey]) ? $row[$indexKey] : 0; 125 } 126 } 127 128 $result[$key] = $tmp; 129 } 130 131 return $result; 132 } 133 }
运行一下看看
当当当当,完美运行,让我拉到最下面看看还有多少权限没加
die
这这这这!!!
这谁写的权限管理系统啊。。。噢!谁来拯救我、、、、