NodeService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace service;
  14. use think\Db;
  15. /**
  16. * 系统权限节点读取器
  17. * Class NodeService
  18. * @package extend
  19. * @author Anyon <zoujingli@qq.com>
  20. * @date 2017/05/08 11:28
  21. */
  22. class NodeService
  23. {
  24. /**
  25. * 应用用户权限节点
  26. * @return bool
  27. */
  28. public static function applyAuthNode()
  29. {
  30. cache('need_access_node', null);
  31. if (($userid = session('user.id'))) {
  32. session('user', Db::name('SystemUser')->where('id', $userid)->find());
  33. }
  34. if (($authorize = session('user.authorize'))) {
  35. $where = ['id' => ['in', explode(',', $authorize)], 'status' => '1'];
  36. $authorizeids = Db::name('SystemAuth')->where($where)->column('id');
  37. if (empty($authorizeids)) {
  38. return session('user.nodes', []);
  39. }
  40. $nodes = Db::name('SystemAuthNode')->where('auth', 'in', $authorizeids)->column('node');
  41. return session('user.nodes', $nodes);
  42. }
  43. return false;
  44. }
  45. /**
  46. * 获取授权节点
  47. * @return array
  48. */
  49. public static function getAuthNode()
  50. {
  51. $nodes = cache('need_access_node');
  52. if (empty($nodes)) {
  53. $nodes = Db::name('SystemNode')->where('is_auth', '1')->column('node');
  54. cache('need_access_node', $nodes);
  55. }
  56. return $nodes;
  57. }
  58. /**
  59. * 检查用户节点权限
  60. * @param string $node 节点
  61. * @return bool
  62. */
  63. public static function checkAuthNode($node)
  64. {
  65. list($module, $controller, $action) = explode('/', str_replace(['?', '=', '&'], '/', $node . '///'));
  66. $auth_node = strtolower(trim("{$module}/{$controller}/{$action}", '/'));
  67. if (session('user.username') === 'admin' || stripos($node, 'admin/index') === 0) {
  68. return true;
  69. }
  70. if (!in_array($auth_node, self::getAuthNode())) {
  71. return true;
  72. }
  73. return in_array($auth_node, (array)session('user.nodes'));
  74. }
  75. /**
  76. * 获取系统代码节点
  77. * @param array $nodes
  78. * @return array
  79. */
  80. public static function get($nodes = [])
  81. {
  82. $alias = Db::name('SystemNode')->column('node,is_menu,is_auth,is_login,title');
  83. $ignore = ['index', 'wechat/api', 'wechat/notify', 'wechat/review', 'admin/plugs', 'admin/login', 'admin/index'];
  84. foreach (self::getNodeTree(APP_PATH) as $thr) {
  85. foreach ($ignore as $str) {
  86. if (stripos($thr, $str) === 0) {
  87. continue 2;
  88. }
  89. }
  90. $tmp = explode('/', $thr);
  91. list($one, $two) = ["{$tmp[0]}", "{$tmp[0]}/{$tmp[1]}"];
  92. $nodes[$one] = array_merge(isset($alias[$one]) ? $alias[$one] : ['node' => $one, 'title' => '', 'is_menu' => 0, 'is_auth' => 0, 'is_login' => 0], ['pnode' => '']);
  93. $nodes[$two] = array_merge(isset($alias[$two]) ? $alias[$two] : ['node' => $two, 'title' => '', 'is_menu' => 0, 'is_auth' => 0, 'is_login' => 0], ['pnode' => $one]);
  94. $nodes[$thr] = array_merge(isset($alias[$thr]) ? $alias[$thr] : ['node' => $thr, 'title' => '', 'is_menu' => 0, 'is_auth' => 0, 'is_login' => 0], ['pnode' => $two]);
  95. }
  96. foreach ($nodes as &$node) {
  97. list($node['is_auth'], $node['is_menu'], $node['is_login']) = [
  98. intval($node['is_auth']), intval($node['is_menu']),
  99. empty($node['is_auth']) ? intval($node['is_login']) : 1
  100. ];
  101. }
  102. return $nodes;
  103. }
  104. /**
  105. * 获取节点列表
  106. * @param string $path 路径
  107. * @param array $nodes 额外数据
  108. * @return array
  109. */
  110. public static function getNodeTree($path, $nodes = [])
  111. {
  112. foreach (self::_getFilePaths($path) as $vo) {
  113. if (!preg_match('|/(\w+)/controller/(\w+)|', str_replace(DS, '/', $vo), $matches) || count($matches) !== 3) {
  114. continue;
  115. }
  116. $className = config('app_namespace') . str_replace('/', '\\', $matches[0]);
  117. if (!class_exists($className)) {
  118. continue;
  119. }
  120. foreach (get_class_methods($className) as $actionName) {
  121. if ($actionName[0] !== '_') {
  122. $nodes[] = strtolower("{$matches[1]}/{$matches[2]}/{$actionName}");
  123. }
  124. }
  125. }
  126. return $nodes;
  127. }
  128. /**
  129. * 获取所有PHP文件
  130. * @param string $path 目录
  131. * @param array $data 额外数据
  132. * @param string $ext 文件后缀
  133. * @return array
  134. */
  135. private static function _getFilePaths($path, $data = [], $ext = 'php')
  136. {
  137. foreach (scandir($path) as $dir) {
  138. if ($dir[0] === '.') {
  139. continue;
  140. }
  141. if (($tmp = realpath($path . DS . $dir)) && (is_dir($tmp) || pathinfo($tmp, 4) === $ext)) {
  142. is_dir($tmp) ? $data = array_merge($data, self::_getFilePaths($tmp)) : $data[] = $tmp;
  143. }
  144. }
  145. return $data;
  146. }
  147. }