Auth.php 5.4 KB

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