public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pmg-devel] [PATCH pmg-api] fix #2071: RuleDB: ignore duplicate entries for Who objects
@ 2021-09-24 11:17 Dominik Csapak
  2021-10-06 16:33 ` [pmg-devel] applied: " Stoiko Ivanov
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Csapak @ 2021-09-24 11:17 UTC (permalink / raw)
  To: pmg-devel

if we detect an entry with a value that is identical, return that id
instead of adding it again to the db.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/PMG/RuleDB/LDAP.pm     | 10 ++++++++++
 src/PMG/RuleDB/LDAPUser.pm | 10 ++++++++++
 src/PMG/RuleDB/WhoRegex.pm | 10 ++++++++++
 src/PMG/Utils.pm           | 17 +++++++++++++++++
 4 files changed, 47 insertions(+)

diff --git a/src/PMG/RuleDB/LDAP.pm b/src/PMG/RuleDB/LDAP.pm
index e17441b..a132499 100644
--- a/src/PMG/RuleDB/LDAP.pm
+++ b/src/PMG/RuleDB/LDAP.pm
@@ -81,6 +81,16 @@ sub save {
     } else {
 	# insert
 
+	# check if it exists first
+	if (my $id = PMG::Utils::get_existing_object_id(
+	    $ruledb->{dbh},
+	    $self->{ogroup},
+	    $self->otype(),
+	    $confdata
+	)) {
+	    return $id;
+	}
+
 	my $sth = $ruledb->{dbh}->prepare(
 	    "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
 	    "VALUES (?, ?, ?);");
diff --git a/src/PMG/RuleDB/LDAPUser.pm b/src/PMG/RuleDB/LDAPUser.pm
index f550870..022d784 100644
--- a/src/PMG/RuleDB/LDAPUser.pm
+++ b/src/PMG/RuleDB/LDAPUser.pm
@@ -83,6 +83,16 @@ sub save {
     } else {
 	# insert
 
+	# check if it exists first
+	if (my $id = PMG::Utils::get_existing_object_id(
+	    $ruledb->{dbh},
+	    $self->{ogroup},
+	    $self->otype(),
+	    $confdata
+	)) {
+	    return $id;
+	}
+
 	my $sth = $ruledb->{dbh}->prepare(
 	    "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
 	    "VALUES (?, ?, ?);");
diff --git a/src/PMG/RuleDB/WhoRegex.pm b/src/PMG/RuleDB/WhoRegex.pm
index b9519ad..37ec3aa 100644
--- a/src/PMG/RuleDB/WhoRegex.pm
+++ b/src/PMG/RuleDB/WhoRegex.pm
@@ -70,6 +70,16 @@ sub save {
     } else {
 	# insert
 
+	# check if it exists first
+	if (my $id = PMG::Utils::get_existing_object_id(
+	    $ruledb->{dbh},
+	    $self->{ogroup},
+	    $self->otype(),
+	    $adr
+	)) {
+	    return $id;
+	}
+
 	my $sth = $ruledb->{dbh}->prepare (
 	    "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
 	    "VALUES (?, ?, ?);");
diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index 20686ad..92c3a7a 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -1505,4 +1505,21 @@ sub update_local_spamassassin_channels {
     return $fresh_updates
 }
 
+sub get_existing_object_id {
+    my ($dbh, $obj_id, $obj_type, $value) = @_;
+
+    my $sth = $dbh->prepare("SELECT id FROM Object WHERE ".
+	"Objectgroup_ID = ? AND ".
+	"ObjectType = ? AND ".
+	"Value = ?"
+    );
+    $sth->execute($obj_id, $obj_type, $value);
+
+    if (my $ref = $sth->fetchrow_hashref()) {
+	return $ref->{id};
+    }
+
+    return;
+}
+
 1;
-- 
2.30.2





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-10-06 16:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-24 11:17 [pmg-devel] [PATCH pmg-api] fix #2071: RuleDB: ignore duplicate entries for Who objects Dominik Csapak
2021-10-06 16:33 ` [pmg-devel] applied: " Stoiko Ivanov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal