Private GIT

Skip to content
Snippets Groups Projects
Unverified Commit e92b3345 authored by Miha Petkovsek's avatar Miha Petkovsek Committed by GitHub
Browse files

Merge pull request #1670 from GaryAllan/delegate_permissions

Edit Sections: delegate permissions speedup
parents 51226204 01ffff19
No related branches found
No related tags found
No related merge requests found
......@@ -747,10 +747,10 @@ class Logging extends Common_functions {
// check if syslog globally enabled and write log
if($this->settings->enableChangelog==1) {
# get user details and initialize required objects
$this->Addresses = new Addresses ($this->Database);
$this->Subnets = new Subnets ($this->Database);
$this->Sections = new Sections ($this->Database);
$this->Tools = new Tools ($this->Database);
if (!is_object($this->Addresses)) $this->Addresses = new Addresses ($this->Database);
if (!is_object($this->Subnets)) $this->Subnets = new Subnets ($this->Database);
if (!is_object($this->Sections)) $this->Sections = new Sections ($this->Database);
if (!is_object($this->Tools)) $this->Tools = new Tools ($this->Database);
# unset unneeded values and format
$this->changelog_unset_unneeded_values ();
......@@ -1618,8 +1618,8 @@ class Logging extends Common_functions {
public function fetch_subnet_addresses_changelog_recursive ($subnetId, $limit = 50) {
# get all addresses ids
$ips = array();
$Addresses = new Addresses ($this->Database);
$ips = $Addresses->fetch_subnet_addresses_recursive ($subnetId, false);
if (!is_object($this->Addresses)) $this->Addresses = new Addresses ($this->Database);
$ips = $this->Addresses->fetch_subnet_addresses_recursive ($subnetId, false);
# fetch changelog for IPs
if(sizeof($ips) > 0) {
......@@ -1717,22 +1717,22 @@ class Logging extends Common_functions {
if(!is_numeric($subnetId)) { $this->Result->show("danger", "Invalid subnet Id", true); return false; }
# fetch all slave subnet ids
$Subnets = new Subnets ($this->Database);
$Subnets->reset_subnet_slaves_recursive ();
$Subnets->fetch_subnet_slaves_recursive ($subnetId);
if (!is_object($this->Subnets)) $this->Subnets = new Subnets ($this->Database);
$this->Subnets->reset_subnet_slaves_recursive ();
$this->Subnets->fetch_subnet_slaves_recursive ($subnetId);
# remove master subnet ID
$key = array_search($subnetId, $Subnets->slaves);
unset($Subnets->slaves[$key]);
$Subnets->slaves = array_unique($Subnets->slaves);
$key = array_search($subnetId, $this->Subnets->slaves);
unset($this->Subnets->slaves[$key]);
$this->Subnets->slaves = array_unique($this->Subnets->slaves);
# if some slaves are present get changelog
if(sizeof($Subnets->slaves) > 0) {
if(sizeof($this->Subnets->slaves) > 0) {
# set query
$query = "select
`u`.`real_name`,`o`.`sectionId`,`o`.`subnet`,`o`.`mask`,`o`.`isFolder`,`o`.`description`,`o`.`id`,`c`.`caction`,`c`.`cresult`,`c`.`cdate`,`c`.`cdiff` from `changelog` as `c`, `users` as `u`, `subnets` as `o`
where `c`.`cuser` = `u`.`id` and `c`.`coid`=`o`.`id`
and (";
foreach($Subnets->slaves as $slaveId) {
foreach($this->Subnets->slaves as $slaveId) {
if(!isset($args)) $args = array();
$query .= "`c`.`coid` = ? or ";
$args[] = $slaveId; //set keys
......@@ -1778,7 +1778,7 @@ class Logging extends Common_functions {
public function changelog_send_mail ($changelog) {
# initialize tools class
$this->Tools = new Tools ($this->Database);
if (!is_object($this->Tools)) $this->Tools = new Tools ($this->Database);
# set object
$obj_details = $this->object_action == "add" ? $this->object_new : $this->object_old;
......
......@@ -827,6 +827,36 @@ abstract class DB {
//execute
return $this->runQuery('TRUNCATE TABLE `'.$tableName.'`;');
}
/**
* Begin SQL Transaction
*
* @access public
* @return bool
*/
public function beginTransaction() {
return $this->pdo->beginTransaction();
}
/**
* Commit SQL Transaction
*
* @access public
* @return bool
*/
public function commit() {
return $this->pdo->commit();
}
/**
* Commit SQL Transaction
*
* @access public
* @return bool
*/
public function rollBack() {
return $this->pdo->rollBack();
}
}
......
......@@ -556,45 +556,51 @@ class Sections extends Common_functions {
public function delegate_section_permissions ($sectionId, $removed_permissions, $changed_permissions) {
// init subnets class
$Subnets = new Subnets ($this->Database);
// fetch section subnets
$section_subnets = $this->fetch_multiple_objects ("subnets", "sectionId", $sectionId);
// fetch section subnets (use $subnets object to prime its cache)
$section_subnets = $Subnets->fetch_multiple_objects ("subnets", "sectionId", $sectionId);
if (!is_array($section_subnets)) $section_subnets = array();
try {
// Begin Transaction
$this->Database->beginTransaction();
// loop
if ($section_subnets!==false) {
foreach ($section_subnets as $s) {
// to array
$s_old_perm = json_decode($s->permissions, true);
// removed
if (sizeof($removed_permissions)>0) {
foreach ($removed_permissions as $k=>$p) {
unset($s_old_perm[$k]);
}
if (is_array($removed_permissions)) {
foreach ($removed_permissions as $k=>$p) unset($s_old_perm[$k]);
}
// added
if (sizeof($changed_permissions)>0) {
foreach ($changed_permissions as $k=>$p) {
$s_old_perm[$k] = $p;
}
if (is_array($changed_permissions)) {
foreach ($changed_permissions as $k=>$p) $s_old_perm[$k] = $p;
}
// set values
$values = array(
"id" => $s->id,
"permissions" => json_encode($s_old_perm)
);
$values = array("id" => $s->id, "permissions" => json_encode($s_old_perm));
// update
if($Subnets->modify_subnet ("edit", $values)===false) { $this->Result->show("danger", _("Failed to set subnet permissons for subnet")." $s->name!", true); }
if($Subnets->modify_subnet ("edit", $values)===false) {
$this->Database->rollBack();
if (!$s->isFolder) {
$name = $this->transform_to_dotted($s->subnet) . '/' . $s->mask . ' ('.$s->description.')';
} else {
$name = $s->description;
}
$this->Result->show("danger", _("Failed to set subnet permissons for subnet")." $name!", true);
return false;
}
// ok
$this->Result->show("success", _("Subnet permissions recursively set")."!", true);
}
try { $this->Database->updateObject("subnets", array("permissions"=>$permissions, "sectionId"=>$sectionId), "sectionId"); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
} catch (Exception $e) {
$this->Database->rollBack();
$this->Result->show("danger", _("Error: ").$e->getMessage(), true);
return false;
}
// ok
$this->Database->commit();
$this->Result->show("success", _("Subnet permissions recursively set")."!");
return true;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment