permission.ctrl.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. $dos = array('display', 'post', 'delete');
  8. $do = !empty($_GPC['do']) ? $_GPC['do'] : 'display';
  9. $module_name = trim($_GPC['m']);
  10. $modulelist = uni_modules();
  11. $module = $_W['current_module'] = $modulelist[$module_name];
  12. if(empty($module)) {
  13. itoast('抱歉,你操作的模块不能被访问!');
  14. }
  15. if(!permission_check_account_user_module($module_name.'_permissions', $module_name)) {
  16. itoast('您没有权限进行该操作');
  17. }
  18. if ($do == 'display') {
  19. $user_permissions = module_clerk_info($module_name); $current_module_permission = module_permission_fetch($module_name);
  20. $permission_name = array();
  21. if (!empty($current_module_permission)) {
  22. foreach ($current_module_permission as $key => $permission) {
  23. $permission_name[$permission['permission']] = $permission['title'];
  24. }
  25. }
  26. if (!empty($user_permissions)) {
  27. foreach ($user_permissions as $key => &$permission) {
  28. if (!empty($permission['permission'])) {
  29. $permission['permission'] = explode('|', $permission['permission']);
  30. foreach ($permission['permission'] as $k => $val) {
  31. $permission['permission'][$val] = $permission_name[$val];
  32. unset($permission['permission'][$k]);
  33. }
  34. }
  35. }
  36. unset($permission);
  37. }
  38. if($_W['ispost'] && $_W['isajax']) {
  39. iajax(0, $user_permissions, '');
  40. }
  41. }
  42. if ($do == 'post') {
  43. $uid = intval($_GPC['uid']);
  44. $user = user_single($uid);
  45. $module_and_plugins = array();
  46. $all_permission = array();
  47. if (!empty($module['plugin_list'])) {
  48. $module_and_plugins = array_reverse($module['plugin_list']);
  49. }
  50. array_push($module_and_plugins, $module_name);
  51. $module_and_plugins = array_reverse($module_and_plugins);
  52. foreach ($module_and_plugins as $key => $module_val) {
  53. $all_permission[$module_val]['info'] = module_fetch($module_val);
  54. $all_permission[$module_val]['permission'] = module_permission_fetch($module_val);
  55. }
  56. if (!empty($uid)) {
  57. foreach ($module_and_plugins as $key => $plugin) {
  58. $have_permission[$plugin] = permission_account_user_menu($uid, $_W['uniacid'], $plugin);
  59. foreach ($all_permission[$plugin]['permission'] as $key => $value) {
  60. $all_permission[$plugin]['permission'][$key]['checked'] = 0;
  61. if (in_array($value['permission'], $have_permission[$plugin]) || in_array('all', $have_permission[$plugin])) {
  62. $all_permission[$plugin]['permission'][$key]['checked'] = 1;
  63. }
  64. if (!empty($value['sub_permission'])) {
  65. foreach ($value['sub_permission'] as $sub_permission_key => $sub_permission_val) {
  66. if (in_array($sub_permission_val['permission'], $have_permission[$plugin])) {
  67. $all_permission[$plugin]['permission'][$key]['sub_permission'][$sub_permission_key]['checked'] = 1;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. if (is_error($have_permission)) {
  74. itoast($have_permission['message']);
  75. }
  76. }
  77. if (checksubmit()) {
  78. if (empty($uid)) {
  79. $founders = explode(',', $_W['config']['setting']['founder']);
  80. $username = trim($_GPC['username']);
  81. $user = user_single(array('username' => $username));
  82. if (!empty($user)) {
  83. if ($user['status'] != 2) {
  84. itoast('用户未通过审核或不存在', url('module/permission', array('m' => $module_name)), 'error');
  85. }
  86. if (in_array($user['uid'], $founders)) {
  87. itoast('不可操作网站创始人!', url('module/permission', array('m' => $module_name)), 'error');
  88. }
  89. } else {
  90. itoast('用户不存在', url('module/permission', array('m' => $module_name)), 'error');
  91. }
  92. $data = array('uniacid' => $_W['uniacid'], 'uid' => $user['uid'], 'type' => $module_name);
  93. $exists = pdo_get('users_permission', $data);
  94. if (is_array($exists) && !empty($exists)) {
  95. itoast('操作员已经存在!', url('module/permission', array('m' => $module_name)), 'error');
  96. }
  97. $uid = $user['uid'];
  98. }
  99. $permission = $_GPC['module_permission'];
  100. if (!empty($permission) && is_array($permission)) {
  101. $permission = safe_gpc_array($permission);
  102. foreach ($module_and_plugins as $name) {
  103. if (empty($permission[$name])) {
  104. $module_permission = '';
  105. } else {
  106. $module_permission = implode('|', array_unique($permission[$name]));
  107. }
  108. if (empty($module_permission) && !empty($have_permission[$name])) {
  109. pdo_delete('users_permission', array('uniacid' => $_W['uniacid'], 'uid' => $uid, 'type' => $name));
  110. continue;
  111. }
  112. if (empty($module_permission)) {
  113. continue;
  114. }
  115. if (empty($have_permission[$name])) {
  116. pdo_insert('users_permission', array('uniacid' => $_W['uniacid'], 'uid' => $uid, 'type' => $name, 'permission' => $module_permission));
  117. } else {
  118. pdo_update('users_permission', array('permission' => $module_permission), array('uniacid' => $_W['uniacid'], 'uid' => $uid, 'type' => $name));
  119. }
  120. }
  121. } else {
  122. if (empty($all_permission[$module_name]['permission'])) {
  123. $data = array('uniacid' => $_W['uniacid'], 'uid' => $user['uid'], 'type' => $module_name);
  124. $exists = pdo_get('users_permission', $data);
  125. if (is_array($exists) && !empty($exists)) {
  126. itoast('操作员已经存在!', url('module/permission', array('m' => $module_name)), 'error');
  127. }
  128. $data['permission'] = 'all';
  129. pdo_insert('users_permission', $data);
  130. } else {
  131. foreach ($module_and_plugins as $name) {
  132. if (!empty($have_permission[$name]) && empty($all_permission[$module_name]['permission'])) {
  133. pdo_delete('users_permission', array('uniacid' => $_W['uniacid'], 'uid' => $uid, 'type' => $name));
  134. }
  135. }
  136. }
  137. }
  138. $role = table('uni_account_users')->getUserRoleByUniacid($uid, $_W['uniacid']);
  139. if (empty($role)) {
  140. pdo_insert('uni_account_users', array('uniacid' => $_W['uniacid'], 'uid' => $uid, 'role' => 'clerk'));
  141. } else {
  142. pdo_update('uni_account_users', array('role' => 'clerk'), array('uniacid' => $_W['uniacid'], 'uid' => $uid));
  143. }
  144. itoast('操作成功', url('module/permission', array('m' => $module_name)), 'success');
  145. }
  146. }
  147. if ($do == 'delete') {
  148. $operator_id = intval($_GPC['uid']);
  149. if (empty($operator_id)) {
  150. itoast('参数错误', referer(), 'error');
  151. } else {
  152. $user = pdo_get('users', array('uid' => $operator_id), array('uid'));
  153. if (!empty($user)) {
  154. $delete_account_users = pdo_delete('uni_account_users', array('uid' => $operator_id, 'role' => 'clerk', 'uniacid' => $_W['uniacid']));
  155. $module_info = module_fetch($module_name);
  156. $module_plugin_list = $module_info['plugin_list'];
  157. if (!empty($module_plugin_list)) {
  158. pdo_delete('users_permission', array('uid' => $_GPC['uid'], 'uniacid' => $_W['uniacid'], 'type in' => $module_plugin_list));
  159. }
  160. $delete_user_permission = pdo_delete('users_permission', array('uid' => $operator_id, 'type' => $module_name, 'uniacid' => $_W['uniacid']));
  161. pdo_delete('users_lastuse', array('uid' => $operator_id, 'uniacid' => $_W['uniacid'], 'modulename' => $module_name));
  162. }
  163. itoast('删除成功', referer(), 'success');
  164. }
  165. }
  166. template('module/permission');