auth_apply.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/auth.php");
  3. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/languages/" . HTML_LANG . ".php");
  4. if (!defined("CONFIG")) die("Not defined");
  5. $page_url = null;
  6. if (getPOST("ApplyForAll", $page_url)) {
  7. // Получаем массив ID авторизаций
  8. $auth_id = getPOST("fid", $page_url, []);
  9. // Получаем и валидируем все параметры через getPOST
  10. $a_ou_id = (int)getPOST("a_new_ou", $page_url, 0);
  11. $a_enabled = (int)getPOST("a_enabled", $page_url, 0);
  12. $a_dhcp = (int)getPOST("a_dhcp", $page_url, 0);
  13. $a_dhcp_acl = trim(getPOST("a_dhcp_acl", $page_url, ''));
  14. $a_dhcp_option_set = trim(getPOST("a_dhcp_option_set", $page_url, ''));
  15. $a_queue = (int)getPOST("a_queue_id", $page_url, 0);
  16. $a_group = (int)getPOST("a_group_id", $page_url, 0);
  17. $a_traf = (int)getPOST("a_traf", $page_url, 0);
  18. $a_bind_mac = (int)getPOST("a_bind_mac", $page_url, 0);
  19. $a_bind_ip = (int)getPOST("a_bind_ip", $page_url, 0);
  20. $n_enabled = (int)getPOST("n_enabled", $page_url, 0);
  21. $n_link = (int)getPOST("n_link", $page_url, 0);
  22. $n_handler = getPOST("n_handler", $page_url, '');
  23. $msg = "Massive User change!";
  24. LOG_WARNING($db_link, $msg);
  25. $all_ok = true;
  26. foreach ($auth_id as $val) {
  27. $id = (int)$val;
  28. if ($id <= 0) continue;
  29. // Получаем текущую авторизацию и пользователя
  30. $cur_auth = get_record_sql($db_link, "SELECT * FROM user_auth WHERE id = ?", [$id]);
  31. if (!$cur_auth) continue;
  32. $user_info = get_record_sql($db_link, "SELECT * FROM user_list WHERE id = ?", [(int)$cur_auth["user_id"]]);
  33. if (!$user_info) continue;
  34. // Формируем данные для обновления auth
  35. $auth_updates = [];
  36. if (getPOST("e_enabled", $page_url) !== null) {
  37. $auth_updates['enabled'] = (int)($user_info["enabled"] * $a_enabled);
  38. }
  39. if (getPOST("e_group_id", $page_url) !== null) {
  40. $auth_updates['filter_group_id'] = $a_group;
  41. }
  42. if (getPOST("e_queue_id", $page_url) !== null) {
  43. $auth_updates['queue_id'] = $a_queue;
  44. }
  45. if (getPOST("e_dhcp", $page_url) !== null) {
  46. $auth_updates['dhcp'] = $a_dhcp;
  47. }
  48. if (getPOST("e_dhcp_acl", $page_url) !== null) {
  49. $auth_updates['dhcp_acl'] = $a_dhcp_acl;
  50. }
  51. if (getPOST("e_dhcp_option_set", $page_url) !== null) {
  52. $auth_updates['dhcp_option_set'] = $a_dhcp_option_set;
  53. }
  54. if (getPOST("e_traf", $page_url) !== null) {
  55. $auth_updates['save_traf'] = $a_traf;
  56. }
  57. if (getPOST("e_nag_enabled", $page_url) !== null) {
  58. $auth_updates['nagios'] = $n_enabled;
  59. }
  60. if (getPOST("e_nag_link", $page_url) !== null) {
  61. $auth_updates['link_check'] = $n_link;
  62. }
  63. if (getPOST("e_nag_handler", $page_url) !== null) {
  64. $auth_updates['nagios_handler'] = $n_handler;
  65. }
  66. // Обновляем запись в user_auth
  67. if (!empty($auth_updates)) {
  68. $ret = update_record($db_link, "user_auth", "id = ?", $auth_updates, [$id]);
  69. if (!$ret) $all_ok = false;
  70. }
  71. // Изменение группы пользователя
  72. if (getPOST("e_new_ou", $page_url) !== null && $a_ou_id >0) {
  73. $user_updates = ['ou_id' => $a_ou_id];
  74. $auth_updates_for_all = ['ou_id' => $a_ou_id];
  75. $log_msg = "For user id: " . $cur_auth['user_id'] . " login: " . ($user_info['login'] ?? '') . " set: ou_id = " . $a_ou_id;
  76. LOG_INFO($db_link, $log_msg);
  77. // Обновляем user_list
  78. $ret = update_record($db_link, "user_list", "id = ?", $user_updates, [(int)$cur_auth['user_id']]);
  79. if (!$ret) $all_ok = false;
  80. // Обновляем все записи user_auth для этого пользователя
  81. $ret = update_records($db_link, "user_auth", "user_id = ?", $auth_updates_for_all, [(int)$cur_auth['user_id']]);
  82. if (!$ret) $all_ok = false;
  83. }
  84. // Правило привязки MAC
  85. if (getPOST("e_bind_mac", $page_url) !== null) {
  86. if ($cur_auth && !empty($cur_auth['mac'])) {
  87. if ($a_bind_mac) {
  88. $user_rule = get_record_sql($db_link, "SELECT * FROM auth_rules WHERE user_id = ? AND rule_type = 2", [(int)$cur_auth['user_id']]);
  89. $mac_rule = get_record_sql($db_link, "SELECT * FROM auth_rules WHERE rule = ? AND rule_type = 2", [$cur_auth['mac']]);
  90. if (!$user_rule && !$mac_rule) {
  91. $new_rule = [
  92. 'user_id' => (int)$cur_auth['user_id'],
  93. 'rule_type' => 2,
  94. 'rule' => $cur_auth['mac']
  95. ];
  96. insert_record($db_link, "auth_rules", $new_rule);
  97. LOG_INFO($db_link, "Created auto rule for user_id: " . $cur_auth['user_id'] . " and mac " . $cur_auth['mac']);
  98. } else {
  99. LOG_INFO($db_link, "Auto rule for user_id: " . $cur_auth['user_id'] . " and mac " . $cur_auth['mac'] . " already exists");
  100. }
  101. } else {
  102. run_sql($db_link, "DELETE FROM auth_rules WHERE user_id = ? AND rule_type = 2", [(int)$cur_auth['user_id']]);
  103. LOG_INFO($db_link, "Remove auto rule for user_id: " . $cur_auth['user_id'] . " and mac " . $cur_auth['mac']);
  104. }
  105. } else {
  106. LOG_ERROR($db_link, "Auto rule for user_id: " . ($cur_auth['user_id'] ?? 'N/A') . " not created. Record not found or empty mac.");
  107. }
  108. }
  109. // Правило привязки IP
  110. if (getPOST("e_bind_ip", $page_url) !== null) {
  111. if ($cur_auth && !empty($cur_auth['ip'])) {
  112. if ($a_bind_ip) {
  113. $user_rule = get_record_sql($db_link, "SELECT * FROM auth_rules WHERE user_id = ? AND rule_type = 1", [(int)$cur_auth['user_id']]);
  114. $ip_rule = get_record_sql($db_link, "SELECT * FROM auth_rules WHERE rule = ? AND rule_type = 1", [$cur_auth['ip']]);
  115. if (!$user_rule && !$ip_rule) {
  116. $new_rule = [
  117. 'user_id' => (int)$cur_auth['user_id'],
  118. 'rule_type' => 1,
  119. 'rule' => $cur_auth['ip']
  120. ];
  121. insert_record($db_link, "auth_rules", $new_rule);
  122. LOG_INFO($db_link, "Created auto rule for user_id: " . $cur_auth['user_id'] . " and ip " . $cur_auth['ip']);
  123. } else {
  124. LOG_INFO($db_link, "Auto rule for user_id: " . $cur_auth['user_id'] . " and ip " . $cur_auth['ip'] . " already exists");
  125. }
  126. } else {
  127. run_sql($db_link, "DELETE FROM auth_rules WHERE user_id = ? AND rule_type = 1", [(int)$cur_auth['user_id']]);
  128. LOG_INFO($db_link, "Remove auto rule for user_id: " . $cur_auth['user_id'] . " and ip " . $cur_auth['ip']);
  129. }
  130. } else {
  131. LOG_ERROR($db_link, "Auto rule for user_id: " . ($cur_auth['user_id'] ?? 'N/A') . " not created. Record not found or empty ip.");
  132. }
  133. }
  134. }
  135. if ($all_ok) {
  136. print "Success!";
  137. } else {
  138. print "Fail!";
  139. }
  140. }
  141. ?>