AuthRule.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Facades\DB;
  4. class AuthRule
  5. {
  6. // 无需验证的路径
  7. protected $except = [
  8. 'admin/login/index',
  9. 'admin/login/send_code',
  10. 'admin/login/out'
  11. ];
  12. //默认配置
  13. protected $_config = [
  14. 'auth_on' => true, // 认证开关
  15. 'auth_type' => 1, // 认证方式,1为实时认证;2为登录认证。
  16. 'auth_group' => 'auth_group', // 用户组数据表名
  17. 'auth_group_access' => 'auth_group_access', // 用户-用户组关系表
  18. 'auth_rule' => 'auth_rule' // 权限规则表
  19. ];
  20. /**
  21. * $prefix表前缀
  22. */
  23. public function __construct()
  24. {
  25. // 判断配置
  26. if ( config('AUTH_CONFIG') ) {
  27. //可设置配置项 AUTH_CONFIG, 此配置项为数组。
  28. $this->_config = array_merge($this->_config, config('AUTH_CONFIG'));
  29. }
  30. }
  31. /**
  32. * 检查权限
  33. * @param name string|array 需要验证的规则列表
  34. * @param uid int 认证用户的id
  35. * @return boolean 通过验证返回true;失败返回false
  36. */
  37. public function check($path, $uid, $type = 1)
  38. {
  39. // 未开启验证,直接通过
  40. if ( !$this->_config['auth_on'] ) return true;
  41. // 获取用户需要验证的所有有效规则列表
  42. $authList = $this->getAuthList($uid, $type);
  43. dd($authList);
  44. // 切割path
  45. $path = explode('/', $path);
  46. // 没有控制器
  47. if( count($path) < 2 ) $path[] = 'index';
  48. // 没有方法
  49. if( count($path) < 3 ) $path[] = 'index';
  50. // 切割path
  51. $path = implode('/', $path);
  52. // 判断是否通过验证
  53. return in_array($path, $authList);
  54. }
  55. /**
  56. * 根据用户id获取用户组,返回值为数组
  57. * @param int $uid 用户id
  58. * @return array 用户所属的用户组
  59. */
  60. public function getGroups($uid)
  61. {
  62. // 用户组
  63. static $groups = [];
  64. // 存在小组,返回
  65. if ( isset($groups[$uid]) ) return $groups[$uid];
  66. // 从数据库查询
  67. $userrGroups = DB::table($this->_config['auth_group_access'])
  68. ->where([[$this->_config['auth_group_access'].'.user_uid','=',$uid],[$this->_config['auth_group'].'.status','=',1]])
  69. ->join( $this->_config['auth_group'], $this->_config['auth_group_access'].'.group_id','=',$this->_config['auth_group'].'.id')
  70. ->select(['user_uid as uid','group_id','title','rules'])->get()->toArray();
  71. // 获取对应用户的权限组
  72. $groups[$uid] = $userrGroups ?: [];
  73. // 返回用户的权限组
  74. return $groups[$uid];
  75. }
  76. /**
  77. * 获得权限列表
  78. * @param integer $uid 用户id
  79. * @param integer $type
  80. */
  81. public function getAuthList($uid, $type)
  82. {
  83. print_r($type);
  84. // 保存用户验证通过的权限列表
  85. static $_authList = [];
  86. // 验证类型
  87. $t = implode(',', (array) $type);
  88. // 已经存在权限列表,直接返回
  89. if (isset($_authList[$uid . $t])) return $_authList[$uid . $t];
  90. // 验证类型,如果是事实验证
  91. if ( $this->_config['auth_type'] == 2 ) {
  92. // 从session读取验证列表
  93. $session = session('_AUTH_LIST_'.$this->prefix.$uid.$t);
  94. // 存在则返回
  95. if( !empty($session) ) return $session;
  96. }
  97. // 读取用户所属用户组
  98. $groups = $this->getGroups($uid);
  99. // 保存用户所属用户组设置的所有权限规则id
  100. $ids = [];
  101. // 获取字段值
  102. $ids = array_column($groups,'rules');
  103. // 合并字符值
  104. $ids = array_unique(array_filter(explode(',',implode(',',$ids))));
  105. // 为空
  106. if( empty($ids) ) return [];
  107. // 读取用户组所有权限规则
  108. $rules = Db::table($this->_config['auth_rule'])->whereIn('menu_id',$ids)->pluck('name')->toArray();
  109. // 循环转大写
  110. foreach ( $rules as $rule ) {
  111. // 转小写,截除左边斜杠
  112. $rules[] = ltrim(strtolower($rule),'/');
  113. }
  114. // 去重
  115. $rules = array_unique($rules);
  116. // 用户验证列表
  117. $rules[$uid . $t] = $rules;
  118. dd($rules);
  119. // 如果是登录验证
  120. if ($this->_config['auth_type'] == 2 ) {
  121. //规则列表结果保存到session
  122. session('_AUTH_LIST_'.$this->prefix.$uid.$t,$rules);
  123. }
  124. // 返回结果
  125. return $rules;
  126. }
  127. }