HonrayAuth.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Author: linchuangbin
  4. // +----------------------------------------------------------------------
  5. namespace com;
  6. use think\Db;
  7. class HonrayAuth{
  8. //默认配置
  9. public $_config = array(
  10. 'AUTH_ON' => true, // 认证开关
  11. 'AUTH_TYPE' => 1, // 认证方式,1为实时认证;2为登录认证。
  12. 'AUTH_GROUP' => 'admin_group', // 用户组数据表名
  13. 'AUTH_GROUP_ACCESS' => 'admin_access', // 用户-用户组关系表
  14. 'AUTH_RULE' => 'admin_rule', // 权限规则表
  15. 'AUTH_USER' => 'admin_user' // 用户信息表
  16. );
  17. private $auth_key;
  18. public function __construct($auth_key) {
  19. $this->auth_key = $auth_key;
  20. $this->_config['AUTH_GROUP'] = $this->_config['AUTH_GROUP'];
  21. $this->_config['AUTH_RULE'] = $this->_config['AUTH_RULE'];
  22. $this->_config['AUTH_USER'] = $this->_config['AUTH_USER'];
  23. $this->_config['AUTH_GROUP_ACCESS'] = $this->_config['AUTH_GROUP_ACCESS'];
  24. if (config('AUTH_CONFIG')) {
  25. //可设置配置项 AUTH_CONFIG, 此配置项为数组。
  26. $this->_config = array_merge($this->_config, config('AUTH_CONFIG'));
  27. }
  28. }
  29. /**
  30. * 检查权限
  31. * @param name string|array 需要验证的规则列表,支持逗号分隔的权限规则或索引数组
  32. * @param uid int 认证用户的id
  33. * @return boolean 通过验证返回true;失败返回false
  34. */
  35. public function check($name, $uid, $relation = 'or') {
  36. if (!$this->_config['AUTH_ON'])
  37. return true;
  38. $authList = $this->getAuthList($uid); //获取用户需要验证的所有有效规则列表
  39. if (is_string($name)) {
  40. $name = strtolower($name);
  41. if (strpos($name, ',') !== false) {
  42. $name = explode(',', $name);
  43. } else {
  44. $name = array($name);
  45. }
  46. }
  47. if (is_array($name)) {
  48. foreach ($name as $k => $v) {
  49. $name[$k] = strtolower($v);
  50. }
  51. }
  52. $list = array(); //保存验证通过的规则名
  53. foreach ( $authList as $auth ) {
  54. if (in_array($auth , $name)){
  55. $list[] = $auth ;
  56. }
  57. }
  58. if ($relation == 'or' and !empty($list)) {
  59. return true;
  60. }
  61. $diff = array_diff($name, $list);
  62. if ($relation == 'and' and empty($diff)) {
  63. return true;
  64. }
  65. return false;
  66. }
  67. /**
  68. * 根据用户id获取用户组,返回值为数组
  69. * @param uid int 用户id
  70. * @return array 用户所属的用户组 array(
  71. * array('uid'=>'用户id','group_id'=>'用户组id','title'=>'用户组名称','rules'=>'用户组拥有的规则id,多个,号隔开'),
  72. * ...)
  73. */
  74. public function getGroups($uid) {
  75. static $groups = array();
  76. if (isset($groups[$uid])) return $groups[$uid];
  77. $userModel = new \app\admin\model\User();
  78. $user_groups = collection($userModel->get($uid)->groups)->toArray();
  79. $groups[$uid] = $user_groups ? : array();
  80. return $groups[$uid];
  81. }
  82. /**
  83. * 获得权限列表
  84. * @param integer $uid 用户id
  85. */
  86. protected function getAuthList($uid) {
  87. $temp = cache('Auth_'.$this->auth_key);
  88. $authList = $temp['_AUTH_LIST_'];
  89. if( $this->_config['AUTH_TYPE'] == 2 && isset($authList)){
  90. return $authList;
  91. }
  92. //读取用户所属用户组
  93. $groups = $this->getGroups($uid);
  94. $ids = [];//保存用户所属用户组设置的所有权限规则id
  95. foreach ($groups as $g) {
  96. $ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
  97. }
  98. $ids = array_unique($ids);
  99. if (empty($ids)) {
  100. return [];
  101. }
  102. $map = [
  103. 'id' => array('in', $ids),
  104. 'status' => 1,
  105. ];
  106. //读取用户组所有权限规则
  107. $rules = Db::name($this->_config['AUTH_RULE'])->where($map)->select();
  108. foreach ($rules as $k => $v) {
  109. $rules[$k]['name'] = strtolower($v['name']);
  110. }
  111. $tree = new \com\Tree();
  112. $authList = $tree->list_to_tree($rules, 'id', 'pid', 'child', 0, true, array('pid'));
  113. $authList = rulesDeal($authList);
  114. //登录有效时间
  115. $cacheConfig = config('cache');
  116. $loginExpire = $cacheConfig['expire'] ? : '3600';
  117. if ($this->_config['AUTH_TYPE'] == 2) {
  118. //规则列表结果保存到缓存
  119. $cache_info = cache('Auth_'.$this->auth_key);
  120. $cache_info['_AUTH_LIST_'] = $authList;
  121. cache('Auth_'.$this->auth_key, $cache_info, $loginExpire);
  122. }
  123. return $authList;
  124. }
  125. /**
  126. * 更新缓存中auth_list
  127. * @param string $type rule的类型
  128. * @return array 权限菜单
  129. */
  130. public function updateCacheAuth(){
  131. $cache = cache('Auth_'.$this->auth_key);
  132. $uid = $cache['userInfo']['u_id'];
  133. $groups = $this->getGroups($uid);
  134. $ids = array();
  135. foreach ($groups as $g) {
  136. $ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
  137. }
  138. $ids = array_unique($ids);
  139. if (empty($ids)) {
  140. return [];
  141. }
  142. $map = [
  143. 'id' => array('in',$ids),
  144. 'status' => 1,
  145. ];
  146. //读取用户组所有权限规则
  147. $rules = Db::name($this->_config['AUTH_RULE'])->where($map)->select();
  148. foreach ($rules as $k => $v) {
  149. $rules[$k]['name'] = strtolower($v['name']);
  150. }
  151. $tree = new \com\Tree();
  152. $authList = $tree->list_to_tree($rules, 'id', 'pid', 'child', 0, true, array('pid'));
  153. $authList = rulesDeal($authList);
  154. //规则列表结果保存到缓存
  155. $cache['_AUTH_LIST_'] = $authList;
  156. cache('Auth_'.$this->auth_key, $cache, config('LOGIN_SESSION_VALID'));
  157. return $cache['_AUTH_LIST_'];
  158. }
  159. }