]> git.proxmox.com Git - pmg-api.git/commitdiff
fix #2071: RuleDB: ignore duplicate entries for Who objects
authorDominik Csapak <d.csapak@proxmox.com>
Fri, 24 Sep 2021 11:17:46 +0000 (13:17 +0200)
committerStoiko Ivanov <s.ivanov@proxmox.com>
Wed, 6 Oct 2021 16:22:08 +0000 (18:22 +0200)
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
src/PMG/RuleDB/LDAPUser.pm
src/PMG/RuleDB/WhoRegex.pm
src/PMG/Utils.pm

index e17441be3f79daf6232642e5ffa2c150afc781be..a1324997b029839c1c697a2b613c5e7e00a948b9 100644 (file)
@@ -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 (?, ?, ?);");
index f5508702db50e298d21857cf28182ad04c667e1f..022d7841b836bcade1ad119c45a59a29e2b4f357 100644 (file)
@@ -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 (?, ?, ?);");
index b9519adfe033513cfabcb005e6ce5a611a8798ed..37ec3aa857bf1807a5e539d8b76baca3cf684855 100644 (file)
@@ -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 (?, ?, ?);");
index 20686ad87bc6b1e34369b27505fd50ee24442921..92c3a7aad96d2b2b4bfda7b55a077c7dfccbd83d 100644 (file)
@@ -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;