Auth.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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['auth'] = array('module' => '', 'menu' => '', 'func' => '');
  37. if ($this->user['role']) {
  38. $role = Dever::db($this->system['role_table'])->select(array('id' => array('in', $this->user['role'])));
  39. foreach ($role as $k => $v) {
  40. $this->user['auth']['module'] .= $v['module'] . ',';
  41. $this->user['auth']['menu'] .= $v['menu'] . ',';
  42. $this->user['auth']['func'] .= $v['auth'] . ',';
  43. }
  44. }
  45. if ($this->user['auth']['module']) {
  46. $this->user['auth']['module'] = rtrim($this->user['auth']['module'], ',');
  47. }
  48. if ($this->user['auth']['menu']) {
  49. $this->user['auth']['menu'] = rtrim($this->user['auth']['menu'], ',');
  50. }
  51. if ($this->user['auth']['func']) {
  52. $this->user['auth']['func'] = ',' . $this->user['auth']['func'];
  53. }
  54. $this->user['select'] = $info['extend'] ?? false;
  55. if (!$this->user['select']) {
  56. # 分别为系统id,系统基本信息id,模块id,模块数据id
  57. $this->user['select'] = array('partition' => 'no', 'system_id' => 1, 'info_id' => 1, 'module_id' => 1, 'data_id' => 1);
  58. }
  59. $this->checkModule($this->user['select']['module_id']);
  60. }
  61. # 设置功能权限
  62. public function getFunc($key, $name, $sort = 1, $param = '')
  63. {
  64. if (!$key) {
  65. $key = md5(base64_encode($name));
  66. }
  67. /*
  68. if ($param) {
  69. if (is_array($param)) {
  70. $param = Dever::json_encode($name);
  71. }
  72. $key = $key . '_' . md5($param);
  73. }*/
  74. if (!$this->menu) {
  75. return false;
  76. }
  77. $data['menu_id'] = $this->menu['id'];
  78. $data['key'] = $key;
  79. $info = Dever::db('menu_func', 'manage')->find($data);
  80. $name = $this->menu['name'] . '-' . $name;
  81. if (!$info) {
  82. $data['name'] = $name;
  83. $data['sort'] = $sort;
  84. $id = Dever::db('menu_func', 'manage')->insert($data);
  85. Dever::db('menu', 'manage')->update($this->menu['id'], array('func' => 1));
  86. } else {
  87. if ($info['name'] != $name) {
  88. $data['name'] = $name;
  89. $data['sort'] = $sort;
  90. Dever::db('menu_func', 'manage')->update($info['id'], $data);
  91. Dever::db('menu', 'manage')->update($this->menu['id'], array('func' => 1));
  92. }
  93. $id = $info['id'];
  94. }
  95. if ($this->user['id'] == 1) {
  96. return $id;
  97. }
  98. if ($this->user['auth']['func'] && strstr($this->user['auth']['func'], ',' . $id . ',')) {
  99. return $id;
  100. }
  101. return false;
  102. }
  103. # 检测系统模块权限
  104. protected function checkModule($module_id)
  105. {
  106. if ($this->user['id'] == 1) {
  107. return;
  108. }
  109. if ($this->user['auth']['module'] && !Dever::check($this->user['auth']['module'], $module_id)) {
  110. Dever::error('无系统权限');
  111. }
  112. }
  113. # 检测菜单权限
  114. protected function checkMenu($menu, $result = true)
  115. {
  116. if ($this->user['id'] == 1) {
  117. if ($result) {
  118. return false;
  119. }
  120. return;
  121. }
  122. if ($this->user['auth']['menu'] && !Dever::check($this->user['auth']['menu'], $menu)) {
  123. if ($result) {
  124. return true;
  125. }
  126. Dever::error('无菜单访问权限');
  127. }
  128. if ($result) {
  129. return false;
  130. }
  131. }
  132. # 检测功能权限
  133. protected function checkFunc()
  134. {
  135. $id = Dever::input('func');
  136. if (!$id) {
  137. return false;
  138. }
  139. if ($this->user['id'] == 1) {
  140. return $id;
  141. }
  142. if ($this->user['auth']['func'] && strstr($this->user['auth']['func'], ',' . $id . ',')) {
  143. return $id;
  144. }
  145. if (isset($this->menu) && $this->menu && $this->menu['show'] != 1) {
  146. return $id;
  147. }
  148. Dever::error('无操作权限');
  149. }
  150. }