You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
7.9 KiB

  1. <?php
  2. namespace backend\logic;
  3. use ReflectionClass;
  4. use ReflectionException;
  5. use ReflectionMethod;
  6. use yii;
  7. class PermissionManager
  8. {
  9. /**
  10. * 获取app的已做标记控制器的权限
  11. * DESCRIBE 控制器描述,只有控制器的注释添加了该描述,才会自动匹配该控制器;使用例子:DESCRIBE {controllerDescribe} DESCRIBE
  12. * ACTION 方法描述,只有控制器下action方法的注释添加了该描述,才会自动匹配该action方法;使用例子:ACTION {actionDescribe} ACTION
  13. * 匹配控制器中actions的方法,需要添加id参数并且在标明含义,例子:
  14. * '{actionName}' => [
  15. * 'class' => 'xxx\xxx\xxx',
  16. * 'id' => '{actionDescribe}'
  17. * ]
  18. * @return array 返回已匹配到的权限。返回例子:[["{controllerDescribe}"=>["{actionDescribe}"=>"action route"]]]
  19. * @throws yii\base\InvalidConfigException
  20. * @throws ReflectionException
  21. */
  22. public static function getAppPermission()
  23. {
  24. $permission = [];
  25. $permission = self::getControllersAndActions($permission); //获取该app的所有controller权限
  26. $permission = self::getModuleControllerAndAction($permission); //获取该app引用module的所有controller权限
  27. return $permission;
  28. }
  29. /**
  30. * 获取app下controller已标记的action方法权限
  31. * 这个方法先是获取该app下controller的路径,控制器的命名空间,然后匹配该路径下面的所有控制器文件
  32. * 截取控制器文件的控制器基础名称,转换为相应url规则名称,拼接为相应类名
  33. * 根据控制器id,通过[[Module::createControllerByID]]方法实例化该控制器,最后通过[[PermissionManager::constructingPermissionArray]]获取所有已标记的权限
  34. * @param array $permission 权限数组
  35. * @return array 权限数组
  36. * @throws yii\base\InvalidConfigException
  37. * @throws ReflectionException
  38. */
  39. private static function getControllersAndActions($permission = [])
  40. {
  41. $dir = Yii::$app->getControllerPath();
  42. $nameSpace = Yii::$app->controllerNamespace . "\\";
  43. $fileList = glob($dir."/*Controller.php");
  44. foreach ($fileList as $file) {
  45. $baseName = substr(basename($file), 0, -14);
  46. //根据路由规则转换控制器名称
  47. $name = strtolower(preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $baseName));
  48. $id = ltrim(str_replace(' ', '-', $name), '-');
  49. $className = $nameSpace . $baseName . 'Controller';
  50. $controller = Yii::$app->createControllerByID($id);
  51. $permission = self::constructingPermissionArray($controller, $className, $permission);
  52. }
  53. return $permission;
  54. }
  55. /**
  56. * 获取该app下关联module的已标记controller的权限
  57. * 这个方法通过[[Module::getModules]]获取该app下关联的module并循环
  58. * 去除gii和debug,通过[[Module::getModule]]获取子类module,获取命名空间,文件位置,然后匹配该路径下面的所有控制器文件
  59. * 截取控制器文件的控制器基础名称,转换为相应url规则名称,拼接为相应类名
  60. * 通过[[BaseYii::createObject]]实例化,最后通过[[PermissionManager::constructingPermissionArray]]获取所有已标记的权限
  61. * @param array $permission 权限数组
  62. * @return array 权限数组
  63. * @throws yii\base\InvalidConfigException
  64. * @throws ReflectionException
  65. */
  66. private static function getModuleControllerAndAction($permission = [])
  67. {
  68. foreach (Yii::$app->getModules() as $id => $child) {
  69. if(in_array($id, ['gii', 'debug'])) {
  70. continue;
  71. }
  72. $module = Yii::$app->getModule($id);
  73. $nameSpace = $module->controllerNamespace."\\";
  74. $dir = $module->controllerPath;
  75. $fileList = glob($dir."/*Controller.php");
  76. foreach ($fileList as $file) {
  77. $baseName = substr(basename($file), 0, -14);
  78. $name = strtolower(preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $baseName));
  79. $id = $module->id.'/'.ltrim(str_replace(' ', '-', $name), '-');
  80. $className = $nameSpace . $baseName . 'Controller';
  81. $controller = Yii::createObject($className, [$id, $module]);
  82. $permission = self::constructingPermissionArray($controller, $className, $permission);
  83. }
  84. }
  85. return $permission;
  86. }
  87. /**
  88. * 构建权限数组
  89. * 根据类名,使用ReflectionClass方法获取该类的信息
  90. * 通过该类的注释,判断是否存在DESCRIBE标记,以此判断是否要记录该类的权限
  91. * 若存在标记,则通过[[PermissionManager::getActionsInController]]获取actions里面的方法权限数组,通过[[PermissionManager::getActionInController]]获取该类下面的action方法权限数组
  92. * @param $controllerObject
  93. * @param $className
  94. * @param array $permission 权限数组
  95. * @return array 权限数组
  96. * @throws ReflectionException
  97. */
  98. private static function constructingPermissionArray($controllerObject, $className, $permission = [])
  99. {
  100. $prefix = '/'.$controllerObject->id.'/';
  101. $reflection = new ReflectionClass($className); //通过ReflectionClass方法获取该类的所有信息,包括参数方法等
  102. $controllerComment = $reflection->getDocComment();
  103. $controllerPregRes = preg_match("/(?<=DESCRIBE ).*?(?= DESCRIBE)/", $controllerComment, $controllerDescribe);
  104. if ($controllerPregRes) {
  105. $permission = self::getActionsInController($controllerObject, $controllerDescribe, $prefix, $permission);
  106. $permission = self::getActionInController($className, $prefix, $controllerDescribe, $permission);
  107. }
  108. return $permission;
  109. }
  110. /**
  111. * 获取控制器类中actions的方法权限
  112. * 根据实例化的控制器类,获取actions的方法并且循环,方法存在id这个参数,则记录
  113. * 最后放回权限数组
  114. * @param $controllerObject
  115. * @param $controllerDescribe
  116. * @param $prefix
  117. * @param array $permission 权限数组
  118. * @return array 权限数组
  119. */
  120. private static function getActionsInController($controllerObject, $controllerDescribe, $prefix, $permission = [])
  121. {
  122. foreach ($controllerObject->actions() as $id => $item) {
  123. if (isset($item['id'])) {
  124. $permission[$controllerDescribe[0]][$item['id']] = $prefix . $id;
  125. }
  126. }
  127. return $permission;
  128. }
  129. /**
  130. * 获取控制器类中的action方法权限
  131. * 通过ReflectionClass方法获取所有的action方法,若注释中存在ACTION标记,则记录
  132. * @param $className
  133. * @param $prefix
  134. * @param $controllerDescribe
  135. * @param array $permission 权限数组
  136. * @return array 权限数组
  137. * @throws ReflectionException
  138. */
  139. private static function getActionInController($className, $prefix, $controllerDescribe, $permission = [])
  140. {
  141. $reflection = new ReflectionClass($className); //通过ReflectionClass方法获取该类的所有信息,包括参数方法等
  142. foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
  143. //action的注释
  144. $actionComment = $method->getDocComment();
  145. $actionPregRes = preg_match("/(?<=ACTION ).*?(?= ACTION)/", $actionComment, $actionDescribe);
  146. if ($actionPregRes) {
  147. $actionName = $method->getName();
  148. if ($actionName != 'actions' && strpos($actionName, 'action') === 0) {
  149. $name = strtolower(preg_replace('/(?<![A-Z])[A-Z]/', ' \0', substr($actionName, 6)));
  150. $id = $prefix . ltrim(str_replace(' ', '-', $name), '-');
  151. $permission[$controllerDescribe[0]][$actionDescribe[0]] = $id;
  152. }
  153. }
  154. }
  155. return $permission;
  156. }
  157. }