diff --git a/app/devices/device_edit.php b/app/devices/device_edit.php index 46767ccf0a..b3f6fedccf 100644 --- a/app/devices/device_edit.php +++ b/app/devices/device_edit.php @@ -40,28 +40,60 @@ require_once "resources/require.php"; $language = new text; $text = $language->get(); -//check duplicate mac address - if ($_GET["mac"] != '' && $_GET["mac"] != "000000000000") { - $sql = "select "; - $sql .= "d2.domain_name "; - $sql .= "from "; - $sql .= "v_devices as d1, "; - $sql .= "v_domains as d2 "; - $sql .= "where "; - $sql .= "d1.domain_uuid = d2.domain_uuid and "; - $sql .= "d1.device_mac_address = '".check_str($_GET["mac"])."' "; - if ($_GET["id"] != '') { - $sql .= " and d1.device_uuid <> '".check_str($_GET["id"])."' "; - } - $prep_statement = $db->prepare($sql); - if ($prep_statement) { - $prep_statement->execute(); - $row = $prep_statement->fetch(PDO::FETCH_ASSOC); - if ($row['domain_name'] != '') { - echo $text['message-duplicate'].((if_group("superadmin") && $_SESSION["domain_name"] != $row["domain_name"]) ? ": ".$row["domain_name"] : null); +//check for duplicates + if ($_GET["check"] == 'duplicate') { + //mac address + if ($_GET["mac"] != '' && $_GET["mac"] != "000000000000") { + $sql = "select "; + $sql .= "d2.domain_name "; + $sql .= "from "; + $sql .= "v_devices as d1, "; + $sql .= "v_domains as d2 "; + $sql .= "where "; + $sql .= "d1.domain_uuid = d2.domain_uuid and "; + $sql .= "d1.device_mac_address = '".check_str($_GET["mac"])."' "; + if ($_GET["device_uuid"] != '') { + $sql .= " and d1.device_uuid <> '".check_str($_GET["device_uuid"])."' "; + } + $prep_statement = $db->prepare($sql); + if ($prep_statement) { + $prep_statement->execute(); + $row = $prep_statement->fetch(PDO::FETCH_ASSOC); + if ($row['domain_name'] != '') { + echo $text['message-duplicate'].((if_group("superadmin") && $_SESSION["domain_name"] != $row["domain_name"]) ? ": ".$row["domain_name"] : null); + } + } + unset($prep_statement); } - } - unset($prep_statement); + + //username + if ($_GET['username'] != '') { + $sql = "select "; + $sql .= "d2.domain_name, "; + $sql .= "d1.device_mac_address "; + $sql .= "from "; + $sql .= "v_devices as d1, "; + $sql .= "v_domains as d2 "; + $sql .= "where "; + $sql .= "d1.domain_uuid = d2.domain_uuid and "; + $sql .= "d1.device_username = '".check_str($_GET["username"])."' "; + if ($_GET['domain_uuid'] != '') { + $sql .= "and d2.domain_uuid = '".check_str($_GET['domain_uuid'])."' "; + } + if ($_GET['device_uuid'] != '') { + $sql .= "and d1.device_uuid <> '".check_str($_GET["device_uuid"])."' "; + } + $prep_statement = $db->prepare($sql); + if ($prep_statement) { + $prep_statement->execute(); + $row = $prep_statement->fetch(PDO::FETCH_ASSOC); + if ($row['domain_name'] != '') { + echo $text['message-duplicate_username'].((if_group("superadmin")) ? ": ".format_mac($row['device_mac_address']).(($_SESSION["domain_name"] != $row["domain_name"]) ? " (".$row["domain_name"].")" : null) : null); + } + } + unset($prep_statement); + } + exit; } @@ -267,14 +299,14 @@ require_once "resources/require.php"; if ($action == "add") { //save the message to a session variable $_SESSION['message'] = $text['message-add']; - //redirect the browser - header("Location: device_edit.php?id=$device_uuid"); - exit; } if ($action == "update") { //save the message to a session variable $_SESSION['message'] = $text['message-update']; } + //redirect the browser + header("Location: device_edit.php?id=$device_uuid"); + exit; } } @@ -409,28 +441,54 @@ require_once "resources/require.php"; obj[0].parentNode.removeChild(obj[2]); } - function check_mac_duplicate(mac_addr, device_uuid_to_ignore) { - if (mac_addr != '') { - check_url = "device_edit.php?mac="+mac_addr+"&id="+device_uuid_to_ignore; - $("#duplicate_mac_response").load(check_url, function() { - if ($("#duplicate_mac_response").html() != '') { - $('#device_mac_address').addClass('formfld_highlight_bad'); - display_message($("#duplicate_mac_response").html(), 'negative'); - } - else { - $('#device_mac_address').removeClass('formfld_highlight_bad'); - document.getElementById('frm').submit(); - } - }); - } - else { - $('#frm').submit(); - } + function check_duplicates() { + //check mac + var mac_addr = document.getElementById('device_mac_address').value; + $("#duplicate_mac_response").load("device_edit.php?check=duplicate&mac="+mac_addr+"&device_uuid=", function() { + var duplicate_mac = false; + + if ($("#duplicate_mac_response").html() != '') { + $('#device_mac_address').addClass('formfld_highlight_bad'); + display_message($("#duplicate_mac_response").html(), 'negative'); + duplicate_mac = true; + } + else { + $("#duplicate_mac_response").html(''); + $('#device_mac_address').removeClass('formfld_highlight_bad'); + duplicate_mac = false; + } + + //check username + if (duplicate_mac == false) { + var username = document.getElementById('device_username').value; + var domain_uuid = document.getElementById('domain_uuid').value; + $("#duplicate_username_response").load("device_edit.php?check=duplicate&username="+username+"&domain_uuid="+domain_uuid+"&device_uuid=", function() { + var duplicate_username = false; + + if ($("#duplicate_username_response").html() != '') { + $('#device_username').addClass('formfld_highlight_bad'); + display_message($("#duplicate_username_response").html(), 'negative'); + duplicate_username = true; + } + else { + $("#duplicate_username_response").html(''); + $('#device_username').removeClass('formfld_highlight_bad'); + duplicate_username = false; + } + + if (duplicate_username == false) { + document.getElementById('frm').submit(); + return false; + } + }); + } + }); + return false; } \n"; + echo "