fix: [sharingGroup] Added missing migrated function to update sgOrgs and sgServers

3.x-ui-sharinggroups
Sami Mokaddem 2023-08-03 11:12:54 +02:00
parent 71987b5327
commit f07209468e
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
2 changed files with 120 additions and 0 deletions

View File

@ -26,4 +26,59 @@ class SharingGroupOrgsTable extends AppTable
]
);
}
public function updateOrgsForSG($id, $new_orgs, $old_orgs, $user)
{
// Loop through all of the organisations we want to add.
foreach ($new_orgs as $org) {
$SgO = array(
'sharing_group_id' => $id,
'org_id' => $org['id'],
'extend' => $org['extend']
);
$found = false;
// If there is a match between a new org and an old org, keep the org in $found and unset it in the old org array.
foreach ($old_orgs as $k => $old_org) {
if ($old_org['org_id'] == $org['id']) {
$found = $old_orgs[$k];
unset($old_orgs[$k]);
break;
}
}
// If we have not found the org previously, create a new sharing group org object.
// Otherwise, if we have found it check whether the extended field has been altered, if not just continue without saving
if (!$found) {
$isChange = false;
} else {
if ($found['extend'] == $SgO['extend']) {
continue;
}
$SgO['id'] = $found['id'];
$isChange = true;
}
if ($isChange) {
$SgOEntity = $this->get($SgO['id']);
} else {
$SgOEntity = $this->newEmptyEntity();
}
$SgOEntity = $this->patchEntity($SgOEntity, $SgO);
if ($this->save($SgOEntity)) {
if ($isChange) {
// TODO: [3.x-MIGRATION] Make sure these log entries are picked up correctly by the auditlog
// $this->loadLog()->createLogEntry($user, 'edit', 'SharingGroupOrg', $this->id, 'Sharing group (' . $id . '): Modified right to alter sharing group for organisation (' . $org['id'] . ').', ($org['extend'] ? 'Organisation (' . $org['id'] . ') can now extend the sharing group.' : 'Organisation (' . $org['id'] . ') can no longer extend the sharing group.'));
} else {
// $this->loadLog()->createLogEntry($user, 'add', 'SharingGroupOrg', $this->id, 'Sharing group (' . $id . '): Added organisation (' . $org['id'] . ').', 'Organisation (' . $org['id'] . ') added to Sharing group.' . ($org['extend'] ? ' Organisation (' . $org['id'] . ') can extend the sharing group.' : ''));
}
}
}
// We are left with some "old orgs" that are not in the new list. This means that they can be safely deleted.
foreach ($old_orgs as $old_org) {
$SgOEntity = $this->get($old_org['id']);
if ($this->delete($SgOEntity)) {
// $this->loadLog()->createLogEntry($user, 'delete', 'SharingGroupOrg', $old_org['id'], 'Sharing group (' . $id . '): Removed organisation (' . $old_org['id'] . ').', 'Organisation (' . $org['id'] . ') removed from Sharing group.');
}
}
}
}

View File

@ -26,4 +26,69 @@ class SharingGroupServersTable extends AppTable
]
);
}
public function updateServersForSG($id, $new_servers, $old_servers, $roaming, $user)
{
// Check first if we need to handle the servers at all, or if we should just delete all servers from the SG (depending on the checkbox in the "MISP instances" tab).
if (!$roaming) {
foreach ($new_servers as $server) {
$SgS = array(
'sharing_group_id' => $id,
'server_id' => $server['id'],
'all_orgs' => $server['all_orgs']
);
$server_name = 'server (' . $server['id'] . ')';
if ($server['id'] == 0) {
$server_name = 'the local server';
}
$found = false;
// If there is a match between a new server and an old server, keep the server in $found and unset it in the old server array.
foreach ($old_servers as $k => $old_server) {
if ($old_server['server_id'] == $server['id']) {
$found = $old_servers[$k];
unset($old_servers[$k]);
break;
}
}
// If we have not found the server previously, create a new sharing group server object.
// Otherwise, if we have found it check whether the extended field has been altered, if not just continue without saving
if (!$found) {
$isChange = false;
} else {
if ($found['all_orgs'] == $SgS['all_orgs']) {
continue;
}
$isChange = true;
$SgS['id'] = $found['id'];
}
if ($isChange) {
$SgSEntity = $this->get($SgS['id']);
} else {
$SgSEntity = $this->newEmptyEntity();
}
$SgSEntity = $this->patchEntity($SgSEntity, $SgS);
if ($this->save($SgSEntity)) {
if ($isChange) {
// TODO: [3.x-MIGRATION] Make sure these log entries are picked up correctly by the auditlog
// $this->loadLog()->createLogEntry($user, 'edit', 'SharingGroupServer', $this->id, 'Sharing group (' . $id . '): Modified access rights for users on ' . $server_name . '.', ($server['all_orgs'] ? 'All organisations on server ' . $server['id'] . ' are now part of the sharing group.' : 'Organisations on ' . $server_name . ' are now not part of the sharing group unless they are present in the list of organisations.'));
} else {
// $this->loadLog()->createLogEntry($user, 'add', 'SharingGroupServer', $this->id, 'Sharing group (' . $id . '): Added server (' . $server['id'] . ').', ucfirst($server_name) . ' added to Sharing group.' . ($server['all_orgs'] ? ' Sharing group visible to all organisations on the server.' : ''));
}
}
}
// We are left with some "old orgs" that are not in the new list. This means that they can be safely deleted.
foreach ($old_servers as $old_server) {
$SgSEntity = $this->get($old_server['id']);
if ($this->SharingGroup->SharingGroupServer->delete($SgSEntity)) {
// $this->loadLog()->createLogEntry($user, 'delete', 'SharingGroupServer', $old_server['id'], 'Sharing group (' . $id . '): Removed server(' . $old_server['server_id'] . ').', 'Server (' . $old_server['server_id'] . ') removed from Sharing group.');
}
}
} else {
$this->deleteAll(array('sharing_group_id' => $id), false);
}
}
}