public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api] fix #2071: RuleDB: ignore duplicate entries for Who objects
Date: Fri, 24 Sep 2021 13:17:46 +0200	[thread overview]
Message-ID: <20210924111746.3937863-1-d.csapak@proxmox.com> (raw)

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





             reply	other threads:[~2021-09-24 11:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-24 11:17 Dominik Csapak [this message]
2021-10-06 16:33 ` [pmg-devel] applied: " Stoiko Ivanov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210924111746.3937863-1-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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