AuthRoleBehavior.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace app\admin\behavior;
  3. /**
  4. * 管理员权限控制
  5. */
  6. load_trait('controller/Jump');
  7. class AuthRoleBehavior
  8. {
  9. use \traits\controller\Jump;
  10. protected static $actionName;
  11. protected static $controllerName;
  12. protected static $moduleName;
  13. protected static $method;
  14. protected static $admin_info;
  15. /**
  16. * 构造方法
  17. * @param Request $request Request对象
  18. * @access public
  19. */
  20. public function __construct()
  21. {
  22. !isset(self::$moduleName) && self::$moduleName = request()->module();
  23. !isset(self::$controllerName) && self::$controllerName = request()->controller();
  24. !isset(self::$actionName) && self::$actionName = request()->action();
  25. !isset(self::$method) && self::$method = request()->method();
  26. !isset(self::$admin_info) && self::$admin_info = session('admin_info');
  27. }
  28. /**
  29. * 模块初始化
  30. * @param array $params 传入参数
  31. * @access public
  32. */
  33. public function moduleInit(&$params)
  34. {
  35. }
  36. /**
  37. * 操作开始执行
  38. * @param array $params 传入参数
  39. * @access public
  40. */
  41. public function actionBegin(&$params)
  42. {
  43. if (0 < intval(self::$admin_info['role_id'])) {
  44. // 检测全局的增、改、删的权限——优先级最高
  45. $this->cud_access();
  46. // 检测每个小插件的权限
  47. $this->weapp_access();
  48. // 检测栏目管理的每个栏目权限
  49. $this->arctype_access();
  50. // 检测内容管理每个栏目对应的内容里列表等权限
  51. $this->archives_access();
  52. }
  53. }
  54. /**
  55. * 视图内容过滤
  56. * @param array $params 传入参数
  57. * @access public
  58. */
  59. public function viewFilter(&$params)
  60. {
  61. }
  62. /**
  63. * 应用结束
  64. * @param array $params 传入参数
  65. * @access public
  66. */
  67. public function appEnd(&$params)
  68. {
  69. }
  70. /**
  71. * 检测全局的增、改、删的权限
  72. * @access private
  73. */
  74. private function cud_access()
  75. {
  76. /*只有相应的控制器和操作名才执行,以便提高性能*/
  77. $ctl = strtolower(self::$controllerName);
  78. $act = strtolower(self::$actionName);
  79. $actArr = ['add','edit','del'];
  80. if ('weapp' == $ctl && 'execute' == $act) {
  81. $sa = input('param.sa/s');
  82. foreach ($actArr as $key => $cud) {
  83. $sa = preg_replace('/^(.*)_('.$cud.')$/i', '$2', $sa); // 同名add 或者以_add类似结尾都符合
  84. if ($sa == $cud) {
  85. $admin_info = self::$admin_info;
  86. $auth_role_info = !empty($admin_info['auth_role_info']) ? $admin_info['auth_role_info'] : [];
  87. $cudArr = !empty($auth_role_info['cud']) ? $auth_role_info['cud'] : [];
  88. if (!in_array($sa, $cudArr)) {
  89. $this->error('您没有操作权限,请联系超级管理员分配权限');
  90. }
  91. break;
  92. }
  93. }
  94. } else {
  95. $post = input('post.');
  96. array_push($actArr, 'changetableval'); // 审核信息
  97. foreach ($actArr as $key => $cud) {
  98. $act = preg_replace('/^(.*)_('.$cud.')$/i', '$2', $act); // 同名add 或者以_add类似结尾都符合
  99. if ($act == $cud) {
  100. $admin_info = self::$admin_info;
  101. $auth_role_info = !empty($admin_info['auth_role_info']) ? $admin_info['auth_role_info'] : [];
  102. $cudArr = !empty($auth_role_info['cud']) ? $auth_role_info['cud'] : [];
  103. if (!in_array($act, $cudArr)) {
  104. if ('changetableval' == $act) {
  105. // 审核信息
  106. if ('archives' == $post['table'] && 'arcrank' == $post['field']) {
  107. $this->error('您没有操作权限,请联系超级管理员分配权限', null, '', 2);
  108. }
  109. } else {
  110. $this->error('您没有操作权限,请联系超级管理员分配权限');
  111. }
  112. } else {
  113. if (!in_array('changetableval', $cudArr)) {
  114. // 审核信息
  115. if (IS_POST && 'edit' == $act) {
  116. $archivesInfo = M('archives')->field('arcrank,admin_id')->find($post['aid']);
  117. if (-1 == $archivesInfo['arcrank'] && $archivesInfo['arcrank'] != $post['arcrank']) {
  118. $this->error('您没有操作权限,请联系超级管理员分配权限', url('Archives/edit', ['id'=>$post['aid']]), '', 3);
  119. }
  120. }
  121. }
  122. }
  123. break;
  124. }
  125. }
  126. }
  127. /*--end*/
  128. }
  129. /**
  130. * 检测每个小插件的权限
  131. * @access private
  132. */
  133. private function weapp_access()
  134. {
  135. /*只有相应的控制器和操作名才执行,以便提高性能*/
  136. $ctl = strtolower(self::$controllerName);
  137. $act = strtolower(self::$actionName);
  138. if ('weapp' == $ctl && 'execute' == $act) {
  139. $sc = input('param.sc/s');
  140. $sm = input('param.sm/s');
  141. $sa = input('param.sa/s');
  142. $admin_info = self::$admin_info;
  143. $auth_role_info = !empty($admin_info['auth_role_info']) ? $admin_info['auth_role_info'] : [];
  144. $plugins = !empty($auth_role_info['permission']['plugins']) ? $auth_role_info['permission']['plugins'] : [];
  145. // 插件本身设置的权限列表
  146. $config = include WEAPP_PATH.$sc.DS.'config.php';
  147. $plugins_permission = !empty($config['permission']) ? array_keys($config['permission']) : [];
  148. // 管理员拥有的插件具体权限
  149. $admin_permission = !empty($plugins[$sc]['child']) ? $plugins[$sc]['child'] : [];
  150. // 没有赋给管理员的插件具体权限
  151. $diff_plugins_perm = array_diff($plugins_permission, $admin_permission);
  152. // 检测插件权限
  153. $bool = true;
  154. if (empty($plugins_permission) && !isset($plugins[$sc])) {
  155. $bool = false;
  156. } else if (!empty($plugins_permission)) {
  157. foreach ($diff_plugins_perm as $key => $val) {
  158. if (strtolower($sm.'@'.$sa) == strtolower($val)) {
  159. $bool = false;
  160. break;
  161. }
  162. }
  163. }
  164. if (!$bool) {
  165. $this->error('您没有操作权限,请联系超级管理员分配权限');
  166. }
  167. }
  168. /*--end*/
  169. }
  170. /**
  171. * 检测栏目管理的每个栏目权限
  172. * @access private
  173. */
  174. private function arctype_access()
  175. {
  176. /*只有相应的控制器和操作名才执行,以便提高性能*/
  177. $ctl_all = strtolower(self::$controllerName.'@*');
  178. $ctlArr = ['arctype@*'];
  179. if (in_array($ctl_all, $ctlArr)) {
  180. $typeids = [];
  181. if (in_array(strtolower(self::$actionName), ['edit','del'])) {
  182. $typeids[] = input('id/d', 0);
  183. } else if (in_array(strtolower(self::$actionName), ['add'])) {
  184. $typeids[] = input('parent_id/d', 0);
  185. }
  186. if (!$this->is_check_arctype($typeids)) {
  187. $this->error('您没有操作权限,请联系超级管理员分配权限');
  188. }
  189. }
  190. /*--end*/
  191. }
  192. /**
  193. * 检测内容管理每个栏目对应的内容里列表等权限
  194. * @access private
  195. */
  196. private function archives_access()
  197. {
  198. /*只有相应的控制器和操作名才执行,以便提高性能*/
  199. $ctl = strtolower(self::$controllerName);
  200. $act = strtolower(self::$actionName);
  201. $ctl_act = $ctl.'@'.$act;
  202. $ctl_all = $ctl.'@*';
  203. $ctlArr= ['arctype@single','archives@*'];
  204. $row = \think\Db::name('channeltype')
  205. ->where('nid','NOTIN', ['single'])
  206. ->column('ctl_name');
  207. foreach ($row as $key => $val) {
  208. array_push($ctlArr, strtolower($val).'@*');
  209. }
  210. if (in_array($ctl_act, $ctlArr) || in_array($ctl_all, $ctlArr)) {
  211. $typeids = [];
  212. if (in_array($act, ['add','edit','del'])) {
  213. $aids = [];
  214. switch ($act) {
  215. case 'edit':
  216. $aids = input('id/a', []);
  217. break;
  218. case 'del':
  219. $aids = input('del_id/a', []);
  220. break;
  221. default:
  222. # code...
  223. break;
  224. }
  225. if (!empty($aids)) {
  226. $typeids = M('archives')->where('aid','IN',$aids)->column('typeid');
  227. }
  228. } else {
  229. $typeids[] = input('typeid/d', 0);
  230. }
  231. if (!$this->is_check_arctype($typeids)) {
  232. $this->error('您没有操作权限,请联系超级管理员分配权限');
  233. }
  234. }
  235. /*--end*/
  236. }
  237. /**
  238. * 检测栏目是否有权限
  239. */
  240. private function is_check_arctype($typeids = []) {
  241. $bool_flag = true;
  242. $admin_info = self::$admin_info;
  243. if (0 < intval($admin_info['role_id'])) {
  244. $auth_role_info = $admin_info['auth_role_info'];
  245. $permission = $auth_role_info['permission'];
  246. foreach ($typeids as $key => $tid) {
  247. if (0 < intval($tid) && !in_array($tid, $permission['arctype'])) {
  248. $bool_flag = false;
  249. break;
  250. }
  251. }
  252. }
  253. return $bool_flag;
  254. }
  255. }