]> git.proxmox.com Git - pmg-api.git/blame - PMG/API2/Action.pm
delete/deliver_quarantined_mail: use receiver instead of pmail
[pmg-api.git] / PMG / API2 / Action.pm
CommitLineData
11a86e67
DM
1package PMG::API2::Action;
2
3use strict;
4use warnings;
5use Data::Dumper;
6
7use PVE::SafeSyslog;
8use PVE::Tools qw(extract_param);
9use HTTP::Status qw(:constants);
10use PVE::JSONSchema qw(get_standard_option);
11use PVE::RESTHandler;
12use PVE::INotify;
13
14use PMG::Config;
15
16use PMG::RuleDB::BCC;
17use PMG::RuleDB;
18
19use base qw(PVE::RESTHandler);
20
bdf383f3
DM
21my $id_property = {
22 description => "Action Object ID.",
23 type => 'string',
24 pattern => '\d+_\d+',
25};
26
27my $format_action_object = sub {
28 my ($og, $action) = @_;
29
30 my $data = $action->get_data();
31 $data->{id} = "$data->{ogroup}_$data->{id}";
32 $data->{name} = $og->{name};
33 $data->{info} = $og->{info};
bdf383f3
DM
34
35 return $data;
36};
37
38my $load_action_with_og = sub {
39 my ($rdb, $id, $exp_otype) = @_;
40
41 die "internal error" if $id !~ m/^(\d+)_(\d+)$/;
42 my ($ogroup, $objid) = ($1, $2);
43
44 my $list = $rdb->load_objectgroups('action', $ogroup);
45 my $og = shift @$list ||
46 die "action group '$ogroup' not found\n";
47
48 my $action = $rdb->load_object_full($objid, $ogroup, $exp_otype);
49
50 return ($og, $action);
51};
59434740 52
11a86e67
DM
53__PACKAGE__->register_method ({
54 name => 'index',
55 path => '',
56 method => 'GET',
57 description => "Directory index.",
b07f4d68 58 permissions => { check => [ 'admin', 'audit' ] },
11a86e67
DM
59 parameters => {
60 additionalProperties => 0,
59434740 61 properties => {},
11a86e67
DM
62 },
63 returns => {
64 type => 'array',
65 items => {
66 type => "object",
67 properties => {
68 subdir => { type => 'string'},
69 },
70 },
71 links => [ { rel => 'child', href => "{subdir}" } ],
72 },
73 code => sub {
74 my ($param) = @_;
75
76 return [
11a86e67
DM
77 { subdir => 'objects' },
78 { subdir => 'bcc' },
b678cdfe 79 { subdir => 'field' },
4cb22484 80 { subdir => 'notification' },
f1d2c9e5 81 { subdir => 'disclaimer' },
46eec8b4 82 { subdir => 'removeattachments' },
11a86e67
DM
83 ];
84
85 }});
86
59434740
DM
87__PACKAGE__->register_method ({
88 name => 'list_actions',
89 path => 'objects',
90 method => 'GET',
91 description => "List 'actions' objects.",
b97da1ac 92 proxyto => 'master',
b07f4d68 93 permissions => { check => [ 'admin', 'audit' ] },
59434740
DM
94 parameters => {
95 additionalProperties => 0,
96 properties => {},
97 },
98 returns => {
99 type => 'array',
100 items => {
101 type => "object",
102 properties => {
103 id => $id_property,
104 },
105 },
106 links => [ { rel => 'child', href => "{id}" } ],
107 },
108 code => sub {
109 my ($param) = @_;
110
111 my $rdb = PMG::RuleDB->new();
112
113 my $ogroups = $rdb->load_objectgroups('action');
114 my $res = [];
115 foreach my $og (@$ogroups) {
116 my $action = $og->{action};
117 next if !$action;
bdf383f3 118 push @$res, $format_action_object->($og, $action);
59434740
DM
119 }
120
121 return $res;
122 }});
123
124__PACKAGE__->register_method ({
125 name => 'delete_action',
126 path => 'objects/{id}',
127 method => 'DELETE',
128 description => "Delete 'actions' object.",
b97da1ac
DM
129 proxyto => 'master',
130 protected => 1,
b07f4d68 131 permissions => { check => [ 'admin' ] },
59434740
DM
132 parameters => {
133 additionalProperties => 0,
134 properties => { id => $id_property }
135 },
136 returns => { type => 'null' },
137 code => sub {
138 my ($param) = @_;
139
140 my $rdb = PMG::RuleDB->new();
59434740 141 # test if object exists
bdf383f3 142 my ($og, $action) = $load_action_with_og->($rdb, $param->{id});
53333505 143
47c733fc 144 die "unable to delete standard actions\n" if !$action->oisedit();
53333505 145
bdf383f3 146 $rdb->delete_group($og->{id});
59434740
DM
147
148 return undef;
149 }});
150
151my $register_action_api = sub {
152 my ($class, $name) = @_;
153
154 my $otype = $class->otype();
155 my $otype_text = $class->otype_text();
156 my $properties = $class->properties();
157
bdf383f3
DM
158 my $create_properties = {
159 name => {
160 description => "Action name.",
161 type => 'string',
162 maxLength => 255,
163 },
164 info => {
165 description => "Informational comment.",
166 type => 'string',
167 maxLength => 255,
168 optional => 1,
169 },
170 };
171 my $update_properties = {
172 id => $id_property,
173 name => {
174 description => "Action name.",
175 type => 'string',
176 maxLength => 255,
177 optional => 1,
178 },
179 info => {
180 description => "Informational comment.",
181 type => 'string',
182 maxLength => 255,
183 optional => 1,
184 },
185 };
59434740
DM
186 my $read_properties = { id => $id_property };
187
188 foreach my $key (keys %$properties) {
189 $create_properties->{$key} = $properties->{$key};
190 $update_properties->{$key} = $properties->{$key};
191 }
192
193 __PACKAGE__->register_method ({
194 name => $name,
195 path => $name,
196 method => 'POST',
197 description => "Create '$otype_text' object.",
198 proxyto => 'master',
b97da1ac 199 protected => 1,
b07f4d68 200 permissions => { check => [ 'admin' ] },
59434740
DM
201 parameters => {
202 additionalProperties => 0,
203 properties => $create_properties,
204 },
205 returns => {
206 description => "The object ID.",
207 type => 'string',
208 },
209 code => sub {
210 my ($param) = @_;
211
212 my $rdb = PMG::RuleDB->new();
213
214 my $obj = $rdb->get_object($otype);
215 $obj->update($param);
216
9973fc66 217 my $og = $rdb->create_group_with_obj($obj, $param->{name}, $param->{info});
59434740
DM
218
219 return "$og->{id}_$obj->{id}";
220 }});
221
222 __PACKAGE__->register_method ({
223 name => "read_$name",
224 path => "$name/{id}",
225 method => 'GET',
226 description => "Read '$otype_text' object settings.",
227 proxyto => 'master',
b07f4d68 228 permissions => { check => [ 'admin', 'audit' ] },
59434740
DM
229 parameters => {
230 additionalProperties => 0,
231 properties => $read_properties,
232 },
233 returns => {
234 type => "object",
235 properties => {
236 id => { type => 'string'},
237 },
238 },
239 code => sub {
240 my ($param) = @_;
241
242 my $rdb = PMG::RuleDB->new();
243
bdf383f3 244 my ($og, $action) = $load_action_with_og->($rdb, $param->{id}, $otype);
59434740 245
bdf383f3 246 return $format_action_object->($og, $action);
59434740
DM
247 }});
248
249 __PACKAGE__->register_method ({
250 name => "update_$name",
251 path => "$name/{id}",
252 method => 'PUT',
253 description => "Update '$otype_text' object.",
254 proxyto => 'master',
b97da1ac 255 protected => 1,
b07f4d68 256 permissions => { check => [ 'admin' ] },
59434740
DM
257 parameters => {
258 additionalProperties => 0,
259 properties => $update_properties,
260 },
261 returns => { type => 'null' },
262 code => sub {
263 my ($param) = @_;
264
265 my $rdb = PMG::RuleDB->new();
266
bdf383f3
DM
267 my ($og, $action) = $load_action_with_og->($rdb, $param->{id}, $otype);
268
269 my $name = extract_param($param, 'name');
270 my $info = extract_param($param, 'info');
271
272 if (defined($name) || defined($info)) {
273 $og->{name} = $name if defined($name);
274 $og->{info} = $info if defined($info);
275 $rdb->save_group($og);
276
277 return undef if !scalar(keys %$param); # we are done
278 }
59434740 279
bdf383f3
DM
280 die "no options specified\n"
281 if !scalar(keys %$param);
59434740
DM
282
283 $action->update($param);
284
285 $action->save($rdb);
286
cbef3ff8
DM
287 PMG::DBTools::reload_ruledb();
288
59434740
DM
289 return undef;
290 }});
291
292};
293
294$register_action_api->('PMG::RuleDB::BCC', 'bcc');
b678cdfe 295$register_action_api->('PMG::RuleDB::ModField', 'field');
4cb22484 296$register_action_api->('PMG::RuleDB::Notify', 'notification');
f1d2c9e5 297$register_action_api->('PMG::RuleDB::Disclaimer', 'disclaimer');
46eec8b4 298$register_action_api->('PMG::RuleDB::Remove', 'removeattachments');
11a86e67 299
11a86e67 3001;