Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 1823

Přidáno uživatelem Ondřej Fibich před téměř 12 roky(ů)

Upravy:
- fixes #347: Validatory MAC address vuci unikatnosti v subnetu

Oprava:
- oprava chybneho chovani chyb formularu (neslo vlozut odkaz do chyby)

Zobrazit rozdíly:

freenetis/branches/1.1/application/i18n/cs_CZ/texts.php
'mac address' => 'MAC adresa',
'mac address already exists' => 'MAC adresa se již nachází v databázi',
'mac address of device' => 'MAC adresa zařízení',
'mac address of this interface is already in the selected %s assigned to another interface' => 'MAC adresa tohoto rozhraní je již ve vybrané %s přiřazena jinému rozhraní',
'mac address is invalid' => 'MAC adresa je neplatná',
'mac restriction' => 'MAC restrikce',
'mac restriction file was successfully generated' => 'Soubor s MAC restrikcemi byl úspěšně vygenerován',
......
'sms message has been successfully added' => 'SMS zpráva byla úspěšně přidána.',
'sms settings' => 'SMS nastavení',
'some phones wasn\'t founded' => 'Některé telefony nebyly nalezeny',
'some mac addresses are same, please change them' => 'Některé MAC adresy jsou stejné, změňte je prosím',
'some ip addresses are same, please change them' => 'Některé IP adresy jsou stejné, změňte je prosím',
'someone from the ip address %s, probably you, requested to change your password' => 'někdo, patrně Vy, žádal z IP %s o změnu vašeho hesla',
'someone have already requested for the same connection' => 'O stejné připojení už zažádal jiný člen.',
'specific symbol' => 'Specifický symbol',
......
'this approval template already contains this approval type' => 'Tato hlasovací šablona již obsahuje tento hlasovací typ.',
'this interface is connected to multiple devices' => 'Toto rozhraní je připojeno k více zařízením.',
'this information is required' => 'Tento údaj je povinný.',
'this mac is in collision with other interfaces on the same subnet' => 'Tato MAC adresa je v kolizi s jiným rozhraním na stejné podsíti',
'this member has already set up this fee type for this interval' => 'Tento člen má již nastaven tento typ poplatku pro tento interval.',
'this member has default system member fee %d %s' => 'Tento člen má výchozí systémový členský příspěvek %d %s.',
'this message was redirected to you from your account at %s' => 'Tato zpráva byla přesměrována z vašeho účtu na %s',
freenetis/branches/1.1/application/models/subnet.php
protected $has_and_belongs_to_many = array('clouds');
/**
* Check if the MAC address is unique in the subnet.
*
* @param string $mac MAC address
* @param int $ip_address_id Iface ID - for edit purposes
* @param int $subnet_id Subnet id [Optional - deafult self ID]
* @return bool
*/
public function is_mac_unique_in_subnet($mac, $ip_address_id = NULL, $subnet_id = NULL)
{
if ($subnet_id === NULL)
{
$subnet_id = $this->id;
}
$ip_address = new Ip_address_Model($ip_address_id);
$max_count = 0;
if ($ip_address && $ip_address->id && ($ip_address->iface->mac == $mac))
{
$max_count = 1;
}
$result = $this->db->query("
SELECT COUNT(*) AS count
FROM subnets s
JOIN ip_addresses ip ON ip.subnet_id = s.id
JOIN ifaces i ON i.id = ip.iface_id
WHERE s.id = ? AND i.mac = ?
", $subnet_id, $mac);
return ($result->count() && $result->current()->count <= $max_count);
}
/**
* Gets mask and net of subnet
*
* @param integer $subnet_id
freenetis/branches/1.1/application/models/iface.php
{
return array_map('__', self::$port_modes);
}
/**
* Checks whether the given new MAC of the given iface is unique in all
* subnets that are in relation with the iface over his IP addresses.
*
* This function should not be use during adding of an iface!
*
* @param int $iface_id
* @param string $mac
* @return bool
*/
public function is_mac_unique($iface_id, $mac)
{
$iface = new Iface_Model($iface_id);
if ($iface->id)
{
if ($iface->mac == $mac)
{
return TRUE; // edit with same MAC
}
return $this->db->query("
SELECT COUNT(*) AS count
FROM subnets s
JOIN ip_addresses ip ON ip.subnet_id = s.id
JOIN ifaces i ON ip.iface_id = i.id
WHERE s.id IN (
SELECT s2.id FROM subnets s2
JOIN ip_addresses ip2 ON ip2.subnet_id = s2.id
JOIN ifaces i2 ON i2.id = ip2.iface_id
WHERE i2.id = ?
) AND i.mac = ?
", $iface_id, $mac)->current()->count <= 0;
}
else
{
return TRUE;
}
}
/**
* Get count of ifaces of device
*
freenetis/branches/1.1/application/controllers/ifaces.php
->label('MAC')
->rules('valid_mac_address')
->style('width:620px')
->value($iface->mac);
->value($iface->mac)
->callback(array($this, 'valid_unique_mac_in_subnets'));
}
$base_form->textarea('comment')
......
// additional infor for AP and reamings
$roaming_id = ORM::factory('link')->get_roaming();
if ($iface->link->id == $roaming_id || (
if (($iface->link->id == $roaming_id && $roaming_id !== NULL) || (
$iface->type == Iface_Model::TYPE_WIRELESS &&
$iface->wireless_mode == Iface_Model::WIRELESS_MODE_AP
))
......
}
/**
* Callback function to validate MAC adress in order to have this MAC
* in maximum count of one.
*
* @author Ondřej Fibich
* @param Form_Field $input
*/
public function valid_unique_mac_in_subnets($input = NULL)
{
// validators cannot be accessed
if (empty($input) || !is_object($input))
{
self::error(PAGE);
}
$iface = new Iface_Model();
if (!$iface->is_mac_unique($this->iface_id, $input->value))
{
$m = 'This MAC is in collision with other interfaces on the same subnet';
$input->add_error('required', __($m) . '!');
}
}
/**
* Callback function to validate vlan ap wmode
*
* @author Ondřej Fibich
freenetis/branches/1.1/application/controllers/ip_addresses.php
->label('Interface name')
->rules('required')
->options($arr_ifaces)
->style('width:520px');
->style('width:520px')
->callback(array($this, 'valid_mac_address_unique_in_subnet'));
$this->form->input('ip_address')
->label('IP address')
......
->options($arr_ifaces)
->selected($ip_address->iface_id)
->style('width:500px')
->add_button('ifaces', 'add', $device->id);
->callback(array($this, 'valid_mac_address_unique_in_subnet'));
$this->form->input('ip_address')
->label('IP address')
......
} // end of valid_ip
/**
* Callback function to validate if MAC address is unique in subnet
*
* @author Ondrej Fibich
* @param Form_Field $input
*/
public function valid_mac_address_unique_in_subnet($input = NULL)
{
// validators cannot be accessed
if (empty($input) || !is_object($input))
{
self::error(PAGE);
}
if ($this->input->post('subnet_id') && $input->value)
{
$subnet = new Subnet_Model($this->input->post('subnet_id'));
$iface = new Iface_Model($input->value);
if ($subnet->id && $iface->id)
{
if (!$subnet->is_mac_unique_in_subnet($iface->mac, $this->ip_address_id))
{
$link = html::anchor('/subnets/show/' . $subnet->id, __('subnet', NULL, 1), 'target="_blank"');
$m = 'MAC address of this interface is already in the '
. 'selected %s assigned to another interface';
$input->add_error('required', str_replace('"', '\'', __($m, $link)) . '!');
}
return;
}
}
$input->add_error('required', __('Subnet or iface not set.'));
}
/**
* Checks ip address if matches subnet and mask.
*
* @param string $ip
freenetis/branches/1.1/application/controllers/json.php
}
/**
* Performs action of ip_address_check jQuery frm validator.
* Performs action of mac_address_check jQuery form validator.
* Checks whether the MAC is unique on subnet.
*
* @author Ondrej Fibich
*/
public function mac_address_check()
{
$mac = trim($this->input->get('mac'));
$subnet = new Subnet_Model($this->input->get('subnet_id'));
$ip_address_id = $this->input->get('ip_address_id');
if (!$subnet || !$subnet->id)
{
die(json_encode(array
(
'state' => false,
'message' => __('Subnet not exists.')
)));
}
if ($subnet->is_mac_unique_in_subnet($mac, $ip_address_id))
{
die(json_encode(array('state' => true)));
}
else
{
$link = html::anchor(
'/subnets/show/' . $subnet->id,
__('subnet', NULL, 1), 'target="_blank"'
);
$m = 'MAC address of this interface is already in the '
. 'selected %s assigned to another interface';
die(json_encode(array
(
'state' => false,
'message' => __($m, $link)
)));
}
}
/**
* Performs action of ip_address_check jQuery form validator.
*
* @author Ondrej Fibich
*/
public function ip_address_check()
{
$edit_id = intval($this->input->get('ip_address_id'));
freenetis/branches/1.1/application/views/forge_template.php
foreach ($input->error_messages() as $error):
?>
<p class="error"><?php echo str_replace('*','',str_replace(':','',$error)) ?></p>
<p class="error"><?php echo (text::ends_with($error, ':') ? mb_substr($error, 0, mb_strlen($error)) : $error) ?></p>
<?php
endforeach;
freenetis/branches/1.1/application/views/js/devices_add.php
}
<?php endif; ?>
html_buffer.push('class="mac_address" style="width: 12em" />');
html_buffer.push('class="mac_address mac_address_check" style="width: 12em" />');
}
else if (data['type'] == <?php echo Iface_Model::TYPE_PORT ?>)
{
......
{
if ($(this).validate().form())
{
var isValid = true;
// check if all IP addresses are unique
var ips = [];
$.each($('input[name^="ip["]'), function (i, v)
{
var ip = trim($(v).val());
if (ip.length)
{
if (ips.indexOf(ip) == -1)
{
ips.push(trim($(v).val()));
}
else
{
alert('<?php echo __('Some IP addresses are same, please change them') ?>!');
isValid = false;
}
}
});
// check if all MAC addresses are unique
var macs = [];
$.each($('input[name^="mac["]'), function (i, v)
{
var mac = trim($(v).val());
if (mac.length)
{
if (macs.indexOf(mac) == -1)
{
macs.push(trim($(v).val()));
}
else
{
alert('<?php echo __('Some MAC addresses are same, please change them') ?>!');
isValid = false;
}
}
});
// send form
return true;
return isValid;
}
return false;
freenetis/branches/1.1/application/views/js/base.php
$.validator.addMethod('mac_address_check', function(value, element)
{
var ret = false;
var ret = false,
subnet_id = null,
ip_id = null;
$.ajax({
url: '<?php echo url_lang::base() ?>json/mac_address_check',
async: false,
dataType: 'json',
data: {mac_address: value},
success: function(result)
{
if(result.state)
// for devices/add
var subnet_dropdown_name = $(element).attr('name').replace('mac', 'subnet');
var $subnet_dropdown = $('select[name="' + subnet_dropdown_name + '"]');
if ($subnet_dropdown.length)
{
subnet_id = $subnet_dropdown.val();
}
if (subnet_id && value && value.length)
{
$.ajax({
url: '<?php echo url_lang::base() ?>json/mac_address_check',
async: false,
dataType: 'json',
data: {mac: value, subnet_id: subnet_id, ip_address_id: ip_id},
success: function(result)
{
ret = true;
if(result.state)
{
ret = true;
}
else
{
$.validator.messages.mac_address_check = result.message;
}
}
else
{
$.validator.messages.mac_address = result.message;
}
}
});
return ret;
});
return ret;
}
else
{ // if subnet is not set -> mac is always correct
return true;
}
}, '<?php echo __('MAC address already exists.') ?>');
$.validator.addMethod('to_field', function(value)
freenetis/branches/1.1/system/libraries/Controller.php
* @author Kohana Team
* @copyright (c) 2007-2008 Kohana Team
* @license http://kohanaphp.com/license.html
*
* @property Loader $load
* @property URI $uri
* @property Input $input
*/
class Controller_Core {

Také k dispozici: Unified diff