post-user.ctrl.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. load()->model('module');
  8. load()->model('system');
  9. $dos = array('delete', 'edit', 'set_permission', 'set_manager', 'module');
  10. $do = in_array($do, $dos) ? $do : 'edit';
  11. $uniacid = intval($_GPC['uniacid']);
  12. $acid = intval($_GPC['acid']);
  13. $_W['page']['title'] = '管理设置 - 微信' . ACCOUNT_TYPE_NAME . '管理';
  14. if (empty($uniacid) || empty($acid)) {
  15. itoast('请选择要编辑的公众号', referer(), 'error');
  16. }
  17. $state = permission_account_user_role($_W['uid'], $uniacid);
  18. $role_permission = in_array($state, array(ACCOUNT_MANAGE_NAME_FOUNDER, ACCOUNT_MANAGE_NAME_OWNER, ACCOUNT_MANAGE_NAME_MANAGER, ACCOUNT_MANAGE_NAME_VICE_FOUNDER));
  19. if (!$role_permission) {
  20. itoast('无权限操作!', referer(), 'error');
  21. }
  22. $founders = explode(',', $_W['config']['setting']['founder']);
  23. $headimgsrc = tomedia('headimg_'.$acid.'.jpg');
  24. $account = account_fetch($acid);
  25. if (is_error($account)) {
  26. itoast($account['message']);
  27. }
  28. if ($do == 'edit') {
  29. $permissions = pdo_fetchall("SELECT id, uid, role FROM ".tablename('uni_account_users')." WHERE uniacid = '$uniacid' and role != :role ORDER BY uid ASC, role DESC", array(':role' => 'clerk'), 'uid');
  30. $owner = pdo_get('uni_account_users', array('uniacid' => $uniacid, 'role' => 'owner'), array('uid', 'id'));
  31. if (!empty($permissions)) {
  32. $member = pdo_fetchall("SELECT username, uid FROM ".tablename('users')." WHERE uid IN (".implode(',', array_keys($permissions)).")", array(), 'uid');
  33. if (!empty($member)) {
  34. foreach ($permissions as $key => $per_val) {
  35. $permissions[$key]['isfounder'] = in_array($member[$key]['uid'], $founders) ? 1 : 0;
  36. $permissions[$key]['username'] = $member[$key]['username'] ? $member[$key]['username'] : '';
  37. }
  38. }
  39. }
  40. $uids = array();
  41. foreach ($permissions as $v) {
  42. $uids[] = $v['uid'];
  43. }
  44. template('account/manage-users');
  45. } elseif ($do == 'delete') {
  46. if (!$_W['isajax'] || !$_W['ispost']) {
  47. itoast('非法操作!', referer(), 'error');
  48. }
  49. $uid = is_array($_GPC['uid']) ? 0 : intval($_GPC['uid']);
  50. if (empty($uid)) {
  51. itoast('请选择要删除的用户!', referer(), 'error');
  52. }
  53. $data = array(
  54. 'uniacid' => $uniacid,
  55. 'uid' => $uid,
  56. );
  57. $exists = pdo_get('uni_account_users', array('uniacid' => $uniacid, 'uid' => $uid));
  58. if (!empty($exists)) {
  59. if ($state == ACCOUNT_MANAGE_NAME_MANAGER && ($exists['role'] == ACCOUNT_MANAGE_NAME_OWNER || $exists['role'] == ACCOUNT_MANAGE_NAME_MANAGER)) {
  60. itoast('管理员不可操作其他管理员', referer(), 'error');
  61. }
  62. $result = pdo_delete('uni_account_users', $data);
  63. if ($result) {
  64. itoast('删除成功!', referer(), 'success');
  65. } else {
  66. itoast('删除失败,请重试!', referer(), 'error');
  67. }
  68. } else {
  69. itoast('该公众号下不存在该用户!', referer(), 'error');
  70. }
  71. } elseif ($do == 'set_manager') {
  72. $username = trim($_GPC['username']);
  73. $user = user_single(array('username' => $username));
  74. if (!empty($user)) {
  75. if ($user['status'] != 2) {
  76. iajax(3, '用户未通过审核或不存在!', '');
  77. }
  78. if (in_array($user['uid'], $founders)) {
  79. iajax(1, '不可操作网站创始人!', '');
  80. }
  81. $addtype = intval($_GPC['addtype']);
  82. if (is_error($permission = permission_create_account($user['uid'], ACCOUNT_TYPE)) && $addtype == ACCOUNT_MANAGE_TYPE_OWNER) {
  83. itoast(error(5, $permission['message']), '', 'error');
  84. }
  85. $data = array(
  86. 'uniacid' => $uniacid,
  87. 'uid' => $user['uid'],
  88. );
  89. $exists = pdo_get('uni_account_users', $data);
  90. $owner = pdo_get('uni_account_users', array('uniacid' => $uniacid, 'role' => 'owner'));
  91. if (empty($exists)) {
  92. if ($addtype == ACCOUNT_MANAGE_TYPE_VICE_FOUNDER) {
  93. if ($user['founder_groupid'] != ACCOUNT_MANAGE_GROUP_VICE_FOUNDER) {
  94. iajax(6, '副创始人不存在!', '');
  95. }
  96. pdo_delete('uni_account_users', array('uniacid' => $uniacid, 'role' => ACCOUNT_MANAGE_NAME_VICE_FOUNDER));
  97. $data['role'] = ACCOUNT_MANAGE_NAME_VICE_FOUNDER;
  98. }
  99. if ($addtype == ACCOUNT_MANAGE_TYPE_OWNER) {
  100. if ($state == ACCOUNT_MANAGE_NAME_MANAGER) {
  101. iajax(4, '管理员不可操作主管理员', '');
  102. }
  103. if (empty($owner)) {
  104. $data['role'] = ACCOUNT_MANAGE_NAME_OWNER;
  105. } else {
  106. $result = pdo_update('uni_account_users', $data, array('id' => $owner['id']));
  107. if ($result) {
  108. pdo_delete('users_permission', array('uniacid' => $uniacid, 'uid' => $user['uid']));
  109. cache_clean(cache_system_key("user_accounts"));
  110. iajax(0, '修改成功!', '');
  111. } else {
  112. iajax(1, '修改失败!', '');
  113. }
  114. exit;
  115. }
  116. } else if ($addtype == ACCOUNT_MANAGE_TYPE_MANAGER) {
  117. if ($state == ACCOUNT_MANAGE_NAME_MANAGER) {
  118. iajax(4, '管理员不可操作管理员', '');
  119. }
  120. $data['role'] = ACCOUNT_MANAGE_NAME_MANAGER;
  121. } else if ($addtype == ACCOUNT_MANAGE_TYPE_OPERATOR) {
  122. $data['role'] = ACCOUNT_MANAGE_NAME_OPERATOR;
  123. }
  124. pdo_delete('uni_account_users', array('uniacid' => $uniacid,'uid' => $user['uid']));
  125. $result = pdo_insert('uni_account_users', $data);
  126. if ($result) {
  127. if ($addtype == ACCOUNT_MANAGE_TYPE_OWNER) {
  128. pdo_delete('users_permission', array('uniacid' => $uniacid, 'uid' => $user['uid']));
  129. }
  130. cache_clean(cache_system_key("user_accounts"));
  131. iajax(0, '添加成功!', '');
  132. } else {
  133. iajax(1, '添加失败!', '');
  134. }
  135. } else {
  136. iajax(2, $username.'已经是该公众号的店员或操作员或管理员,请勿重复添加!', '');
  137. }
  138. } else {
  139. iajax(-1, '参数错误,请刷新重试!', '');
  140. }
  141. } elseif ($do == 'set_permission') {
  142. $uid = intval($_GPC['uid']);
  143. $user = user_single(array('uid' => $uid));
  144. if (empty($user)) {
  145. itoast('您操作的用户不存在或是已经被删除!', '', '');
  146. }
  147. $role = permission_account_user_role($_W['uid'], $uniacid);
  148. if (empty($role)) {
  149. itoast('此用户没有操作该统一公众号的权限,请选指派“管理员”或是“操作员”权限!', '', '');
  150. }
  151. $module_permission = permission_account_user_menu($uid, $uniacid, 'modules');
  152. if (is_error($module_permission)) {
  153. itoast('参数错误!');
  154. }
  155. $module_permission_keys = array_keys($module_permission);
  156. $module = uni_modules_by_uniacid($uniacid);
  157. if (!empty($module)) {
  158. foreach ($module as $key => $value) {
  159. if (in_array($account['type'], array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH)) && $value['app_support'] != MODULE_SUPPORT_ACCOUNT) {
  160. unset($module[$key]);
  161. }
  162. if ($account['type'] == ACCOUNT_TYPE_APP_NORMAL && $value['wxapp_support'] != MODULE_SUPPORT_WXAPP) {
  163. unset($module[$key]);
  164. }
  165. }
  166. }
  167. if ($account['type'] == ACCOUNT_TYPE_OFFCIAL_NORMAL || $account['type'] == ACCOUNT_TYPE_OFFCIAL_AUTH) {
  168. $user_menu_permission_account = permission_account_user_menu($uid, $uniacid, PERMISSION_ACCOUNT);
  169. if (is_error($user_menu_permission_account)) {
  170. itoast('参数错误!');
  171. }
  172. } elseif ($account['type'] == ACCOUNT_TYPE_APP_NORMAL) {
  173. $user_menu_permission_wxapp = permission_account_user_menu($uid, $uniacid, PERMISSION_WXAPP);
  174. if (is_error($user_menu_permission_wxapp)) {
  175. itoast('参数错误!');
  176. }
  177. }
  178. $menus = system_menu_permission_list($role);
  179. if (checksubmit('submit')) {
  180. $all_menu_permission = permission_menu_name();
  181. $user_menu_permission_new = array();
  182. if ($account['type'] == ACCOUNT_TYPE_OFFCIAL_NORMAL || $account['type'] == ACCOUNT_TYPE_OFFCIAL_AUTH) {
  183. if (!empty($_GPC['system'])) {
  184. foreach ($_GPC['system'] as $permission_name) {
  185. if (in_array($permission_name, $all_menu_permission)) {
  186. $user_menu_permission_new[] = $permission_name;
  187. }
  188. }
  189. $data = array(
  190. 'type' => PERMISSION_ACCOUNT,
  191. 'permission' => implode('|', $user_menu_permission_new)
  192. );
  193. $result = permission_update_account_user($uid, $uniacid, $data);
  194. if (is_error($result)) {
  195. itoast($result['message']);
  196. }
  197. } else {
  198. pdo_delete('users_permission', array('uniacid' => $uniacid, 'uid' => $uid, 'type' => PERMISSION_ACCOUNT));
  199. }
  200. } elseif ($account['type'] == ACCOUNT_TYPE_APP_NORMAL) {
  201. if (!empty($_GPC['wxapp'])) {
  202. foreach ($_GPC['wxapp'] as $permission_name) {
  203. if (in_array($permission_name, $all_menu_permission)) {
  204. $user_menu_permission_new[] = $permission_name;
  205. }
  206. }
  207. $data = array(
  208. 'type' => PERMISSION_WXAPP,
  209. 'permission' => implode('|', $user_menu_permission_new)
  210. );
  211. $result = permission_update_account_user($uid, $uniacid, $data);
  212. if (is_error($result)) {
  213. itoast($result['message']);
  214. }
  215. } else {
  216. pdo_delete('users_permission', array('uniacid' => $uniacid, 'uid' => $uid, 'type' => PERMISSION_WXAPP));
  217. }
  218. }
  219. pdo_query("DELETE FROM " . tablename('users_permission') . " WHERE uniacid = :uniacid AND uid = :uid AND type != '" . PERMISSION_ACCOUNT . "' AND type != '" . PERMISSION_WXAPP . "'", array(':uniacid' => $uniacid, ':uid' => $uid));
  220. if(!empty($_GPC['module'])) {
  221. foreach($_GPC['module'] as $module_val) {
  222. $insert = array(
  223. 'uniacid' => $uniacid,
  224. 'uid' => $uid,
  225. 'type' => $module_val,
  226. );
  227. if(empty($_GPC['module_'. $module_val]) || $_GPC[$module_val . '_select'] == 1) {
  228. $insert['permission'] = 'all';
  229. pdo_insert('users_permission', $insert);
  230. continue;
  231. } else {
  232. $data = array();
  233. foreach($_GPC['module_'. $module_val] as $v) {
  234. $data[] = $v;
  235. }
  236. if(!empty($data)) {
  237. $insert['permission'] = implode('|', $data);
  238. pdo_insert('users_permission', $insert);
  239. }
  240. }
  241. }
  242. }
  243. $cachekey = cache_system_key("permission:{$uniacid}:{$uid}");
  244. cache_delete($cachekey);
  245. itoast('操作菜单权限成功!', referer(), 'success');
  246. }
  247. template('account/set-permission');
  248. } elseif($do == 'module' && $_W['isajax']) {
  249. $uid = intval($_GPC['uid']);
  250. $user = user_single($uid);
  251. if(empty($user)) {
  252. iajax(1, '访问错误, 未找到指定操作用户.', '');
  253. }
  254. $founders = explode(',', $_W['config']['setting']['founder']);
  255. $isfounder = in_array($user['uid'], $founders);
  256. if($isfounder) {
  257. iajax(2, '访问错误, 无法编辑站长.', '');
  258. }
  259. $module_name = trim($_GPC['m']);
  260. $uniacid = intval($_GPC['uniacid']);
  261. $uid = intval($_GPC['uid']);
  262. $module = pdo_fetch('SELECT * FROM ' . tablename('modules') . ' WHERE name = :m', array(':m' => $module_name));
  263. $purview = pdo_fetch('SELECT * FROM ' . tablename('users_permission') . ' WHERE uniacid = :aid AND uid = :uid AND type = :type', array(':aid' => $uniacid, ':uid' => $uid, ':type' => $module_name));
  264. if(!empty($purview['permission'])) {
  265. $purview['permission'] = explode('|', $purview['permission']);
  266. } else {
  267. $purview['permission'] = array();
  268. }
  269. $mineurl = array();
  270. $all = 0;
  271. if(!empty($mods)) {
  272. foreach($mods as $mod) {
  273. if($mod['url'] == 'all') {
  274. $all = 1;
  275. break;
  276. } else {
  277. $mineurl[] = $mod['url'];
  278. }
  279. }
  280. }
  281. $data = array();
  282. if($module['settings']) {
  283. $data[] = array('title' => '参数设置', 'permission' => $module_name.'_settings');
  284. }
  285. if($module['isrulefields']) {
  286. $data[] = array('title' => '回复规则列表', 'permission' => $module_name.'_rule');
  287. }
  288. $entries = module_entries($module_name);
  289. if(!empty($entries['home'])) {
  290. $data[] = array('title' => '微站首页导航', 'permission' => $module_name.'_home');
  291. }
  292. if(!empty($entries['profile'])) {
  293. $data[] = array('title' => '个人中心导航', 'permission' => $module_name.'_profile');
  294. }
  295. if(!empty($entries['shortcut'])) {
  296. $data[] = array('title' => '快捷菜单', 'permission' => $module_name.'_shortcut');
  297. }
  298. if(!empty($entries['cover'])) {
  299. foreach($entries['cover'] as $cover) {
  300. $data[] = array('title' => $cover['title'], 'permission' => $module_name.'_cover_'.$cover['do']);
  301. }
  302. }
  303. if(!empty($entries['menu'])) {
  304. foreach($entries['menu'] as $menu) {
  305. $data[] = array('title' => $menu['title'], 'permission' => $module_name.'_menu_'.$menu['do']);
  306. }
  307. }
  308. unset($entries);
  309. if(!empty($module['permissions'])) {
  310. $module['permissions'] = (array)iunserializer($module['permissions']);
  311. $data = array_merge($data, $module['permissions']);
  312. }
  313. foreach($data as &$data_val) {
  314. $data_val['checked'] = 0;
  315. if(in_array($data_val['permission'], $purview['permission']) || in_array('all', $purview['permission'])) {
  316. $data_val['checked'] = 1;
  317. }
  318. }
  319. unset($data_val);
  320. if (empty($data)) {
  321. iajax(3, '无子权限!', '');
  322. } else {
  323. iajax(0, $data, '');
  324. }
  325. }