0 ORDER BY id");
if (!empty($first_auth) and !empty($first_auth['mac'])) {
$new['user_id']=$id;
$new['type']=2;
$new['rule']=$first_auth['mac'];
insert_record($db_link,"auth_rules",$new);
LOG_INFO($db_link,"Создано правило атоназначения юзеру id: ".$id." login: ".$user_info["login"]." для мака ".$first_auth['mac']);
}
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
}
if (isset($_POST["delMacRule"])) {
run_sql($db_link,"DELETE FROM auth_rules WHERE user_id=".$id." AND type=2");
LOG_INFO($db_link,"Удалены все правила атоназначения юзеру id: $id login: ".$user_info["login"]." по маку");
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
}
if (isset($_POST["addIPRule"])) {
unset($new);
$first_auth = get_record_sql($db_link,"SELECT ip FROM User_auth WHERE user_id=".$id." AND deleted=0 AND LENGTH(ip)>0 ORDER BY id");
if (!empty($first_auth) and !empty($first_auth['ip'])) {
$new['user_id']=$id;
$new['type']=1;
$new['rule']=$first_auth['ip'];
insert_record($db_link,"auth_rules",$new);
LOG_INFO($db_link,"Создано правило атоназначения юзеру id: ".$id." login: ".$user_info["login"]." для IP ".$first_auth['IP']);
}
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
}
if (isset($_POST["delIPRule"])) {
run_sql($db_link,"DELETE FROM auth_rules WHERE user_id=".$id." AND type=1");
LOG_INFO($db_link,"Удалены все правила атоназначения юзеру id: $id login: ".$user_info["login"]." по ip");
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
}
if (isset($_POST["showDevice"])) {
$device = get_record_sql($db_link,"SELECT * FROM devices WHERE user_id=".$id);
$auth = get_record_sql($db_link,"SELECT * FROM User_auth WHERE user_id=".$id);
if (empty($device) and !empty($auth)) {
$new['user_id']=$id;
$new['device_name'] = $user_info['login'];
$new['device_type'] = 5;
$new['ip']=$auth['ip'];
$new['community'] = get_const('snmp_default_community');
$new['snmp_version'] = get_const('snmp_default_version');
$new_id=insert_record($db_link, "devices", $new);
unset($_POST);
if (!empty($new_id)) {
LOG_INFO($db_link, "Created device with id: $new_id for auth_id: $id");
header("Location: /admin/devices/editdevice.php?id={$new_id}");
exit;
} else {
header("Location: ".$_SERVER["REQUEST_URI"]);
exit;
}
}
header("Location: /admin/devices/editdevice.php?id=".$device['id']);
exit;
}
if (isset($_POST["addauth"])) {
$fip = substr(trim($_POST["newip"]), 0, 18);
if (isset($_POST["newmac"])) { $fmac = mac_dotted(substr(trim($_POST["newmac"]), 0, 17)); }
if ($fip) {
if (checkValidIp($fip)) {
$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].
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].
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);
apply_auth_rule($db_link,$fid,$id);
LOG_WARNING($db_link,"Создан новый адрес доступа для login: ".$user_info["login"].": ip => $fip, mac => $fmac",$fid);
header("Location: /admin/users/editauth.php?id=".$fid);
exit;
}
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
} else {
$msg_error = "$msg_ip_error xxx.xxx.xxx.xxx";
$_SESSION[$page_url]['msg'] = $msg_error;
}
}
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
}
if (isset($_POST["removeauth"])) {
$auth_id = $_POST["f_auth_id"];
foreach ($auth_id as $key => $val) {
if ($val) {
run_sql($db_link, 'DELETE FROM connections WHERE auth_id='.$val);
run_sql($db_link, 'DELETE FROM User_auth_alias WHERE auth_id='.$val);
$auth["deleted"] = 1;
$changes = get_diff_rec($db_link,"User_auth","id='$val'", '', 0);
if (!empty($changes)) { LOG_WARNING($db_link,"Удалён адрес доступа для login: ".$user_info["login"].": \r\n $changes",$val); }
update_record($db_link, "User_auth", "id=" . $val, $auth);
}
}
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
}
if (isset($_POST["new_user"])) {
$auth_id = $_POST["f_auth_id"];
$save_traf = get_option($db_link, 23) * 1;
foreach ($auth_id as $key => $val) {
if ($val) {
$auth_info = get_record_sql($db_link,"SELECT ip, mac, comments, dns_name, dhcp_hostname FROM User_auth WHERE id=$val");
$ou_id = $user_info["ou_id"];
$login = NULL;
if (!empty($auth_info["dns_name"])) { $login = $auth_info["dns_name"]; }
if (empty($login) and !empty($auth_info["comments"])) { $login = transliterate($auth_info["comments"]); }
if (empty($login) and !empty($auth_info["dhcp_hostname"])) { $login = $auth_info["dhcp_hostname"]; }
if (empty($login) and !empty($auth_info["mac"])) { $login = $auth_info["mac"]; }
if (empty($login)) { $login = $auth_info["ip"]; }
$new_user = get_record_sql($db_link,"SELECT * FROM User_list WHERE LCase(login)=LCase('$login') and deleted=0");
if (!empty($new_user)) {
// move auth
$auth["user_id"] = $new_user["id"];
$auth["ou_id"] = $new_user["ou_id"];
$auth["save_traf"] = $save_traf;
update_record($db_link, "User_auth", "id='" . $val . "'", $auth);
apply_auth_rule($db_link,$val,$l_id);
LOG_WARNING($db_link,"Адрес доступа id: $val перемещён к другому юзеру user_id: ".$new_user["id"], $val);
} else {
$new["login"] = $login;
$new["ou_id"] = $ou_id;
$l_id=insert_record($db_link, "User_list", $new);
$auth["user_id"] = $l_id;
$auth["save_traf"] = $save_traf;
update_record($db_link, "User_auth", "id='" . $val . "'", $auth);
LOG_WARNING($db_link,"Создан новый пользователь из адреса доступа: login => $login. Адрес доступа auth_id: $val перемещён к созданному пользователю.", $val);
}
}
}
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
}
unset($_POST);
require_once ($_SERVER["DOCUMENT_ROOT"]."/inc/header.php");
?>