Bläddra i källkod

add create autorule for user list page

Roman Dmitriev 2 år sedan
förälder
incheckning
ffc3497bb7
3 ändrade filer med 37 tillägg och 4 borttagningar
  1. 1 0
      html/admin/users/index.php
  2. 32 1
      html/utils/user_apply.php
  3. 4 3
      scripts/fetch_new_arp.pl

+ 1 - 0
html/admin/users/index.php

@@ -74,6 +74,7 @@ if ($msg_error) {
         <tr><td><input type=checkbox class="putField" name="e_day_q" value='1'></td><td><?php print WEB_cell_perday."&nbsp"; ?><input type="text" name="a_day_q" value="0" size=5></td></tr>
         <tr><td><input type=checkbox class="putField" name="e_month_q" value='1'></td><td><?php print WEB_cell_permonth."&nbsp"; ?><input type="text" name="a_month_q" value="0" size=5></td></tr>
         <tr><td><input type=checkbox class="putField" name="e_new_ou" value='1'></td><td><?php print WEB_cell_ou."&nbsp";print_ou_select($db_link, 'a_new_ou', $rou); ?></td></tr>
+        <tr><td><input type=checkbox class="putField" name="e_bind_mac" value='1'></td><td><?php print WEB_user_bind_mac."&nbsp";print_qa_select('a_bind_mac', 1);?></td></tr>
         </table>
         <input type="submit" name="submit" class="btn" value="<?php echo WEB_btn_apply; ?>">
     </form>

+ 32 - 1
html/utils/user_apply.php

@@ -34,6 +34,9 @@ if (isset($_POST["ApplyForAll"])) {
         $_POST["a_new_ou"] = 0;
     }
 
+    if (empty($_POST["a_bind_mac"])) {
+        $_POST["a_bind_mac"] = 0;
+    }
 
     $a_enabled  = $_POST["a_enabled"] * 1;
     $a_dhcp     = $_POST["a_dhcp"] * 1;
@@ -45,6 +48,8 @@ if (isset($_POST["ApplyForAll"])) {
     $a_month    = $_POST["a_month_q"] * 1;
     $a_ou_id    = $_POST["a_new_ou"] * 1;
 
+    $a_bind_mac = $_POST["a_bind_mac"] * 1;
+
     $msg = "Massive User change!";
     LOG_WARNING($db_link, $msg);
 
@@ -91,18 +96,44 @@ if (isset($_POST["ApplyForAll"])) {
                 $all_ok = 0;
             }
 
-            $auth_list = get_records_sql($db_link, "SELECT id FROM User_auth WHERE deleted=0 AND user_id=" . $val);
+            $auth_list = get_records_sql($db_link, "SELECT id, mac FROM User_auth WHERE deleted=0 AND user_id=" . $val);
+            $b_mac = '';
             if (!empty($auth)) {
                 foreach ($auth_list as $row) {
                     if (empty($row)) {
                         continue;
                     }
+                    if (empty($b_mac) and !empty($row["mac"])) {
+                        $b_mac = $row["mac"];
+                    }
                     $ret = update_record($db_link, "User_auth", "id='" . $row["id"] . "'", $auth);
                     if (!$ret) {
                         $all_ok = 0;
                     }
                 }
             }
+
+            //bind mac rule
+            if (isset($_POST["e_bind_mac"]) and !empty($b_mac)) {
+                if ($a_bind_mac) {
+                    $auth_rules_user = get_record_sql($db_link, "SELECT * FROM auth_rules WHERE user_id=" . $val . " AND type=2");
+                    $auth_rules_mac = get_record_sql($db_link, "SELECT * FROM auth_rules WHERE rule='" . $b_mac . "' AND type=2");
+                    if (empty($auth_rules_user) and empty($auth_rules_mac)) {
+                        $new['user_id'] = $val;
+                        $new['type'] = 2;
+                        $new['rule'] = $b_mac;
+                        insert_record($db_link, "auth_rules", $new);
+                        LOG_INFO($db_link, "Created auto rule for user_id: " . $val . " and mac " . $b_mac);
+                    } else {
+                        LOG_INFO($db_link, "Auto rule for user_id: " . $val . " and mac " . $mac . " already exists");
+                    }
+                } else {
+                    run_sql($db_link, "DELETE FROM auth_rules WHERE user_id=" . $val . " AND type=2");
+                    LOG_INFO($db_link, "Remove auto rule for user_id: " . $val . " and mac " . $b_mac);
+                }
+            } else {
+                LOG_ERROR($db_link, "Auto rule for user_id: " . $first_auth['user_id'] . " not created. Record not found or empty mac.");
+            }
         }
     }
     if ($all_ok) {

+ 4 - 3
scripts/fetch_new_arp.pl

@@ -314,6 +314,7 @@ foreach my $mac (keys %mac_address_table) {
 
     $mac_history{$simple_mac}{port_id}=$port_index{$port};
     $mac_history{$simple_mac}{dev_id}=$dev_id;
+
     if (!$mac_history{$simple_mac}{changed}) { $mac_history{$simple_mac}{changed}=0; }
 
     my $port_id=$port_index{$port};
@@ -332,7 +333,7 @@ foreach my $mac (keys %mac_address_table) {
                         if (exists $auth_table{oper_table}{$simple_mac}) {
                     	    my $auth_rec;
                     	    $auth_rec->{last_found}=$now_str;
-	                    update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
+	                        update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
                     	    }
                         next;
                         }
@@ -340,7 +341,7 @@ foreach my $mac (keys %mac_address_table) {
                     $connections{$auth_id}{port}=$port_id;
                     $mac_history{$simple_mac}{changed}=1;
                     $mac_history{$simple_mac}{auth_id}=$auth_id;
-                    db_log_info($dbh,"Found auth_id: $auth_id [$mac_splitted] at device $dev_name [$port]. Update connection",$auth_id);
+                    db_log_info($dbh,"Found auth_id: $auth_id ip: $mac_history{$simple_mac}{ip} [$mac_splitted] at device $dev_name [$port]. Update connection",$auth_id);
                     my $auth_rec;
                     $auth_rec->{last_found}=$now_str;
                     update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);
@@ -352,7 +353,7 @@ foreach my $mac (keys %mac_address_table) {
                     $mac_history{$simple_mac}{changed}=1;
                     $mac_history{$simple_mac}{auth_id}=$auth_id;
                     $connections{$auth_id}{port}=$port_id;
-                    db_log_info($dbh,"Found auth_id: $auth_id [$mac_splitted] at device $dev_name [$port]. Create connection.",$auth_id);
+                    db_log_info($dbh,"Found auth_id: $auth_id ip: $mac_history{$simple_mac}{ip} [$mac_splitted] at device $dev_name [$port]. Create connection.",$auth_id);
                     my $auth_rec;
                     $auth_rec->{last_found}=$now_str;
                     update_record($dbh,'User_auth',$auth_rec,"id=".$auth_id);