Просмотр исходного кода

fix edit auth record with some mac in subnet

Dmitriev Roman 5 лет назад
Родитель
Сommit
5c3f1865ac
4 измененных файлов с 102 добавлено и 83 удалено
  1. 88 79
      html/admin/users/editauth.php
  2. 6 0
      html/admin/users/edituser.php
  3. 3 0
      html/inc/auth.php
  4. 5 4
      html/inc/common.php

+ 88 - 79
html/admin/users/editauth.php

@@ -13,51 +13,48 @@ $old_auth_info = get_record_sql($db_link, $sSQL);
 if (isset($_POST["editauth"]) and !$old_auth_info['deleted']) {
     $ip = trim($_POST["f_ip"]);
     if (checkValidIp($ip)) {
+        $ip_aton = ip2long($ip);
 	$mac=mac_dotted($_POST["f_mac"]);
+        $old_auth = get_record_sql($db_link, "SELECT user_id FROM User_auth WHERE id=$id");
+        $parent_id = $old_auth['user_id'];
+        //search mac
 	$mac_exists=find_mac_in_subnet($db_link,$ip,$mac);
-	if (isset($mac_exists) and $mac_exists['count']==1 and $mac_exists['1']!=$id) {
-	        LOG_ERROR($db_link, "Mac $mac already exists in this subnet! Skip creating $ip [$mac] auth_id: ".$mac_exists['1']);
+	if (isset($mac_exists) and $mac_exists['count']>=1 and !in_array($parent_id,$mac_exists['users_id'],true)) {
+	        $dup_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=".$mac_exists['users_id'][0]);
+		$msg_error="Mac already exists at another user in this subnet! Skip creating $ip [$mac]. Old user id: ".$dup_info['id']." login: ".$dup_info['login'];
+		$_SESSION[$page_url]['msg'] = $msg_error;
+	        LOG_ERROR($db_link, $msg_error);
 	        header("Location: " . $_SERVER["REQUEST_URI"]);
 	        exit;
 		}
-	if (isset($mac_exists) and $mac_exists['count']>1) {
-	        LOG_ERROR($db_link, "Mac $mac already exists in this subnet! Skip creating $ip [$mac] auth_id: ".$mac_exists['2']);
-	        header("Location: " . $_SERVER["REQUEST_URI"]);
-	        exit;
-		}
-        $range = cidrToRange($ip);
-        $first_user_ip = $range[0];
-        $last_user_ip = $range[1];
-        $cidr = $range[2][1];
-        if (isset($cidr) and $cidr < 32) {
-            $ip = $first_user_ip . '/' . $cidr;
-        } else {
-            $ip = $first_user_ip;
-        }
-        $ip_aton = ip2long($first_user_ip);
-        $ip_aton_end = ip2long($last_user_ip);
-        $parent_user = get_record_sql($db_link, "SELECT user_id FROM User_auth WHERE id=$id");
-        $parent_id = $parent_user['user_id'];
-        $locate_user = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND id<>$id AND deleted=0");
-        $lid = $locate_user['user_id'];
-        if (isset($lid) and ($lid != $parent_id)) {
-            $msg_error = "$ip $msg_exists Принадлежит пользователю ".$locate_user['login'];
-            unset($_POST);
-    	    } else {
-            $new['ip'] = $ip;
-            $new['ip_int'] = $ip_aton;
-            $new['mac'] = mac_dotted($_POST["f_mac"]);
-//            $new['clientid'] = $_POST["f_clientid"];
-            $new['comments'] = $_POST["f_comments"];
-            $new['firmware'] = $_POST["f_firmware"];
-            $new['WikiName'] = $_POST["f_wiki"];
-            $f_dnsname=trim($_POST["f_dns_name"]);
-            if (!empty($f_dnsname) and checkValidHostname($f_dnsname) and checkUniqHostname($db_link,$id,$f_dnsname)) { $new['dns_name'] = $f_dnsname; }
-            if (empty($f_dnsname)) { $new['dns_name'] = ''; }
-            $new['device_model_id'] = $_POST["f_device_model_id"]*1;
-            $new['save_traf'] = $_POST["f_save_traf"] * 1;
-            $new['dhcp_acl'] = trim($_POST["f_acl"]);
-            if ($default_user_id == $parent_id or $hotspot_user_id == $parent_id) {
+	$f_dhcp = $_POST["f_dhcp"] * 1;
+	if (in_array($parent_id,$mac_exists['users_id'],true)) {
+	    if ($id != $mac_exists['users_id'][0]) { $f_dhcp = 0; }
+	    }
+	//search ip
+        $dup_ip_record = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND id<>$id AND deleted=0");
+        if (!empty($dup_ip_record)) {
+            $dup_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=".$dup_ip_record['user_id']);
+            $msg_error = "$ip already exists. Skip creating $ip [$mac]. Old user id: ".$dup_info['id']." login: ".$dup_info['login'];
+	    $_SESSION[$page_url]['msg'] = $msg_error;
+            LOG_ERROR($db_link, $msg_error);
+            header("Location: " . $_SERVER["REQUEST_URI"]);
+            exit;
+    	    }
+        $new['ip'] = $ip;
+        $new['ip_int'] = $ip_aton;
+        $new['mac'] = mac_dotted($_POST["f_mac"]);
+//      $new['clientid'] = $_POST["f_clientid"];
+        $new['comments'] = $_POST["f_comments"];
+        $new['firmware'] = $_POST["f_firmware"];
+        $new['WikiName'] = $_POST["f_wiki"];
+        $f_dnsname=trim($_POST["f_dns_name"]);
+        if (!empty($f_dnsname) and checkValidHostname($f_dnsname) and checkUniqHostname($db_link,$id,$f_dnsname)) { $new['dns_name'] = $f_dnsname; }
+        if (empty($f_dnsname)) { $new['dns_name'] = ''; }
+        $new['device_model_id'] = $_POST["f_device_model_id"]*1;
+        $new['save_traf'] = $_POST["f_save_traf"] * 1;
+        $new['dhcp_acl'] = trim($_POST["f_acl"]);
+        if ($default_user_id == $parent_id or $hotspot_user_id == $parent_id) {
                 $new['nagios_handler'] = '';
                 $new['enabled'] = 0;
                 $new['link_check'] = 0;
@@ -72,22 +69,22 @@ if (isset($_POST["editauth"]) and !$old_auth_info['deleted']) {
                 $new['enabled'] = $_POST["f_enabled"] * 1;
                 $new['link_check'] = $_POST["f_link"] * 1;
                 $new['nagios'] = $_POST["f_nagios"] * 1;
-                $new['dhcp'] = $_POST["f_dhcp"] * 1;
+                $new['dhcp'] = $f_dhcp;
                 $new['blocked'] = $_POST["f_blocked"] * 1;
                 $new['day_quota'] = $_POST["f_day_q"] * 1;
                 $new['month_quota'] = $_POST["f_month_q"] * 1;
                 $new['queue_id'] = $_POST["f_queue_id"] * 1;
                 $new['filter_group_id'] = $_POST["f_group_id"] * 1;
             }
-            $changes = get_diff_rec($db_link,"User_auth","id='$id'", $new, 0);
-            if (!empty($changes)) { LOG_WARNING($db_link,"Изменен адрес доступа! Список изменений: $changes"); }
-            update_record($db_link, "User_auth", "id='$id'", $new);
-        }
+        $changes = get_diff_rec($db_link,"User_auth","id='$id'", $new, 0);
+        if (!empty($changes)) { LOG_WARNING($db_link,"Изменен адрес доступа! Список изменений: $changes"); }
+        update_record($db_link, "User_auth", "id='$id'", $new);
     } else {
-        $msg_error = "$msg_ip_error xxx.xxx.xxx.xxx/xx";
-    }
+	$msg_error = "$msg_ip_error xxx.xxx.xxx.xxx";
+        $_SESSION[$page_url]['msg'] = $msg_error;
+	}
     header("Location: " . $_SERVER["REQUEST_URI"]);
-}
+    }
 
 if (isset($_POST["moveauth"]) and !$old_auth_info['deleted']) {
     $new['user_id'] = $_POST["new_parent"];
@@ -95,33 +92,41 @@ if (isset($_POST["moveauth"]) and !$old_auth_info['deleted']) {
     if (!empty($changes)) { LOG_WARNING($db_link,"Адрес доступа перемещён к другому пользователю! Применено: $changes"); }
     update_record($db_link, "User_auth", "id='$id'", $new);
     header("Location: " . $_SERVER["REQUEST_URI"]);
-}
+    }
 
 if (isset($_POST["recovery"])) {
     $ip = trim($_POST["f_ip"]);
     if (checkValidIp($ip)) {
-        $range = cidrToRange($ip);
-        $first_user_ip = $range[0];
-        $last_user_ip = $range[1];
-        $cidr = $range[2][1];
-        if (isset($cidr) and $cidr < 32) {
-            $ip = $first_user_ip . '/' . $cidr;
-        } else {
-            $ip = $first_user_ip;
-        }
-        $ip_aton = ip2long($first_user_ip);
-        $ip_aton_end = ip2long($last_user_ip);
-
-        $parent_user = get_record_sql($db_link, "SELECT user_id FROM User_auth WHERE id=$id");
-        $parent_id = $parent_user['user_id'];
-        $locate_user = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND id<>$id AND deleted=0");
-        $lid = $locate_user['user_id'];
-        if (isset($lid) and ($lid != $parent_id)) {
-            $msg_error = "$ip $msg_exists Принадлежит пользователю ".$locate_user['login'];
-            unset($_POST);
-            } else {
-            $new['deleted'] = 0;
-            if ($default_user_id == $parent_id or $hotspot_user_id == $parent_id) {
+        $ip_aton = ip2long($ip);
+	$mac=mac_dotted($_POST["f_mac"]);
+        $old_auth = get_record_sql($db_link, "SELECT user_id FROM User_auth WHERE id=$id");
+        $parent_id = $old_auth['user_id'];
+        //search mac
+	$mac_exists=find_mac_in_subnet($db_link,$ip,$mac);
+	if (isset($mac_exists) and $mac_exists['count']>=1 and !in_array($parent_id,$mac_exists['users_id'],true)) {
+	        $dup_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=".$mac_exists['users_id'][0]);
+		$msg_error="Mac already exists at another user in this subnet! Skip creating $ip [$mac]. Old user id: ".$dup_info['id']." login: ".$dup_info['login'];
+		$_SESSION[$page_url]['msg'] = $msg_error;
+	        LOG_ERROR($db_link, $msg_error);
+	        header("Location: " . $_SERVER["REQUEST_URI"]);
+	        exit;
+		}
+	$f_dhcp = $_POST["f_dhcp"] * 1;
+	if (in_array($parent_id,$mac_exists['users_id'],true)) {
+	    if ($id != $mac_exists['users_id'][0]) { $f_dhcp = 0; }
+	    }
+	//search ip
+        $dup_ip_record = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND id<>$id AND deleted=0");
+        if (!empty($dup_ip_record)) {
+            $dup_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=".$dup_ip_record['user_id']);
+            $msg_error = "$ip already exists. Skip creating $ip [$mac]. Old user id: ".$dup_info['id']." login: ".$dup_info['login'];
+	    $_SESSION[$page_url]['msg'] = $msg_error;
+            LOG_ERROR($db_link, $msg_error);
+            header("Location: " . $_SERVER["REQUEST_URI"]);
+            exit;
+    	    }
+        $new['deleted'] = 0;
+        if ($default_user_id == $parent_id or $hotspot_user_id == $parent_id) {
                 $new['nagios_handler'] = '';
                 $new['enabled'] = 0;
                 $new['link_check'] = 0;
@@ -143,15 +148,15 @@ if (isset($_POST["recovery"])) {
                 $new['queue_id'] = $_POST["f_queue_id"] * 1;
                 $new['filter_group_id'] = $_POST["f_group_id"] * 1;
             }
-            $changes = get_diff_rec($db_link,"User_auth","id='$id'", $new, 0);
-            if (!empty($changes)) { LOG_WARNING($db_link,"Восстановлен адрес доступа! Применено: $changes"); }
-            update_record($db_link, "User_auth", "id='$id'", $new);
-        }
-    } else {
+        $changes = get_diff_rec($db_link,"User_auth","id='$id'", $new, 0);
+        if (!empty($changes)) { LOG_WARNING($db_link,"Восстановлен адрес доступа! Применено: $changes"); }
+        update_record($db_link, "User_auth", "id='$id'", $new);
+	} else {
         $msg_error = "$msg_ip_error xxx.xxx.xxx.xxx/xx";
-    }
+        $_SESSION[$page_url]['msg'] = $msg_error;
+	}
     header("Location: " . $_SERVER["REQUEST_URI"]);
-}
+    }
 
 unset($_POST);
 
@@ -165,7 +170,11 @@ if ($auth_info['dhcp_time'] == '0000-00-00 00:00:00') { $dhcp_str = ''; } else {
 if ($auth_info['last_found'] == '0000-00-00 00:00:00') { $auth_info['last_found'] = ''; }
 ?>
 <div id="cont">
-<?
+<?php
+if (!empty($msg_error)) {
+    print '<div id="msg">'.$msg_error.'</div>';
+    unset($_SESSION[$page_url]['msg']);
+    }
 print "<b> Адрес доступа пользователя <a href=/admin/users/edituser.php?id=".$auth_info['user_id'].">".$parent_name."</a> </b>";
 ?>
 <form name="def" action="editauth.php?id=<? echo $id; ?>" method="post">

+ 6 - 0
html/admin/users/edituser.php

@@ -76,6 +76,7 @@ if (isset($_POST["removeauth"])) {
 if (isset($_POST["ApplyForAll"])) {
     $auth_id = $_POST["f_auth_id"];
     $a_enabled = $_POST["a_enabled"] * 1;
+    $a_dhcp = $_POST["a_dhcp"] * 1;
     $a_day = $_POST["a_day_q"] * 1;
     $a_month = $_POST["a_month_q"] * 1;
     $a_queue = $_POST["a_queue_id"] * 1;
@@ -84,12 +85,14 @@ if (isset($_POST["ApplyForAll"])) {
         if ($val) {
             unset($new);
             if ($default_user_id == $id or $hotspot_user_id == $id) {
+                $new["dhcp"] = 0;
                 $new["enabled"] = 0;
                 $new["day_quota"] = 0;
                 $new["month_quota"] = 0;
                 $new["queue_id"] = 0;
                 $new["filter_group_id"] = 0;
             } else {
+                $new["dhcp"] = $a_dhcp;
                 $new["enabled"] = $a_enabled;
                 $new["day_quota"] = $a_day;
                 $new["month_quota"] = $a_month;
@@ -226,6 +229,7 @@ if ($msg_error) { print "<div id='msg'><b>$msg_error</b></div><br>\n"; }
 <table class="data">
 <tr>
 <td>Для выделенных установить: Включен&nbsp<?php print_qa_select('a_enabled', 0); ?></td>
+<td>DHCP&nbsp<?php print_qa_select('a_dhcp', 0); ?></td>
 <td>Фильтр&nbsp<?php print_group_select($db_link, 'a_group_id', 0); ?></td>
 <td>Шейпер&nbsp<?php print_queue_select($db_link, 'a_queue_id', 0); ?></td>
 <td>В день&nbsp<input type="text" name="a_day_q" value="0" size=5></td>
@@ -265,6 +269,7 @@ if ($id == $default_user_id or $id == $hotspot_user_id) { $default_sort = 'last_
 <td class="data"><?php print $cell_comment; ?></td>
 <td class="data"><?php print $sort_url . "&sort=dns_name&order=$new_order>" . $cell_dns_name . "</a>"; ?></td>
 <td class="data"><?php print $cell_enabled; ?></td>
+<td class="data"><?php print $cell_dhcp; ?></td>
 <td class="data"><?php print $cell_filter; ?></td>
 <td class="data"><?php print $cell_shaper; ?></td>
 <td class="data"><?php print $cell_perday."/<br>".$cell_permonth.", Mb"; ?></td>
@@ -298,6 +303,7 @@ if (!empty($flist)) {
         $ip_status = 1;
         if ($row["blocked"] or !$row["enabled"]) { $ip_status = 0; }
         print "<td class=\"data\" >" . get_qa($ip_status). "</td>\n";
+        print "<td class=\"data\" >" . get_qa($row["dhcp"]). "</td>\n";
         print "<td class=\"data\" >" . get_group($db_link, $row["filter_group_id"]) . "</td>\n";
         print "<td class=\"data\" >" . get_queue($db_link, $row["queue_id"]) . "</td>\n";
         print "<td class=\"data\" >".$row["day_quota"]."/".$row["month_quota"]."</td>\n";

+ 3 - 0
html/inc/auth.php

@@ -28,4 +28,7 @@ if (!isset($displayed)) { $displayed=$default_displayed; }
 $_SESSION[$page_url]['page']=$page;
 $_SESSION[$page_url]['rows']=$displayed;
 
+$msg_error='';
+if (!empty($_SESSION[$page_url]['msg'])) { $msg_error=$_SESSION[$page_url]['msg']; }
+
 ?>

+ 5 - 4
html/inc/common.php

@@ -1230,14 +1230,15 @@ if (empty($ip)) { return; }
 if (empty($mac)) { return; }
 $ip_subnet=get_ip_subnet($db,$ip);
 if (!isset($ip_subnet)) { return; }
-$t_auth=mysqli_query($db, "SELECT id FROM User_auth WHERE ip_int>=".$ip_subnet['int_start']." and ip_int<=".$ip_subnet['int_stop']." and mac='" . $mac . "' and deleted=0");
+$t_auth=mysqli_query($db, "SELECT id,mac,user_id FROM User_auth WHERE ip_int>=".$ip_subnet['int_start']." and ip_int<=".$ip_subnet['int_stop']." and mac='" . $mac . "' and deleted=0 ORDER BY id");
 $auth_count=0;
 $result['count']=0;
-while (list($aid)=mysqli_fetch_array($t_auth)) {
-    if (isset($aid) and $aid>0) { 
-	$auth_count++; 
+while (list($aid,$amac,$u_id)=mysqli_fetch_array($t_auth)) {
+    if (isset($aid) and $aid>0) {
+	$auth_count++;
 	$result['count']=$auth_count;
 	$result[$auth_count]=$aid;
+	array_push($result['users_id'],$u_id);
 	}
     }
 return $result;