]>
Commit | Line | Data |
---|---|---|
62ebb4bc DM |
1 | package PMG::API2::Users; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Data::Dumper; | |
6 | ||
7 | use PVE::SafeSyslog; | |
8 | use PVE::Tools qw(extract_param); | |
9 | use PVE::JSONSchema qw(get_standard_option); | |
10 | use PVE::RESTHandler; | |
11 | use PVE::INotify; | |
12 | ||
13 | use PMG::UserConfig; | |
14 | ||
15 | use base qw(PVE::RESTHandler); | |
16 | ||
7bf06780 DM |
17 | my $extract_userdata = sub { |
18 | my ($entry) = @_; | |
19 | ||
20 | my $res = {}; | |
21 | foreach my $k (keys %$entry) { | |
22 | $res->{$k} = $entry->{$k} if $k ne 'crypt_pass'; | |
23 | } | |
24 | ||
25 | return $res; | |
26 | }; | |
27 | ||
62ebb4bc DM |
28 | __PACKAGE__->register_method ({ |
29 | name => 'index', | |
30 | path => '', | |
31 | method => 'GET', | |
32 | description => "List users.", | |
33 | proxyto => 'master', | |
34 | parameters => { | |
35 | additionalProperties => 0, | |
36 | properties => {}, | |
37 | }, | |
38 | returns => { | |
39 | type => 'array', | |
40 | items => { | |
41 | type => "object", | |
42 | properties => { | |
43 | userid => { type => 'string'}, | |
44 | enable => { type => 'boolean'}, | |
45 | role => { type => 'string'}, | |
46 | comment => { type => 'string', optional => 1}, | |
47 | }, | |
48 | }, | |
49 | links => [ { rel => 'child', href => "{userid}" } ], | |
50 | }, | |
51 | code => sub { | |
52 | my ($param) = @_; | |
53 | ||
54 | my $cfg = PMG::UserConfig->new(); | |
55 | ||
56 | my $res = []; | |
57 | ||
58 | foreach my $userid (sort keys %$cfg) { | |
7bf06780 | 59 | push @$res, $extract_userdata->($cfg->{$userid}); |
62ebb4bc DM |
60 | } |
61 | ||
62 | return $res; | |
63 | }}); | |
64 | ||
65 | __PACKAGE__->register_method ({ | |
66 | name => 'create', | |
67 | path => '', | |
68 | method => 'POST', | |
69 | proxyto => 'master', | |
70 | protected => 1, | |
71 | description => "Creat new user", | |
fff8e89c | 72 | parameters => $PMG::UserConfig::schema, |
62ebb4bc DM |
73 | returns => { type => 'null' }, |
74 | code => sub { | |
75 | my ($param) = @_; | |
76 | ||
77 | my $code = sub { | |
78 | ||
79 | my $cfg = PMG::UserConfig->new(); | |
80 | ||
81 | die "User '$param->{userid}' already exists\n" | |
82 | if $cfg->{$param->{userid}}; | |
83 | ||
fff8e89c DM |
84 | my $entry = {}; |
85 | foreach my $k (keys %$param) { | |
86 | my $v = $param->{$k}; | |
87 | if ($k eq 'password') { | |
88 | $entry->{$k} = PMG::Utils::encrypt_pw($v); | |
89 | } else { | |
90 | $entry->{$k} = $v; | |
91 | } | |
92 | } | |
93 | ||
94 | $entry->{enable} //= 0; | |
95 | $entry->{expire} //= 0; | |
96 | $entry->{role} //= 'audit'; | |
97 | ||
98 | $cfg->{$param->{userid}} = $entry; | |
62ebb4bc DM |
99 | |
100 | $cfg->write(); | |
101 | }; | |
102 | ||
103 | PMG::UserConfig::lock_config($code, "create user failed"); | |
104 | ||
105 | return undef; | |
106 | }}); | |
107 | ||
108 | __PACKAGE__->register_method ({ | |
109 | name => 'read', | |
110 | path => '{userid}', | |
111 | method => 'GET', | |
112 | description => "Read User data.", | |
113 | proxyto => 'master', | |
114 | parameters => { | |
115 | additionalProperties => 0, | |
116 | properties => { | |
4d813470 | 117 | userid => get_standard_option('userid'), |
62ebb4bc DM |
118 | }, |
119 | }, | |
120 | returns => { | |
121 | type => "object", | |
122 | properties => {}, | |
123 | }, | |
124 | code => sub { | |
125 | my ($param) = @_; | |
126 | ||
127 | my $cfg = PMG::UserConfig->new(); | |
128 | ||
7bf06780 DM |
129 | my $data = $cfg->lookup_user_data($param->{userid}); |
130 | ||
131 | my $res = $extract_userdata->($data); | |
132 | ||
133 | return $res; | |
62ebb4bc DM |
134 | }}); |
135 | ||
136 | __PACKAGE__->register_method ({ | |
137 | name => 'write', | |
138 | path => '{userid}', | |
139 | method => 'PUT', | |
140 | description => "Update user data.", | |
141 | protected => 1, | |
142 | proxyto => 'master', | |
0ecf02bc | 143 | parameters => $PMG::UserConfig::update_schema, |
62ebb4bc DM |
144 | returns => { type => 'null' }, |
145 | code => sub { | |
146 | my ($param) = @_; | |
147 | ||
148 | my $code = sub { | |
149 | ||
150 | my $cfg = PMG::UserConfig->new(); | |
151 | ||
0ecf02bc DM |
152 | my $userid = extract_param($param, 'userid'); |
153 | ||
154 | my $entry = $cfg->lookup_user_data($userid); | |
62ebb4bc | 155 | |
0ecf02bc DM |
156 | my $delete_str = extract_param($param, 'delete'); |
157 | die "no options specified\n" | |
158 | if !$delete_str && !scalar(keys %$param); | |
159 | ||
160 | foreach my $k (PVE::Tools::split_list($delete_str)) { | |
161 | delete $entry->{$k}; | |
162 | } | |
163 | ||
164 | foreach my $k (keys %$param) { | |
165 | my $v = $param->{$k}; | |
166 | if ($k eq 'password') { | |
167 | $entry->{$k} = PMG::Utils::encrypt_pw($v); | |
168 | } else { | |
169 | $entry->{$k} = $v; | |
170 | } | |
171 | } | |
62ebb4bc DM |
172 | |
173 | $cfg->write(); | |
174 | }; | |
175 | ||
176 | PMG::UserConfig::lock_config($code, "update user failed"); | |
177 | ||
178 | return undef; | |
179 | }}); | |
180 | ||
181 | __PACKAGE__->register_method ({ | |
182 | name => 'delete', | |
183 | path => '{userid}', | |
184 | method => 'DELETE', | |
185 | description => "Delete a user.", | |
186 | protected => 1, | |
187 | proxyto => 'master', | |
188 | parameters => { | |
189 | additionalProperties => 0, | |
190 | properties => { | |
277c84e4 | 191 | userid => get_standard_option('userid'), |
62ebb4bc DM |
192 | } |
193 | }, | |
194 | returns => { type => 'null' }, | |
195 | code => sub { | |
196 | my ($param) = @_; | |
197 | ||
198 | my $code = sub { | |
199 | ||
200 | my $cfg = PMG::UserConfig->new(); | |
201 | ||
202 | $cfg->lookup_user_data($param->{userid}); # user exists? | |
203 | ||
204 | delete $cfg->{$param->{userid}}; | |
205 | ||
206 | $cfg->write(); | |
207 | }; | |
208 | ||
209 | PMG::UserConfig::lock_config($code, "delete user failed"); | |
210 | ||
211 | return undef; | |
212 | }}); | |
213 | ||
214 | 1; |