Skip to content

Commit

Permalink
disable main inherit code if there is custom rule
Browse files Browse the repository at this point in the history
  • Loading branch information
juanSTIC committed Jan 2, 2024
1 parent 8177741 commit 7e43ec0
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 162 deletions.
172 changes: 109 additions & 63 deletions custom/modules/SecurityGroups/CustomSecurityGroupsInherit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,100 +4,147 @@ class CustomSecurityGroupsInherit

public function after_save(&$bean, $event, $arguments)
{
//return;

if (in_array($bean->module_name, ['SugarFeed'])) {
return;
}
if (!$bean->fetched_row) {
global $sugar_config;
$rulesBean = self::getModuleRule($bean->module_name);

if (!empty($rulesBean)) {
include_once 'modules/SecurityGroups/SecurityGroup.php';
include_once 'SticInclude/Utils.php';

// Se aplica la herencia del usuario asignado si está activada
// Check if the inheritance of the assigned user is enabled
if ($rulesBean->inherit_assigned == 1) {
// Temporarily enable the inheritance setting
$sugar_config['securitysuite_inherit_assigned'] = true;

// Apply the inheritance logic for the assigned user
SecurityGroup::inherit_assigned($bean, false);

// Reset the inheritance setting to its original state
$sugar_config['securitysuite_inherit_assigned'] = false;
} else {
// Check if the inheritance setting is enabled
if ($sugar_config['securitysuite_inherit_assigned'] == true) {
// Log a message indicating that the inheritance rule does not apply
// due to the configuration setting
$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ': ' .
"The variable {sugar_config['securitysuite_inherit_assigned']} " .
"is set to true, indicating that the inheritance rule for the " .
"assigned user defined in the stic_Advanced_Security_Groups module " .
"does not apply");
}
}
// Se aplica la herencia del usuario creador si está activada

// Check if the inheritance of the creator is enabled
if ($rulesBean->inherit_creator == 1) {
// Temporarily enable the inheritance setting
$sugar_config['securitysuite_inherit_creator'] = true;

// Apply the inheritance logic for the creator
SecurityGroup::inherit_creator($bean, false);

// Reset the inheritance setting to its original state
$sugar_config['securitysuite_inherit_creator'] = false;
} else {
// Check if the inheritance setting is enabled
if ($sugar_config['securitysuite_inherit_creator'] == true) {
// Log a message indicating that the inheritance rule does not apply
// due to the configuration setting
$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ': ' .
"The variable {sugar_config['securitysuite_inherit_creator']} " .
"is set to true, indicating that the inheritance rule for the " .
"creator defined in the stic_Advanced_Security_Groups module " .
"does not apply");
}
}

// Se aplica la herencia del registro padre si está activada
// Check if the inheritance of the parent is enabled
if ($rulesBean->inherit_parent == 1) {
// Temporarily enable the inheritance setting
$sugar_config['securitysuite_inherit_parent'] = true;

// Apply the inheritance logic for the parent
SecurityGroup::inherit_parent($bean, false);

// Reset the inheritance setting to its original state
$sugar_config['securitysuite_inherit_parent'] = false;
} else {
// Check if the inheritance setting is enabled
if ($sugar_config['securitysuite_inherit_parent'] == true) {
// Log a message indicating that the inheritance rule does not apply
// due to the configuration setting
$GLOBALS['log']->info('Line ' . __LINE__ . ': ' . __METHOD__ . ': ' .
"The variablrsos 24/07/2023 11:20 13/07/2023 15:22
 PROPUESTAS - HE No No No e {sugar_config['securitysuite_inherit_parent']} " .
"is set to true, indicating that the inheritance rule for the " .
"parent defined in the stic_Advanced_Security_Groups module " .
"does not apply");
}
}

// Si se han establecido reglas de herencia de algún módulo relacioando la aplicamos

$finalSGtoInherit = array();
$bean->load_relationship('SecurityGroups');

for ($i = 10; $i > 0; $i--) {
$customInherit ='';
$customInherit = $rulesBean->{"inherit_from_modules_".$i};

if (!empty($customInherit) && (is_string($bean->$customInherit))) {
$finalSGtoInherit[] = $bean->$customInherit;
} else if (!empty($customInherit)){
$relatedSG='';
$customInheritBean = SticUtils::getRelatedBeanObject($bean, $customInherit);
//$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ': relatedField: '.$customInheritBean->id);
if (!empty($customInheritBean->id)){
$relatedSG = self::getRelatedSG($customInheritBean->id);
foreach ($relatedSG as $relSG){
$finalSGtoInherit[] = $relSG;
}
}
}
$finalSGtoInherit = array();
$bean->load_relationship('SecurityGroups');

for ($i = 10; $i > 0; $i--) {
$customInherit = '';
$customInherit = $rulesBean->{"inherit_from_modules_" . $i};

if (!empty($customInherit) && (is_string($bean->$customInherit))) {
$finalSGtoInherit[] = $bean->$customInherit;
} else if (!empty($customInherit)) {
$relatedSG = '';
$customInheritBean = SticUtils::getRelatedBeanObject($bean, $customInherit);
//$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ': relatedField: '.$customInheritBean->id);
if (!empty($customInheritBean->id)) {
$relatedSG = self::getRelatedSG($customInheritBean->id);
foreach ($relatedSG as $relSG) {
$finalSGtoInherit[] = $relSG;
}
}
}
}

//$exemptInherit = $rulesBean->non_inherit_from_security_groups;

$finalExemptInherit = array();
//$exemptInherit = $rulesBean->non_inherit_from_security_groups;

for ($i = 5; $i > 0; $i--) {
$exemptInherit ='';
$exemptInherit = $rulesBean->{"non_inherit_from_security_groups_".$i};
if (!empty($exemptInherit)){
$finalExemptInherit[] = $exemptInherit;
}
}
$finalExemptInherit = array();

for ($i = 5; $i > 0; $i--) {
$exemptInherit = '';
$exemptInherit = $rulesBean->{"non_inherit_from_security_groups_" . $i};
if (!empty($exemptInherit)) {
$finalExemptInherit[] = $exemptInherit;
}
}

if (!empty($finalSGtoInherit)) {
//$finalSGtoInherit = array_unique($finalSGtoInherit);
$finalSGtoInherit = array_unique($finalSGtoInherit);

if (!empty($finalExemptInherit)) {
$finalExemptInherit = array_unique($finalExemptInherit);
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ': exemptInherit: ' . $exemptInherit);
//$finalSGtoInherit.indexOf($exemptInherit);
/*foreach (array_diff($finalSGtoInherit, $finalExemptInherit) as $key) {
unset($finalSGtoInherit[$key]);
}*/
$finalSGtoInherit = array_diff($finalSGtoInherit, $finalExemptInherit);
}
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ': relatedSG: ' . print_r($finalSGtoInherit, true));

if (!empty($finalSGtoInherit)) {
//$finalSGtoInherit = array_unique($finalSGtoInherit);
$finalSGtoInherit = array_unique($finalSGtoInherit);

if (!empty($finalExemptInherit)){
$finalExemptInherit = array_unique($finalExemptInherit);
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ': exemptInherit: '.$exemptInherit);
//$finalSGtoInherit.indexOf($exemptInherit);
/*foreach (array_diff($finalSGtoInherit, $finalExemptInherit) as $key) {
unset($finalSGtoInherit[$key]);
}*/
$finalSGtoInherit= array_diff($finalSGtoInherit, $finalExemptInherit);
}
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ': relatedSG: '.print_r($finalSGtoInherit, true));

foreach ($finalSGtoInherit as $SG_ID) {
self::setCustomInherit($bean->id, $bean->module_name, $SG_ID);
}
}
foreach ($finalSGtoInherit as $SG_ID) {
self::setCustomInherit($bean->id, $bean->module_name, $SG_ID);
}
}
}
}

}

/**
* Save one securitygroup record
*
Expand All @@ -117,7 +164,7 @@ public static function setCustomInherit($recordId, $moduleName, $securityGroupId
/**
* Obtener la regla definida para el módulo
*
* @param String $moduleName
* @param String $moduleName
* @return Object
*/
public static function getModuleRule($moduleName)
Expand All @@ -132,23 +179,22 @@ public static function getModuleRule($moduleName)
public static function getRelatedSG($relatedID)
{
global $db;
$relatedIDs= array();
$relatedIDs = array();
$result = $db->query("SELECT DISTINCT securitygroup_id FROM securitygroups_records sr left join securitygroups sg on sr.securitygroup_id=sg.id WHERE sr.record_id='{$relatedID}' AND sr.deleted=0 AND sg.noninheritable=0");
foreach($result as $row){
$relatedIDs[]= $row['securitygroup_id'];
foreach ($result as $row) {
$relatedIDs[] = $row['securitygroup_id'];
}
return $relatedIDs;
}

public static function checkrelatedField($relatedID, $bean)
{
global $db;
$relID= '';
$relID = '';
$relID = $db->getOne("SELECT {$relatedID} FROM contacts_cstm WHERE id_c='{$bean->id}' LIMIT 1");
//$relID = $db->getOne("SELECT id FROM securitygroups WHERE id='{$relatedID}' AND deleted=0 LIMIT 1");

return $relID;
}

}

Loading

0 comments on commit 7e43ec0

Please sign in to comment.