Role.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\user\model;
  10. use think\Model;
  11. use util\Tree;
  12. use app\admin\model\Menu as MenuModel;
  13. /**
  14. * 角色模型
  15. * @package app\admin\model
  16. */
  17. class Role extends Model
  18. {
  19. // 设置当前模型对应的完整数据表名称
  20. protected $name = 'admin_role';
  21. // 自动写入时间戳
  22. protected $autoWriteTimestamp = true;
  23. // 写入时,将菜单id转成json格式
  24. public function setMenuAuthAttr($value)
  25. {
  26. return json_encode($value);
  27. }
  28. // 读取时,将菜单id转为数组
  29. public function getMenuAuthAttr($value)
  30. {
  31. return json_decode($value, true);
  32. }
  33. /**
  34. * 获取树形角色
  35. * @param null $id 需要隐藏的角色id
  36. * @param string $default 默认第一个菜单项,默认为“顶级角色”,如果为false则不显示,也可传入其他名称
  37. * @param null $filter 角色id,过滤显示指定角色及其子角色
  38. * @author 蔡伟明 <314013107@qq.com>
  39. * @return mixed
  40. */
  41. public static function getTree($id = null, $default = '', $filter = null)
  42. {
  43. $result[0] = '顶级角色';
  44. $where = [
  45. ['status', '=', 1]
  46. ];
  47. // 排除指定菜单及其子菜单
  48. $hide_ids = [];
  49. if ($id !== null) {
  50. $hide_ids = array_merge([$id], self::getChildsId($id));
  51. }
  52. // 过滤显示指定角色及其子角色
  53. if ($filter !== null) {
  54. $show_ids = self::getChildsId($filter);
  55. if (!empty($hide_ids)) {
  56. $ids = array_diff($show_ids, $hide_ids);
  57. $where[] = ['id', 'in', $ids];
  58. } else {
  59. $where[] = ['id', 'in', $show_ids];
  60. }
  61. } else {
  62. if (!empty($hide_ids)) {
  63. $where[] = ['id', 'not in', $hide_ids];
  64. }
  65. }
  66. // 获取菜单
  67. $roles = self::where($where)->column('id,pid,name');
  68. $pid = self::where($where)->order('pid')->value('pid');
  69. $roles = Tree::config(['title' => 'name'])->toList($roles, $pid);
  70. foreach ($roles as $role) {
  71. $result[$role['id']] = $role['title_display'];
  72. }
  73. // 设置默认菜单项标题
  74. if ($default != '') {
  75. $result[0] = $default;
  76. }
  77. // 隐藏默认菜单项
  78. if ($default === false) {
  79. unset($result[0]);
  80. }
  81. return $result;
  82. }
  83. /**
  84. * 获取所有子角色id
  85. * @param string $pid 父级id
  86. * @author 蔡伟明 <314013107@qq.com>
  87. * @return array
  88. */
  89. public static function getChildsId($pid = '')
  90. {
  91. $ids = self::where('pid', $pid)->column('id');
  92. foreach ($ids as $value) {
  93. $ids = array_merge($ids, self::getChildsId($value));
  94. }
  95. return $ids;
  96. }
  97. /**
  98. * 检查访问权限
  99. * @param int $id 需要检查的节点ID,默认检查当前操作节点
  100. * @param bool $url 是否为节点url,默认为节点id
  101. * @author 蔡伟明 <314013107@qq.com>
  102. * @return bool
  103. * @throws \think\Exception
  104. */
  105. public static function checkAuth($id = 0, $url = false)
  106. {
  107. // 当前用户的角色
  108. $role = session('user_auth.role');
  109. // id为1的是超级管理员,或者角色为1的,拥有最高权限
  110. if (session('user_auth.uid') == '1' || $role == '1') {
  111. return true;
  112. }
  113. // 获取当前用户的权限
  114. $menu_auth = session('role_menu_auth');
  115. // 检查权限
  116. if ($menu_auth) {
  117. if ($id !== 0) {
  118. return $url === false ? isset($menu_auth[$id]) : in_array($id, $menu_auth);
  119. }
  120. // 获取当前操作的id
  121. $location = MenuModel::getLocation();
  122. $action = end($location);
  123. return $url === false ? isset($menu_auth[$action['id']]) : in_array($action['url_value'], $menu_auth);
  124. }
  125. // 其他情况一律没有权限
  126. return false;
  127. }
  128. /**
  129. * 读取当前角色权限
  130. * @author 蔡伟明 <314013107@qq.com>
  131. * @return mixed
  132. */
  133. public function roleAuth()
  134. {
  135. $menu_auth = cache('role_menu_auth_'.session('user_auth.role'));
  136. if (!$menu_auth) {
  137. $menu_auth = self::where('id', session('user_auth.role'))->value('menu_auth');
  138. $menu_auth = json_decode($menu_auth, true);
  139. $menu_auth = MenuModel::where('id', 'in', $menu_auth)->column('id,url_value');
  140. }
  141. // 非开发模式,缓存数据
  142. if (config('develop_mode') == 0) {
  143. cache('role_menu_auth_'.session('user_auth.role'), $menu_auth);
  144. }
  145. return $menu_auth;
  146. }
  147. /**
  148. * 根据节点id获取所有角色id和权限
  149. * @param string $menu_id 节点id
  150. * @param bool $menu_auth 是否返回所有节点权限
  151. * @author 蔡伟明 <314013107@qq.com>
  152. * @return array
  153. */
  154. public static function getRoleWithMenu($menu_id = '', $menu_auth = false)
  155. {
  156. if ($menu_auth) {
  157. return self::where('menu_auth', 'like', '%"'.$menu_id.'"%')->column('id,menu_auth');
  158. } else {
  159. return self::where('menu_auth', 'like', '%"'.$menu_id.'"%')->column('id');
  160. }
  161. }
  162. /**
  163. * 根据角色id获取权限
  164. * @param array $role 角色id
  165. * @author 蔡伟明 <314013107@qq.com>
  166. * @return array
  167. */
  168. public static function getAuthWithRole($role = [])
  169. {
  170. return self::where('id', 'in', $role)->column('id,menu_auth');
  171. }
  172. /**
  173. * 重设权限
  174. * @param null $pid 父级id
  175. * @param array $new_auth 新权限
  176. * @author 蔡伟明 <314013107@qq.com>
  177. */
  178. public static function resetAuth($pid = null, $new_auth = [])
  179. {
  180. if ($pid !== null) {
  181. $data = self::where('pid', $pid)->column('id,menu_auth');
  182. foreach ($data as $id => $menu_auth) {
  183. $menu_auth = json_decode($menu_auth, true);
  184. $menu_auth = json_encode(array_intersect($menu_auth, $new_auth));
  185. self::where('id', $id)->setField('menu_auth', $menu_auth);
  186. self::resetAuth($id, $new_auth);
  187. }
  188. }
  189. }
  190. }