]>
git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Users.pm
1 package PMG
::API2
::Users
;
8 use PVE
::Tools
qw(extract_param);
9 use PVE
::JSONSchema
qw(get_standard_option);
15 use base
qw(PVE::RESTHandler);
17 my $extract_userdata = sub {
21 foreach my $k (keys %$entry) {
22 $res->{$k} = $entry->{$k} if $k ne 'crypt_pass';
28 __PACKAGE__-
>register_method ({
32 description
=> "List users.",
35 additionalProperties
=> 0,
43 userid
=> { type
=> 'string'},
44 enable
=> { type
=> 'boolean'},
45 role => { type
=> 'string'},
46 comment
=> { type
=> 'string', optional
=> 1},
49 links
=> [ { rel
=> 'child', href
=> "{userid}" } ],
54 my $cfg = PMG
::UserConfig-
>new();
58 foreach my $userid (sort keys %$cfg) {
59 push @$res, $extract_userdata->($cfg->{$userid});
65 __PACKAGE__-
>register_method ({
71 description
=> "Creat new user",
72 parameters
=> $PMG::UserConfig
::create_schema
,
73 returns
=> { type
=> 'null' },
79 my $cfg = PMG
::UserConfig-
>new();
81 die "User '$param->{userid}' already exists\n"
82 if $cfg->{$param->{userid
}};
85 foreach my $k (keys %$param) {
87 if ($k eq 'password') {
88 $entry->{$k} = PMG
::Utils
::encrypt_pw
($v);
94 $entry->{enable
} //= 0;
95 $entry->{expire
} //= 0;
96 $entry->{role} //= 'audit';
98 $cfg->{$param->{userid
}} = $entry;
103 PMG
::UserConfig
::lock_config
($code, "create user failed");
108 __PACKAGE__-
>register_method ({
112 description
=> "Read User data.",
115 additionalProperties
=> 0,
117 userid
=> get_standard_option
('userid'),
127 my $cfg = PMG
::UserConfig-
>new();
129 my $data = $cfg->lookup_user_data($param->{userid
});
131 my $res = $extract_userdata->($data);
136 __PACKAGE__-
>register_method ({
140 description
=> "Update user data.",
143 parameters
=> $PMG::UserConfig
::update_schema
,
144 returns
=> { type
=> 'null' },
150 my $cfg = PMG
::UserConfig-
>new();
152 my $userid = extract_param
($param, 'userid');
154 my $entry = $cfg->lookup_user_data($userid);
156 my $delete_str = extract_param
($param, 'delete');
157 die "no options specified\n"
158 if !$delete_str && !scalar(keys %$param);
160 foreach my $k (PVE
::Tools
::split_list
($delete_str)) {
164 foreach my $k (keys %$param) {
165 my $v = $param->{$k};
166 if ($k eq 'password') {
167 $entry->{$k} = PMG
::Utils
::encrypt_pw
($v);
176 PMG
::UserConfig
::lock_config
($code, "update user failed");
181 __PACKAGE__-
>register_method ({
185 description
=> "Delete a user.",
189 additionalProperties
=> 0,
191 userid
=> get_standard_option
('userid'),
194 returns
=> { type
=> 'null' },
200 my $cfg = PMG
::UserConfig-
>new();
202 $cfg->lookup_user_data($param->{userid
}); # user exists?
204 delete $cfg->{$param->{userid
}};
209 PMG
::UserConfig
::lock_config
($code, "delete user failed");