Auth.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php namespace Manage\Lib;
  2. use Dever;
  3. class Auth
  4. {
  5. protected $login = true;
  6. public $uid;
  7. protected $user;
  8. protected $system;
  9. protected $info;
  10. protected $func;
  11. public $data = array();
  12. public function __construct()
  13. {
  14. $info = Dever::load('common', 'manage')->auth();
  15. if (!$info && $this->login) {
  16. $info['uid'] = 1;
  17. $info['extend']['system_id'] = 'no';
  18. $info['extend']['system_id'] = 1;
  19. $info['extend']['info_id'] = 1;
  20. $info['extend']['module_id'] = 1;
  21. $info['extend']['data_id'] = 1;
  22. #Dever::error('请先登录');
  23. }
  24. $this->system = Dever::db('system', 'manage')->find($info['extend']['system_id']);
  25. if (!$this->system) {
  26. Dever::error('当前系统不存在');
  27. }
  28. $this->system_info = Dever::db($this->system['info_table'])->find($info['extend']['info_id']);
  29. if (!$this->system) {
  30. Dever::error('当前系统设置错误');
  31. }
  32. $this->uid = $info['uid'];
  33. $this->user = Dever::db($this->system['user_table'])->find($this->uid);
  34. if (!$this->user) {
  35. Dever::error('请先登录');
  36. }
  37. $this->user['table'] = $this->system['user_table'];
  38. $this->user['auth'] = array('module' => '', 'menu' => '', 'func' => '');
  39. if ($this->user['role']) {
  40. $role = Dever::db($this->system['role_table'])->select(array('id' => array('in', $this->user['role'])));
  41. foreach ($role as $k => $v) {
  42. $this->user['auth']['module'] .= $v['module'] . ',';
  43. $this->user['auth']['menu'] .= $v['menu'] . ',';
  44. $this->user['auth']['func'] .= $v['auth'] . ',';
  45. }
  46. }
  47. if ($this->user['auth']['module']) {
  48. $this->user['auth']['module'] = rtrim($this->user['auth']['module'], ',');
  49. }
  50. if ($this->user['auth']['menu']) {
  51. $this->user['auth']['menu'] = rtrim($this->user['auth']['menu'], ',');
  52. }
  53. if ($this->user['auth']['func']) {
  54. $this->user['auth']['func'] = ',' . $this->user['auth']['func'];
  55. }
  56. $this->user['select'] = $info['extend'] ?? false;
  57. if (!$this->user['select']) {
  58. # 分别为系统id,系统基本信息id,模块id,模块数据id
  59. $this->user['select'] = array('partition' => 'no', 'system_id' => 1, 'info_id' => 1, 'module_id' => 1, 'data_id' => 1);
  60. }
  61. $this->checkModule($this->user['select']['module_id']);
  62. }
  63. # 设置功能权限
  64. public function getFunc($key, $name, $sort = 1, $param = '')
  65. {
  66. if (!$key) {
  67. $key = md5(base64_encode($name));
  68. }
  69. /*
  70. if ($param) {
  71. if (is_array($param)) {
  72. $param = Dever::json_encode($name);
  73. }
  74. $key = $key . '_' . md5($param);
  75. }*/
  76. if (!$this->menu) {
  77. return false;
  78. }
  79. $data['menu_id'] = $this->menu['id'];
  80. $data['key'] = $key;
  81. $key = $key . $data['menu_id'];
  82. if (isset($this->func[$key]['id'])) {
  83. return $this->func[$key]['id'];
  84. }
  85. $this->func[$key] = Dever::db('menu_func', 'manage')->find($data);
  86. $name = $this->menu['name'] . '-' . $name;
  87. if (!$this->func[$key]) {
  88. $data['name'] = $name;
  89. $data['sort'] = $sort;
  90. $id = Dever::db('menu_func', 'manage')->insert($data);
  91. Dever::db('menu', 'manage')->update($this->menu['id'], array('func' => 1));
  92. } else {
  93. /*
  94. if ($info['name'] != $name) {
  95. $data['name'] = $name;
  96. $data['sort'] = $sort;
  97. Dever::db('menu_func', 'manage')->update($info['id'], $data);
  98. Dever::db('menu', 'manage')->update($this->menu['id'], array('func' => 1));
  99. }*/
  100. $id = $this->func[$key]['id'];
  101. }
  102. if ($this->user['id'] == 1) {
  103. return $id;
  104. }
  105. if ($this->user['auth']['func'] && strstr($this->user['auth']['func'], ',' . $id . ',')) {
  106. return $id;
  107. }
  108. return false;
  109. }
  110. # 检测系统模块权限
  111. protected function checkModule($module_id)
  112. {
  113. if ($this->user['id'] == 1) {
  114. return;
  115. }
  116. if ($this->user['auth']['module'] && !Dever::check($this->user['auth']['module'], $module_id)) {
  117. Dever::error('无系统权限');
  118. }
  119. }
  120. # 检测菜单权限
  121. protected function checkMenu($menu, $result = true)
  122. {
  123. if ($this->user['id'] == 1) {
  124. if ($result) {
  125. return false;
  126. }
  127. return;
  128. }
  129. if ($this->user['auth']['menu'] && !Dever::check($this->user['auth']['menu'], $menu)) {
  130. if ($result) {
  131. return true;
  132. }
  133. Dever::error('无菜单访问权限');
  134. }
  135. if ($result) {
  136. return false;
  137. }
  138. }
  139. # 检测功能权限
  140. protected function checkFunc()
  141. {
  142. $id = Dever::input('func');
  143. if (!$id) {
  144. return false;
  145. }
  146. if ($this->user['id'] == 1) {
  147. return $id;
  148. }
  149. if ($this->user['auth']['func'] && strstr($this->user['auth']['func'], ',' . $id . ',')) {
  150. return $id;
  151. }
  152. if (isset($this->menu) && $this->menu && $this->menu['show'] != 1) {
  153. return $id;
  154. }
  155. Dever::error('无操作权限');
  156. }
  157. }