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

allow add auth record with some mac for user

rajven 5 лет назад
Родитель
Сommit
12927db925
4 измененных файлов с 71 добавлено и 65 удалено
  1. 18 16
      html/admin/users/editauth.php
  2. 39 8
      html/admin/users/edituser.php
  3. 0 3
      html/inc/auth.php
  4. 14 38
      html/inc/common.php

+ 18 - 16
html/admin/users/editauth.php

@@ -15,27 +15,28 @@ if (isset($_POST["editauth"]) and !$old_auth_info['deleted']) {
     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'];
+        $parent_id = $old_auth_info['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'];
+	if (isset($mac_exists) and $mac_exists['count']>=1 and !in_array($parent_id,$mac_exists['users_id'])) {
+	        $dup_sql = "SELECT * FROM User_list WHERE id=".$mac_exists['users_id']['0'];
+	        $dup_info = get_record_sql($db_link, $dup_sql);
+		$msg_error="Mac already exists at another user in this subnet! Skip creating $ip [$mac].<br>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;
 		}
+	//disable dhcp for secondary ip
 	$f_dhcp = $_POST["f_dhcp"] * 1;
-	if (in_array($parent_id,$mac_exists['users_id'],true)) {
+	if (in_array($parent_id,$mac_exists['users_id'])) {
 	    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'];
+            $msg_error = "$ip already exists. Skip creating $ip [$mac].<br>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"]);
@@ -99,27 +100,28 @@ if (isset($_POST["recovery"])) {
     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'];
+        $parent_id = $old_auth_info['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'];
+	if (isset($mac_exists) and $mac_exists['count']>=1 and !in_array($parent_id,$mac_exists['users_id'])) {
+	        $dup_sql = "SELECT * FROM User_list WHERE id=".$mac_exists['users_id']['0'];
+	        $dup_info = get_record_sql($db_link, $dup_sql);
+		$msg_error="Mac already exists at another user in this subnet! Skip creating $ip [$mac].<br>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;
 		}
+	//disable dhcp for secondary ip
 	$f_dhcp = $_POST["f_dhcp"] * 1;
-	if (in_array($parent_id,$mac_exists['users_id'],true)) {
+	if (in_array($parent_id,$mac_exists['users_id'])) {
 	    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'];
+            $msg_error = "$ip already exists. Skip creating $ip [$mac].<br>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"]);
@@ -171,8 +173,8 @@ 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>';
+if (!empty($_SESSION[$page_url]['msg'])) {
+    print '<div id="msg">'.$_SESSION[$page_url]['msg'].'</div>';
     unset($_SESSION[$page_url]['msg']);
     }
 print "<b> Адрес доступа пользователя <a href=/admin/users/edituser.php?id=".$auth_info['user_id'].">".$parent_name."</a> </b>";

+ 39 - 8
html/admin/users/edituser.php

@@ -46,16 +46,46 @@ if (isset($_POST["addauth"])) {
     if (isset($_POST["newmac"])) { $fmac = mac_dotted(substr(trim($_POST["newmac"]), 0, 17)); }
     if ($fip) {
         if (checkValidIp($fip)) {
-    		$fid = new_auth($db_link, $fip, $fmac, $id);
+            $ip_aton = ip2long($fip);
+            //search mac
+            $mac_exists=find_mac_in_subnet($db_link,$fip,$fmac);
+            if (isset($mac_exists) and $mac_exists['count']>=1 and !in_array($id,$mac_exists['users_id'])) {
+                $dup_sql = "SELECT * FROM User_list WHERE id=".$mac_exists['users_id']['0'];
+                $dup_info = get_record_sql($db_link, $dup_sql);
+                $msg_error="Mac already exists at another user in this subnet! Skip creating $fip [$fmac].<br>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;
+                }
+            //disable dhcp for secondary ip
+            $f_dhcp = 1;
+            if (in_array($id,$mac_exists['users_id'])) { $f_dhcp = 0; }
+            //search ip
+            $dup_ip_record = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND user_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 = "$fip already exists. Skip creating $fip [$fmac].<br>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;
+                }
+            $fid = new_auth($db_link, $fip, $fmac, $id);
+            if (!empty($fid)) {
+                $new['dhcp']=$f_dhcp;
+                update_record($db_link,"User_auth","id=".$fid,$new);
                 LOG_WARNING($db_link,"Создан новый адрес доступа: ip => $fip, mac => $fmac");
-                if (isset($fid)) { header("location: /admin/users/editauth.php?id=$fid"); }
-	        header("Location: " . $_SERVER["REQUEST_URI"]);
+                header("Location: /admin/users/editauth.php?id=".$fid);
+                }
+	    header("Location: " . $_SERVER["REQUEST_URI"]);
     	    } 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["removeauth"])) {
     $auth_id = $_POST["f_auth_id"];
@@ -173,9 +203,10 @@ require_once ($_SERVER["DOCUMENT_ROOT"]."/inc/header.php");
 ?>
 <div id="cont">
 <?php
-if ($msg_error) {
-    print "<div id='msg'><b>$msg_error</b></div><br>\n";
-}
+if (!empty($_SESSION[$page_url]['msg'])) {
+    print '<div id="msg">'.$_SESSION[$page_url]['msg'].'</div>';
+    unset($_SESSION[$page_url]['msg']);
+    }
 ?>
 <form name="def" action="edituser.php?id=<?php echo $id; ?>" method="post">
 <input type="hidden" name="id" value=<? echo $id; ?>>

+ 0 - 3
html/inc/auth.php

@@ -28,7 +28,4 @@ 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']; }
-
 ?>

+ 14 - 38
html/inc/common.php

@@ -1230,15 +1230,16 @@ 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,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");
+$t_auth=get_records_sql($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,$amac,$u_id)=mysqli_fetch_array($t_auth)) {
-    if (isset($aid) and $aid>0) {
+$result['users_id']=[];
+foreach ($t_auth as $row) {
+    if (!empty($row['id'])) {
 	$auth_count++;
 	$result['count']=$auth_count;
-	$result[$auth_count]=$aid;
-	array_push($result['users_id'],$u_id);
+	$result[$auth_count]=$row['id'];
+	array_push($result['users_id'],$row['user_id']);
 	}
     }
 return $result;
@@ -1271,9 +1272,6 @@ function new_auth($db, $ip, $mac, $user_id)
     $ip_aton = ip2long($ip);
     $msg = '';
 
-    // default id
-    $save_traf = get_option($db, 23);
-
     if (!empty($mac)) {
         list ($lid, $aid) = mysqli_fetch_array(mysqli_query($db, "Select user_id,id FROM User_auth WHERE ip_int=$ip_aton and mac='" . $mac . "' and deleted=0 limit 1"));
 	    if ($lid > 0) {
@@ -1282,42 +1280,19 @@ function new_auth($db, $ip, $mac, $user_id)
 	    }
 	}
 
-    $mac_exists=find_mac_in_subnet($db,$ip,$mac);
-    if (isset($mac_exists) and $mac_exists['count']>0) {
-        LOG_WARNING($db, "Mac $mac already exists in this subnet! Skip creating $ip [$mac] auth_id: ".$mac_exists['1']);
-        return;
-	}
-
-    // search changed mac
-    list ($aid, $amac) = mysqli_fetch_array(mysqli_query($db, "Select id,mac FROM User_auth WHERE ip_int=$ip_aton and deleted=0 limit 1"));
-    if ($aid > 0) {
-        if (empty($amac)) {
-            $auth['user_id'] = $user_id;
-            $auth['ip'] = $ip;
-            $auth['ip_int'] = $ip_aton;
-            $auth['mac'] = $mac;
-            $auth['deleted'] = 0;
-            $auth['save_traf'] = $save_traf *1;
-            LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
-            update_record($db, "User_auth", "id=" . $aid, $auth);
-            apply_auth_rule($db,$aid,$user_id);
-            return $aid;
-        } else {
-            LOG_WARNING($db, "Ip [$ip] already exists! Old mac: [$amac]. Skip creating!");
-            return $aid;
-        }
-    }
+    // default id
+    $save_traf = get_option($db, 23);
     $resurrection_id = NULL;
+
     // seek old auth with same ip and mac
-    if (get_count_records($db, 'User_auth', "ip_int=" . $ip_aton . " and mac='" . $mac . "'")) {
-        // found ->Resurrection old record
-        $resurrection_id = get_id_record($db, 'User_auth', "ip_int=" . $ip_aton . " and mac='" . $mac . "'");
+    $resurrection_id = get_id_record($db, 'User_auth', " deleted=1 AND ip_int=" . $ip_aton . " AND mac='" . $mac . "'");
+    if (!empty($resurrection_id)) {
         $msg.="Восстанавливаем доступ для auth_id: $resurrection_id with ip: $ip and mac: $mac ";
         $auth['user_id'] = $user_id;
         $auth['deleted'] = 0;
         $auth['save_traf'] = $save_traf *1;
         update_record($db, "User_auth", "id=$resurrection_id", $auth);
-    } else {
+        } else {
         // not found ->create new record
         $msg.="Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
         $auth['deleted'] = 0;
@@ -1327,7 +1302,8 @@ function new_auth($db, $ip, $mac, $user_id)
         $auth['mac'] = $mac;
         $auth['save_traf'] = $save_traf *1;
         $resurrection_id=insert_record($db, "User_auth", $auth);
-    }
+        }
+
     //check rules, update filter and state for new record
     if (!empty($resurrection_id)) {
         apply_auth_rule($db,$resurrection_id,$user_id);