check for duplicate host name
This commit is contained in:
parent
2d3f584bb4
commit
bb9a1b1719
|
@ -7,7 +7,7 @@ $Id$
|
|||
|
||||
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
|
||||
Copyright (C) 2008 Thomas Manninger
|
||||
2008 - 2017 Roland Gruber
|
||||
2008 - 2018 Roland Gruber
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -61,6 +61,9 @@ class fixed_ip extends baseModule {
|
|||
/** cached host entries (list of array('cn' => ..., 'iphostnumber' => ..., 'macaddress' => ...)) */
|
||||
private $hostCache = null;
|
||||
|
||||
/** cache for existing host entries */
|
||||
private $existingDhcpHostsCache = null;
|
||||
|
||||
/**
|
||||
* Returns true if this module can manage accounts of the current type, otherwise false.
|
||||
*
|
||||
|
@ -287,7 +290,9 @@ class fixed_ip extends baseModule {
|
|||
*/
|
||||
public function process_attributes() {
|
||||
$errors = array();
|
||||
if (!$this->isRootNode()) {
|
||||
if ($this->isRootNode()) {
|
||||
return $errors;
|
||||
}
|
||||
$this->processed = true;
|
||||
|
||||
$this->reset_overlapd_ip();
|
||||
|
@ -350,7 +355,7 @@ class fixed_ip extends baseModule {
|
|||
if (!empty($_POST['pc_'.$id])) $_POST['pc_'.$id] = trim($_POST['pc_'.$id]);
|
||||
if (!empty($_POST['pc_'.$id])) {
|
||||
|
||||
// Already use?
|
||||
// name already in use
|
||||
if (in_array($_POST['pc_'.$id], $pcs) ) {
|
||||
$error = true;
|
||||
}
|
||||
|
@ -391,7 +396,6 @@ class fixed_ip extends baseModule {
|
|||
);
|
||||
$this->orderByIP();
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
@ -407,7 +411,6 @@ class fixed_ip extends baseModule {
|
|||
$return->addElement(new htmlStatusMessage('ERROR', _("Please fill out the DHCP settings first.")), true);
|
||||
return $return;
|
||||
}
|
||||
else {
|
||||
$this->initCache();
|
||||
// auto-completion for host names
|
||||
$autoNames = array();
|
||||
|
@ -452,6 +455,7 @@ class fixed_ip extends baseModule {
|
|||
foreach($this->fixed_ip AS $id=>$arr) {
|
||||
// pc name
|
||||
$pcError = "";
|
||||
$existsInDifferentDn = $this->hostNameExists($_POST['pc_'.$id]);
|
||||
if (!$this->processed) {
|
||||
$pcError = "";
|
||||
}
|
||||
|
@ -467,6 +471,9 @@ class fixed_ip extends baseModule {
|
|||
elseif (isset($_POST['pc_'.$id]) && !preg_match("/^[A-Za-z0-9\\._-]*$/", $_POST['pc_'.$id])) {
|
||||
$pcError = _("The PC name may only contain A-Z, a-z and 0-9.");
|
||||
}
|
||||
elseif ($existsInDifferentDn !== false) {
|
||||
$pcError = sprintf(_('This PC name already exists in %s. Use e.g. %s.'), $existsInDifferentDn[0], $existsInDifferentDn[1]);
|
||||
}
|
||||
$pcs[] = $this->fixed_ip[$id]['cn'];
|
||||
|
||||
// MAC address
|
||||
|
@ -548,10 +555,58 @@ class fixed_ip extends baseModule {
|
|||
$addHostButton->setIconClass('createButton');
|
||||
$return->addElement($addHostButton , true);
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given host name already exists.
|
||||
*
|
||||
* @param string $name host name
|
||||
* @return boolean|array false if not exists, DN + new name suggestion if exists
|
||||
*/
|
||||
private function hostNameExists($name) {
|
||||
$this->fillExistingDhcpHosts();
|
||||
foreach ($this->existingDhcpHostsCache as &$host) {
|
||||
if ($name === $host['name']) {
|
||||
$dn = $host['dn'];
|
||||
if ($this->getAccountContainer()->isNewAccount || strpos($dn, $this->getAccountContainer()->dn_orig) === false) {
|
||||
return array($dn, $this->getHostNameSuggestion($name));
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a suggestion for a free host name.
|
||||
*
|
||||
* @param string $name host name
|
||||
* @return string suggested new name
|
||||
*/
|
||||
private function getHostNameSuggestion($name) {
|
||||
$matches = array();
|
||||
$number = 0;
|
||||
$namePrefix = $name;
|
||||
if (preg_match('/(.*[^0-9])([0-9]+)/', $name, $matches)) {
|
||||
$namePrefix = $matches[1];
|
||||
$number = $matches[2];
|
||||
}
|
||||
while (true) {
|
||||
$number++;
|
||||
$newName = $namePrefix . $number;
|
||||
$found = false;
|
||||
foreach ($this->existingDhcpHostsCache as &$host) {
|
||||
if ($host['name'] === $newName) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
return $newName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the HTML meta data for the add host page.
|
||||
*
|
||||
|
@ -869,6 +924,23 @@ class fixed_ip extends baseModule {
|
|||
return $this->getAccountContainer()->dn_orig == $rootSuffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills the list of existing DHCP hosts.
|
||||
*/
|
||||
private function fillExistingDhcpHosts() {
|
||||
if ($this->existingDhcpHostsCache != null) {
|
||||
return $this->existingDhcpHostsCache;
|
||||
}
|
||||
$entries = searchLDAPByAttribute('cn', '*', 'dhcpHost', array('cn'), array('dhcp'));
|
||||
$this->existingDhcpHostsCache = array();
|
||||
foreach ($entries as $entry) {
|
||||
$this->existingDhcpHostsCache[] = array(
|
||||
'dn' => $entry['dn'],
|
||||
'name' => $entry['cn'][0]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
Loading…
Reference in New Issue