post-user.ctrl.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. if (empty($uniacid) || empty($acid)) {
  14. itoast('请选择要编辑的平台账号', referer(), 'error');
  15. }
  16. $state = permission_account_user_role($_W['uid'], $uniacid);
  17. $role_permission = in_array($state, array(ACCOUNT_MANAGE_NAME_FOUNDER, ACCOUNT_MANAGE_NAME_OWNER, ACCOUNT_MANAGE_NAME_MANAGER, ACCOUNT_MANAGE_NAME_VICE_FOUNDER));
  18. if (!$role_permission) {
  19. itoast('无权限操作!', referer(), 'error');
  20. }
  21. $founders = explode(',', $_W['config']['setting']['founder']);
  22. $founder_info = pdo_getcolumn('users', array('uid' => current($founders)), 'username');
  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' ORDER BY uid ASC, role DESC", array(), 'uid');
  30. if (!empty($permissions)) {
  31. $member = pdo_fetchall("SELECT username, uid FROM ".tablename('users')." WHERE uid IN (".implode(',', array_keys($permissions)).")", array(), 'uid');
  32. if (!empty($member)) {
  33. $operator = $manager = $owner = $vice_founder = array();
  34. foreach ($permissions as $key => $per_val) {
  35. $per_val['isfounder'] = in_array($member[$key]['uid'], $founders) ? 1 : 0;
  36. $per_val['username'] = $member[$key]['username'] ? $member[$key]['username'] : '';
  37. switch($per_val['role']) {
  38. case 'vice_founder':
  39. $vice_founder = $per_val;
  40. break;
  41. case 'owner':
  42. $owner = $per_val;
  43. break;
  44. case 'manager':
  45. $manager[] = $per_val;
  46. break;
  47. case 'operator':
  48. $operator[] = $per_val;
  49. break;
  50. default:
  51. break;
  52. }
  53. }
  54. }
  55. }
  56. template('account/manage-users');
  57. }
  58. if ($do == 'delete') {
  59. if (!$_W['isajax'] || !$_W['ispost']) {
  60. itoast('非法操作!', referer(), 'error');
  61. }
  62. $uid = is_array($_GPC['uid']) ? 0 : intval($_GPC['uid']);
  63. if (empty($uid)) {
  64. itoast('请选择要删除的用户!', referer(), 'error');
  65. }
  66. $data = array(
  67. 'uniacid' => $uniacid,
  68. 'uid' => $uid,
  69. );
  70. $exists = pdo_get('uni_account_users', array('uniacid' => $uniacid, 'uid' => $uid));
  71. if (empty($exists)) {
  72. itoast('该平台账号下不存在该用户!', referer(), 'error');
  73. }
  74. if ($state == ACCOUNT_MANAGE_NAME_MANAGER && ($exists['role'] == ACCOUNT_MANAGE_NAME_OWNER || $exists['role'] == ACCOUNT_MANAGE_NAME_MANAGER)) {
  75. itoast('管理员不可操作其他管理员', referer(), 'error');
  76. }
  77. $result = pdo_delete('uni_account_users', $data);
  78. if ($result) {
  79. if ($exists['role'] == ACCOUNT_MANAGE_NAME_OPERATOR) {
  80. pdo_delete('users_permission', $data);
  81. }
  82. pdo_delete('users_lastuse', $data);
  83. pdo_delete('system_stat_visit', $data);
  84. itoast('删除成功!', referer(), 'success');
  85. } else {
  86. itoast('删除失败,请重试!', referer(), 'error');
  87. }
  88. }
  89. if ($do == 'set_manager') {
  90. $username = trim($_GPC['username']);
  91. $user = user_single(array('username' => $username));
  92. if (!empty($user)) {
  93. if ($user['status'] != 2) {
  94. iajax(3, '用户未通过审核或不存在!', '');
  95. }
  96. $user_end_time = user_end_time($user['uid']);
  97. $account_end_time = $user_end_time == 0 ? 0 : strtotime($user_end_time);
  98. pdo_update('account', array('endtime' => $account_end_time), array('uniacid' => $uniacid));
  99. $addtype = intval($_GPC['addtype']);
  100. if (!in_array($addtype, array(ACCOUNT_MANAGE_TYPE_OPERATOR, ACCOUNT_MANAGE_TYPE_MANAGER, ACCOUNT_MANAGE_TYPE_OWNER, ACCOUNT_MANAGE_TYPE_VICE_FOUNDER))) {
  101. iajax(-1, '添加使用者类型有误,只能添加操作员/管理员/主管理员/副创始人!');
  102. }
  103. if (in_array($user['uid'], $founders)) {
  104. if ($addtype == ACCOUNT_MANAGE_TYPE_OWNER) {
  105. pdo_delete('uni_account_users', array('uniacid' => $uniacid, 'role' => ACCOUNT_MANAGE_NAME_OWNER));
  106. iajax(0, '修改成功!', '');
  107. }
  108. iajax(1, '不可操作网站创始人!', '');
  109. }
  110. if ($addtype == ACCOUNT_MANAGE_TYPE_OWNER && !in_array($_W['uid'], $founders)) {
  111. $creat_limit = permission_user_account_num($user['uid']);
  112. if ($creat_limit[TYPE_SIGN . '_limit'] <= 0) {
  113. itoast(error(5, ACCOUNT_TYPE_NAME . '创建数量已达上限'), '', 'error');
  114. }
  115. }
  116. $data = array(
  117. 'uniacid' => $uniacid,
  118. 'uid' => $user['uid'],
  119. );
  120. $exists = pdo_get('uni_account_users', $data);
  121. if (empty($exists)) {
  122. if ($addtype == ACCOUNT_MANAGE_TYPE_VICE_FOUNDER) {
  123. if ($user['founder_groupid'] != ACCOUNT_MANAGE_GROUP_VICE_FOUNDER) {
  124. iajax(6, '副创始人不存在!', '');
  125. }
  126. pdo_delete('uni_account_users', array('uniacid' => $uniacid, 'role' => ACCOUNT_MANAGE_NAME_VICE_FOUNDER));
  127. $data['role'] = ACCOUNT_MANAGE_NAME_VICE_FOUNDER;
  128. }
  129. if ($addtype == ACCOUNT_MANAGE_TYPE_OWNER) {
  130. if ($state == ACCOUNT_MANAGE_NAME_MANAGER) {
  131. iajax(4, '管理员不可操作主管理员', '');
  132. }
  133. $owner = pdo_get('uni_account_users', array('uniacid' => $uniacid, 'role' => 'owner'));
  134. if (empty($owner)) {
  135. $data['role'] = ACCOUNT_MANAGE_NAME_OWNER;
  136. } else {
  137. $result = pdo_update('uni_account_users', $data, array('id' => $owner['id']));
  138. if ($result) {
  139. pdo_delete('users_permission', array('uniacid' => $uniacid, 'uid' => $user['uid']));
  140. cache_clean(cache_system_key("user_accounts"));
  141. cache_build_account_modules($uniacid);
  142. iajax(0, '修改成功!', '');
  143. } else {
  144. iajax(1, '修改失败!', '');
  145. }
  146. exit;
  147. }
  148. } else if ($addtype == ACCOUNT_MANAGE_TYPE_MANAGER) {
  149. if ($state == ACCOUNT_MANAGE_NAME_MANAGER) {
  150. iajax(4, '管理员不可操作管理员', '');
  151. }
  152. $data['role'] = ACCOUNT_MANAGE_NAME_MANAGER;
  153. } else if ($addtype == ACCOUNT_MANAGE_TYPE_OPERATOR) {
  154. $data['role'] = ACCOUNT_MANAGE_NAME_OPERATOR;
  155. }
  156. pdo_delete('uni_account_users', array('uniacid' => $uniacid,'uid' => $user['uid']));
  157. $result = pdo_insert('uni_account_users', $data);
  158. if ($result) {
  159. if ($addtype == ACCOUNT_MANAGE_TYPE_OWNER) {
  160. pdo_delete('users_permission', array('uniacid' => $uniacid, 'uid' => $user['uid']));
  161. }
  162. if (in_array($addtype, array(ACCOUNT_MANAGE_TYPE_MANAGER, ACCOUNT_MANAGE_TYPE_OPERATOR))) {
  163. permission_account_user_init($user['uid'], $uniacid);
  164. }
  165. cache_clean(cache_system_key("user_accounts"));
  166. cache_build_account_modules($uniacid);
  167. iajax(0, '添加成功!', '');
  168. } else {
  169. iajax(1, '添加失败!', '');
  170. }
  171. } else {
  172. $permissions = table('users_permission')->getAllUserPermission($user['uid'], $uniacid);
  173. $modulelist = uni_modules();
  174. if (!empty($permissions)) {
  175. $i = 0;
  176. foreach ($permissions as $permission) {
  177. if ($i >= 3) {
  178. break;
  179. }
  180. if (!in_array($permission['type'], array_keys($modulelist))) {
  181. continue;
  182. }
  183. $module_info = module_fetch($permission['type']);
  184. $modules_str .= $module_info['title'] . ',';
  185. $i ++;
  186. }
  187. }
  188. if (empty($permissions) && !empty($exists)) {
  189. if ($exists['role'] == ACCOUNT_MANAGE_NAME_OPERATOR) {
  190. $role_name = '操作员';
  191. } elseif ($exists['role'] == ACCOUNT_MANAGE_NAME_MANAGER) {
  192. $role_name = '管理员';
  193. } elseif ($exists['role'] == ACCOUNT_MANAGE_NAME_OWNER) {
  194. $role_name = '主管理员';
  195. }
  196. iajax(2, "添加平台使用者失败, <a style='color:#3296fa'>" . $username . "<a/>, 已经是公众号的<a style='color:#3296fa'>" .$role_name. "<a/> ,请勿重复添加!", '');
  197. } else {
  198. iajax(2, "添加平台使用者失败, <a style='color:#3296fa'>" . $username . "<a/>, 已经是 <a style='color:#3296fa'>" . rtrim($modules_str, ',') . "</a>的应用操作员,请先取消其应用内操作员身份后再添加平台使用者!", '');
  199. }
  200. }
  201. } else {
  202. iajax(-1, '用户不存在!', '');
  203. }
  204. }
  205. if ($do == 'set_permission') {
  206. $uid = intval($_GPC['uid']);
  207. $user = user_single(array('uid' => $uid));
  208. if (empty($user)) {
  209. itoast('您操作的用户不存在或是已经被删除!', '', '');
  210. }
  211. $role = permission_account_user_role($_W['uid'], $uniacid);
  212. if (empty($role)) {
  213. itoast('此用户没有操作该统一公众号的权限,请选指派“管理员”或是“操作员”权限!', '', '');
  214. }
  215. $module_permission = permission_account_user_menu($uid, $uniacid, 'modules');
  216. if (is_error($module_permission)) {
  217. itoast('参数错误!');
  218. }
  219. if (current($module_permission) == 'all') {
  220. $module_permission_keys = array('permission' => 'all');
  221. $module_permission_all = 1;
  222. } else {
  223. $module_permission_keys = array_keys($module_permission);
  224. $module_permission_all = 0;
  225. }
  226. $module = uni_modules_by_uniacid($uniacid);
  227. $uni_modules_permission_all = array();
  228. $uni_modules_permissions = array();
  229. if (!empty($module)) {
  230. foreach ($module as $key => $value) {
  231. if ($value['issystem'] == 1) {
  232. continue;
  233. }
  234. $permissions = module_permission_fetch($key);
  235. if (empty($permissions)) {
  236. $uni_modules_permission_all[] = $key;
  237. $uni_modules_permissions[$key] = array();
  238. } else {
  239. foreach ($permissions as $permission_info) {
  240. $uni_modules_permission_all[] = $permission_info['permission'];
  241. $uni_modules_permissions[$key][] = $permission_info['permission'];
  242. }
  243. }
  244. }
  245. }
  246. foreach ($account_all_type_sign as $account_type_sign => $account_type_info) {
  247. if (in_array($account['type'], $account_type_info['contain_type'])) {
  248. $account_type = $account_type_sign == 'account' ? 'system' : $account_type_sign;
  249. }
  250. }
  251. $user_menu_permissions = permission_account_user_menu($uid, $uniacid, $account_type);
  252. $menus = system_menu_permission_list($role);
  253. permission_account_user_init($user['uid'], $uniacid);
  254. $all_menu_permission = permission_menu_name();
  255. if (in_array($account['type_sign'], array(ACCOUNT_TYPE_SIGN, XZAPP_TYPE_SIGN, WEBAPP_TYPE_SIGN))) {
  256. $account_type_menu = ACCOUNT_TYPE_SIGN;
  257. } elseif (in_array($account['type_sign'], array(ALIAPP_TYPE_SIGN, BAIDUAPP_TYPE_SIGN, TOUTIAOAPP_TYPE_SIGN))){
  258. $account_type_menu = WXAPP_TYPE_SIGN;
  259. } else {
  260. $account_type_menu = $account['type_sign'];
  261. }
  262. if (checksubmit('submit')) {
  263. $user_menu_permission_new = array();
  264. if (!empty($_GPC['menus'])) {
  265. foreach ($_GPC['menus'] as $permission_name) {
  266. if (in_array($permission_name, $all_menu_permission)) {
  267. $user_menu_permission_new[] = $permission_name;
  268. }
  269. }
  270. $data = array(
  271. 'type' => $account_type,
  272. 'permission' => implode('|', $user_menu_permission_new),
  273. );
  274. $result = permission_update_account_user($uid, $uniacid, $data);
  275. if (is_error($result)) {
  276. itoast($result['message']);
  277. }
  278. } else {
  279. $data = array(
  280. 'type' => $account_type,
  281. 'permission' => '|',
  282. );
  283. $result = permission_update_account_user($uid, $uniacid, $data);
  284. }
  285. if (is_error($result)) {
  286. itoast($result['message']);
  287. }
  288. $permission_names = "'" . implode(array(PERMISSION_ACCOUNT, PERMISSION_WXAPP, PERMISSION_WEBAPP, PERMISSION_PHONEAPP, PERMISSION_XZAPP, PERMISSION_ALIAPP, PERMISSION_BAIDUAPP, PERMISSION_TOUTIAOAPP, PERMISSION_SYSTEM),"','") . "'";
  289. pdo_query("DELETE FROM " . tablename('users_permission') . " WHERE uniacid = :uniacid AND uid = :uid AND type not in (" .$permission_names . ")", array(':uniacid' => $uniacid, ':uid' => $uid));
  290. if (!empty($_GPC['module'])) {
  291. foreach($_GPC['module'] as $module_val) {
  292. if (empty($_GPC['module_' . $module_val]) || $_GPC[$module_val . '_select'] == 1) {
  293. if (empty($uni_modules_permissions[$module_val])) {
  294. $selected_modules_permission[] = $module_val;
  295. } else {
  296. foreach ($uni_modules_permissions[$module_val] as $sub_modules_permission) {
  297. $selected_modules_permission[] = $sub_modules_permission;
  298. }
  299. }
  300. }
  301. }
  302. $selected_all_module_permission = array_diff($uni_modules_permission_all, $selected_modules_permission);
  303. if (!empty($selected_all_module_permission)) {
  304. foreach($_GPC['module'] as $module_val) {
  305. $insert = array(
  306. 'uniacid' => $uniacid,
  307. 'uid' => $uid,
  308. 'type' => $module_val,
  309. );
  310. if (empty($_GPC['module_' . $module_val]) || $_GPC[$module_val . '_select'] == 1) {
  311. $insert['permission'] = 'all';
  312. pdo_insert('users_permission', $insert);
  313. continue;
  314. } else {
  315. $data = array();
  316. foreach ($_GPC['module_' . $module_val] as $v) {
  317. $data[] = $v;
  318. }
  319. if (!empty($data)) {
  320. $insert['permission'] = implode('|', $data);
  321. pdo_insert('users_permission', $insert);
  322. }
  323. }
  324. }
  325. }
  326. } else {
  327. $module_all_permission_data = array(
  328. 'uid' => $uid,
  329. 'uniacid' => $uniacid,
  330. 'type' => 'modules'
  331. );
  332. $module_all_permission = pdo_get('users_permission', $module_all_permission_data);
  333. if (empty($module_all_permission)) {
  334. $module_all_permission_data['permission'] = 'none';
  335. pdo_insert('users_permission', $module_all_permission_data);
  336. }
  337. }
  338. cache_delete(cache_system_key('permission', array('uniacid' => $uniacid, 'uid' => $uid)));
  339. itoast('操作菜单权限成功!', referer(), 'success');
  340. }
  341. template('account/set-permission');
  342. }
  343. if($do == 'module' && $_W['isajax']) {
  344. $uid = intval($_GPC['uid']);
  345. $user = user_single($uid);
  346. if(empty($user)) {
  347. iajax(1, '访问错误, 未找到指定操作用户.', '');
  348. }
  349. $founders = explode(',', $_W['config']['setting']['founder']);
  350. $isfounder = in_array($user['uid'], $founders);
  351. if($isfounder) {
  352. iajax(2, '访问错误, 无法编辑站长.', '');
  353. }
  354. $module_name = trim($_GPC['m']);
  355. $module = pdo_fetch('SELECT * FROM ' . tablename('modules') . ' WHERE name = :m', array(':m' => $module_name));
  356. $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));
  357. $user_module_permissions = permission_account_user_menu($uid, $uniacid, 'modules');
  358. if(!empty($purview['permission'])) {
  359. $purview['permission'] = explode('|', $purview['permission']);
  360. } else {
  361. $purview['permission'] = array();
  362. }
  363. $mineurl = array();
  364. $all = 0;
  365. if(!empty($mods)) {
  366. foreach($mods as $mod) {
  367. if($mod['url'] == 'all') {
  368. $all = 1;
  369. break;
  370. } else {
  371. $mineurl[] = $mod['url'];
  372. }
  373. }
  374. }
  375. $data = array();
  376. if($module['settings']) {
  377. $data[] = array('title' => '参数设置', 'permission' => $module_name.'_settings');
  378. }
  379. if($module['isrulefields']) {
  380. $data[] = array('title' => '回复规则列表', 'permission' => $module_name.'_rule');
  381. }
  382. $entries = module_entries($module_name);
  383. if(!empty($entries['home'])) {
  384. $data[] = array('title' => '微站首页导航', 'permission' => $module_name.'_home');
  385. }
  386. if(!empty($entries['profile'])) {
  387. $data[] = array('title' => '个人中心导航', 'permission' => $module_name.'_profile');
  388. }
  389. if(!empty($entries['shortcut'])) {
  390. $data[] = array('title' => '快捷菜单', 'permission' => $module_name.'_shortcut');
  391. }
  392. if(!empty($entries['cover'])) {
  393. foreach($entries['cover'] as $cover) {
  394. $data[] = array('title' => $cover['title'], 'permission' => $module_name.'_cover_'.$cover['do']);
  395. }
  396. }
  397. if(!empty($entries['menu'])) {
  398. foreach($entries['menu'] as $menu) {
  399. if (!empty($menu['multilevel'])) {
  400. continue;
  401. }
  402. $data[] = array('title' => $menu['title'], 'permission' => $module_name.'_menu_'.$menu['do']);
  403. }
  404. }
  405. unset($entries);
  406. if(!empty($module['permissions'])) {
  407. $module['permissions'] = (array)iunserializer($module['permissions']);
  408. $data = array_merge($data, $module['permissions']);
  409. }
  410. foreach($data as &$data_val) {
  411. $data_val['checked'] = 0;
  412. if(in_array($data_val['permission'], $purview['permission']) || in_array('all', $purview['permission']) || in_array('all', $user_module_permissions)) {
  413. $data_val['checked'] = 1;
  414. }
  415. }
  416. unset($data_val);
  417. if (empty($data)) {
  418. iajax(3, '无子权限!', '');
  419. } else {
  420. iajax(0, $data, '');
  421. }
  422. }