]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/RuleDB.pm
add pmg report api call for the gui
[pmg-api.git] / PMG / RuleDB.pm
index f785941ed3c51a774e9b8950d28208b655c383e2..542fd4a21f8164f552d6e2149b42a4a85c108194 100644 (file)
@@ -23,9 +23,8 @@ use PMG::RuleDB::IPAddress;
 use PMG::RuleDB::IPNet;
 use PMG::RuleDB::Domain;
 use PMG::RuleDB::ReceiverDomain;
-# fixme:
-#use Proxmox::RuleDB::LDAP;
-#use Proxmox::RuleDB::LDAPUser;
+use PMG::RuleDB::LDAP;
+use PMG::RuleDB::LDAPUser;
 use PMG::RuleDB::TimeFrame;
 use PMG::RuleDB::Spam;
 use PMG::RuleDB::ReportSpam;
@@ -116,7 +115,7 @@ sub load_groups {
 
     $sth->execute($rule->{id});
 
-    my ($from, $to, $when, $what, $action) = ((), (), (), (), ());
+    my ($from, $to, $when, $what, $action) = ([], [], [], [], []);
 
     while (my $ref = $sth->fetchrow_hashref()) {
        my $og = PMG::RuleDB::Group->new($ref->{name}, $ref->{info});
@@ -144,6 +143,21 @@ sub load_groups {
     return ($from, $to, $when, $what, $action);
 }
 
+sub load_groups_by_name {
+    my ($self, $rule) = @_;
+
+    my ($from, $to, $when, $what, $action) =
+       $self->load_groups($rule);
+
+    return {
+       from => $from,
+       to => $to,
+       when => $when,
+       what => $what,
+       action => $action,
+    };
+}
+
 sub save_group {
     my ($self, $og) = @_;
 
@@ -301,13 +315,12 @@ sub get_object {
     elsif ($otype == PMG::RuleDB::IPNet::otype) {
        $obj = PMG::RuleDB::IPNet->new();
     }
-# fixme
-#    elsif ($otype == Proxmox::RuleDB::LDAP::otype) {
-#      $obj = Proxmox::RuleDB::LDAP->new();
-#    }
-#    elsif ($otype == Proxmox::RuleDB::LDAPUser::otype) {
-#      $obj = Proxmox::RuleDB::LDAPUser->new();
-#    }
+    elsif ($otype == PMG::RuleDB::LDAP::otype) {
+       $obj = PMG::RuleDB::LDAP->new();
+    }
+    elsif ($otype == PMG::RuleDB::LDAPUser::otype) {
+       $obj = PMG::RuleDB::LDAPUser->new();
+    }
     # WHEN OBJECTS
     elsif ($otype == PMG::RuleDB::TimeFrame::otype) {
        $obj = PMG::RuleDB::TimeFrame->new();
@@ -646,8 +659,19 @@ sub delete_testrules {
     return 1;
 }
 
+my $grouptype_hash = {
+    from => 0,
+    to => 1,
+    when => 2,
+    what => 3,
+    action => 4,
+};
+
 sub rule_add_group {
-    my ($self, $ruleid, $groupid, $gtype) = @_;
+    my ($self, $ruleid, $groupid, $gtype_str) = @_;
+
+    my $gtype = $grouptype_hash->{$gtype_str} //
+       die "unknown group type '$gtype_str'\n";
 
     defined($ruleid) || die "undefined rule id: ERROR";
     defined($groupid) || die "undefined group id: ERROR";
@@ -663,35 +687,38 @@ sub rule_add_group {
 sub rule_add_from_group {
     my ($self, $rule, $group) = @_;
 
-    $self->rule_add_group($rule->{id}, $group->{id}, 0);
+    $self->rule_add_group($rule->{id}, $group->{id}, 'from');
 }
 
 sub rule_add_to_group {
     my ($self, $rule, $group) = @_;
 
-    $self->rule_add_group($rule->{id}, $group->{id}, 1);
+    $self->rule_add_group($rule->{id}, $group->{id}, 'to');
 }
 
 sub rule_add_when_group {
     my ($self, $rule, $group) = @_;
 
-    $self->rule_add_group($rule->{id}, $group->{id}, 2);
+    $self->rule_add_group($rule->{id}, $group->{id}, 'when');
 }
 
 sub rule_add_what_group {
     my ($self, $rule, $group) = @_;
 
-    $self->rule_add_group($rule->{id}, $group->{id}, 3);
+    $self->rule_add_group($rule->{id}, $group->{id}, 'what');
 }
 
 sub rule_add_action {
     my ($self, $rule, $group) = @_;
 
-    $self->rule_add_group($rule->{id}, $group->{id}, 4);
+    $self->rule_add_group($rule->{id}, $group->{id}, 'action');
 }
 
 sub rule_remove_group {
-    my ($self, $ruleid, $groupid, $gtype) = @_;
+    my ($self, $ruleid, $groupid, $gtype_str) = @_;
+
+    my $gtype = $grouptype_hash->{$gtype_str} //
+       die "unknown group type '$gtype_str'\n";
 
     defined($ruleid) || die "undefined rule id: ERROR";
     defined($groupid) || die "undefined group id: ERROR";
@@ -806,7 +833,7 @@ Add an object group to the rule.
 
 Possible values for $gtype are:
 
-    0 = FROM, 1 = TO, 2 = WHEN, 3 = WHAT, 4 = ACTION
+    'from' 'to', 'when', 'what', 'action'
 
 =head3  $ruledb->rule_remove_group ($rule, $og, $gtype)