]>
git.proxmox.com Git - pmg-api.git/blob - PMG/API2/RuleDB.pm
1 package PMG
::API2
::RuleDB
;
8 use PVE
::JSONSchema
qw(get_standard_option);
9 use PMG
::RESTEnvironment
;
11 use PVE
::Tools
qw(extract_param);
17 use PMG
::API2
::ObjectGroupHelpers
;
21 use PMG
::API2
::Action
;
24 use base
qw(PVE::RESTHandler);
26 __PACKAGE__-
>register_method ({
30 description
=> "Directory index.",
32 additionalProperties
=> 0,
35 permissions
=> { check
=> [ 'admin', 'audit' ] },
42 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
59 __PACKAGE__-
>register_method ({
60 name
=> 'reset_ruledb',
63 description
=> "Reset PMG rule database back to factory defaults.",
66 additionalProperties
=> 0,
69 returns
=> { type
=> 'null'},
73 my $dbh = PMG
::DBTools
::open_ruledb
("Proxmox_ruledb");
74 my $ruledb = PMG
::RuleDB-
>new($dbh);
75 PMG
::DBTools
::init_ruledb
($ruledb, 1);
80 __PACKAGE__-
>register_method({
81 name
=> 'ruledb_digest',
84 description
=> "Returns the rule database digest. This is used internally for cluster synchronization.",
85 # always run on local node, root@pam only
87 additionalProperties
=> 0,
90 permissions
=> { check
=> [ 'admin', 'audit' ] },
91 returns
=> { type
=> 'string' },
95 my $rdb = PMG
::RuleDB-
>new();
96 my $rulecache = PMG
::RuleCache-
>new($rdb);
98 return $rulecache->{digest
};
101 __PACKAGE__-
>register_method({
102 name
=> 'list_rules',
105 description
=> "Get list of rules.",
107 permissions
=> { check
=> [ 'admin', 'audit' ] },
109 additionalProperties
=> 0,
117 id
=> { type
=> 'integer' },
120 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
125 my $rdb = PMG
::RuleDB-
>new();
127 my $rules = $rdb->load_rules();
131 my $cond_create_group = sub {
132 my ($res, $name, $groupdata) = @_;
134 return if !$groupdata;
136 $res->{$name} = PMG
::API2
::ObjectGroupHelpers
::format_object_group
($groupdata);
139 foreach my $rule (@$rules) {
140 my ($from, $to, $when, $what, $action) =
141 $rdb->load_groups($rule);
143 my $data = PMG
::API2
::ObjectGroupHelpers
::format_rule
(
144 $rule, $from, $to, $when, $what, $action);
154 __PACKAGE__-
>register_method({
155 name
=> 'create_rule',
158 description
=> "Create new rule.",
161 permissions
=> { check
=> [ 'admin' ] },
163 additionalProperties
=> 0,
166 description
=> "Rule name",
170 description
=> "Rule priotity.",
176 description
=> "Rule direction. Value `0` matches incomming mails, value `1` matches outgoing mails, and value `2` matches both directions.",
183 description
=> "Flag to activate rule.",
189 returns
=> { type
=> 'integer' },
193 my $rdb = PMG
::RuleDB-
>new();
195 my $rule = PMG
::RuleDB
::Rule-
>new (
196 $param->{name
}, $param->{priority
}, $param->{active
}, $param->{direction
});
198 return $rdb->save_rule($rule);
201 __PACKAGE__-
>register_method ({
202 subclass
=> 'PMG::API2::Rules',
203 path
=> 'rules/{id}',
207 __PACKAGE__-
>register_method ({
208 subclass
=> 'PMG::API2::Action',
212 PMG
::API2
::ObjectGroupHelpers
::register_group_list_api
(__PACKAGE__
, 'what');
213 PMG
::API2
::ObjectGroupHelpers
::register_group_list_api
(__PACKAGE__
, 'when');
214 PMG
::API2
::ObjectGroupHelpers
::register_group_list_api
(__PACKAGE__
, 'who');
216 __PACKAGE__-
>register_method ({
217 subclass
=> 'PMG::API2::Who',
218 path
=> 'who/{ogroup}',
221 __PACKAGE__-
>register_method ({
222 subclass
=> 'PMG::API2::When',
223 path
=> 'when/{ogroup}',
226 __PACKAGE__-
>register_method ({
227 subclass
=> 'PMG::API2::What',
228 path
=> 'what/{ogroup}',