1 package PVE
::API2
::User
;
5 use PVE
::Exception
qw(raise raise_perm_exc);
6 use PVE
::Cluster qw
(cfs_read_file cfs_write_file
);
7 use PVE
::Tools
qw(split_list);
8 use PVE
::AccessControl
;
9 use PVE
::JSONSchema
qw(get_standard_option);
13 use Data
::Dumper
; # fixme: remove
17 use base
qw(PVE::RESTHandler);
19 my $extract_user_data = sub {
20 my ($data, $full) = @_;
24 foreach my $prop (qw(enable expire firstname lastname email comment)) {
25 $res->{$prop} = $data->{$prop} if defined($data->{$prop});
28 return $res if !$full;
30 $res->{groups
} = $data->{groups
} ?
[ keys %{$data->{groups
}} ] : [];
35 __PACKAGE__-
>register_method ({
39 description
=> "User index.",
41 description
=> "The returned list is restricted to users where you have 'User.Modify' or 'Sys.Audit' permissions on '/access/groups' or on a group the user belongs too. But it always includes the current (authenticated) user.",
45 additionalProperties
=> 0,
49 description
=> "Optional filter for enable property.",
59 userid
=> { type
=> 'string' },
62 links
=> [ { rel
=> 'child', href
=> "{userid}" } ],
67 my $rpcenv = PVE
::RPCEnvironment
::get
();
68 my $usercfg = $rpcenv->{user_cfg
};
69 my $authuser = $rpcenv->get_user();
73 my $privs = [ 'User.Modify', 'Sys.Audit' ];
74 my $canUserMod = $rpcenv->check_any($authuser, "/access/groups", $privs, 1);
75 my $groups = $rpcenv->filter_groups($authuser, $privs, 1);
76 my $allowed_users = $rpcenv->group_member_join([keys %$groups]);
78 foreach my $user (keys %{$usercfg->{users
}}) {
80 if (!($canUserMod || $user eq $authuser)) {
81 next if !$allowed_users->{$user};
84 my $entry = &$extract_user_data($usercfg->{users
}->{$user});
86 if (defined($param->{enabled
})) {
87 next if $entry->{enable
} && !$param->{enabled
};
88 next if !$entry->{enable
} && $param->{enabled
};
91 $entry->{userid
} = $user;
98 __PACKAGE__-
>register_method ({
99 name
=> 'create_user',
104 description
=> "You need 'Realm.AllocateUser' on '/access/realm/<realm>' on the realm of user <userid>, and 'User.Modify' permissions to '/access/groups/<group>' for any group specified (or 'User.Modify' on '/access/groups' if you pass no groups.",
106 [ 'userid-param', 'Realm.AllocateUser'],
107 [ 'userid-group', ['User.Modify'], groups_param
=> 1],
110 description
=> "Create new user.",
112 additionalProperties
=> 0,
114 userid
=> get_standard_option
('userid'),
116 description
=> "Initial password.",
122 groups
=> { type
=> 'string', optional
=> 1, format
=> 'pve-groupid-list'},
123 firstname
=> { type
=> 'string', optional
=> 1 },
124 lastname
=> { type
=> 'string', optional
=> 1 },
125 email
=> { type
=> 'string', optional
=> 1, format
=> 'email-opt' },
126 comment
=> { type
=> 'string', optional
=> 1 },
128 description
=> "Account expiration date (seconds since epoch). '0' means no expiration date.",
134 description
=> "Enable the account (default). You can set this to '0' to disable the accout",
141 returns
=> { type
=> 'null' },
145 PVE
::AccessControl
::lock_user_config
(
148 my ($username, $ruid, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
150 my $usercfg = cfs_read_file
("user.cfg");
152 die "user '$username' already exists\n"
153 if $usercfg->{users
}->{$username};
155 PVE
::AccessControl
::domain_set_password
($realm, $ruid, $param->{password
})
156 if defined($param->{password
});
158 my $enable = defined($param->{enable
}) ?
$param->{enable
} : 1;
159 $usercfg->{users
}->{$username} = { enable
=> $enable };
160 $usercfg->{users
}->{$username}->{expire
} = $param->{expire
} if $param->{expire
};
162 if ($param->{groups
}) {
163 foreach my $group (split_list
($param->{groups
})) {
164 if ($usercfg->{groups
}->{$group}) {
165 PVE
::AccessControl
::add_user_group
($username, $usercfg, $group);
167 die "no such group '$group'\n";
172 $usercfg->{users
}->{$username}->{firstname
} = $param->{firstname
} if $param->{firstname
};
173 $usercfg->{users
}->{$username}->{lastname
} = $param->{lastname
} if $param->{lastname
};
174 $usercfg->{users
}->{$username}->{email
} = $param->{email
} if $param->{email
};
175 $usercfg->{users
}->{$username}->{comment
} = $param->{comment
} if $param->{comment
};
177 cfs_write_file
("user.cfg", $usercfg);
178 }, "create user failed");
183 __PACKAGE__-
>register_method ({
187 description
=> "Get user configuration.",
189 check
=> ['userid-group', ['User.Modify', 'Sys.Audit']],
192 additionalProperties
=> 0,
194 userid
=> get_standard_option
('userid'),
198 additionalProperties
=> 0,
200 enable
=> { type
=> 'boolean' },
201 expire
=> { type
=> 'integer', optional
=> 1 },
202 firstname
=> { type
=> 'string', optional
=> 1 },
203 lastname
=> { type
=> 'string', optional
=> 1 },
204 email
=> { type
=> 'string', optional
=> 1 },
205 comment
=> { type
=> 'string', optional
=> 1 },
206 groups
=> { type
=> 'array' },
212 my ($username, undef, $domain) =
213 PVE
::AccessControl
::verify_username
($param->{userid
});
215 my $usercfg = cfs_read_file
("user.cfg");
217 my $data = PVE
::AccessControl
::check_user_exist
($usercfg, $username);
219 return &$extract_user_data($data, 1);
222 __PACKAGE__-
>register_method ({
223 name
=> 'update_user',
228 check
=> ['userid-group', ['User.Modify'], groups_param
=> 1 ],
230 description
=> "Update user configuration.",
232 additionalProperties
=> 0,
234 userid
=> get_standard_option
('userid'),
235 groups
=> { type
=> 'string', optional
=> 1, format
=> 'pve-groupid-list' },
239 requires
=> 'groups',
242 description
=> "Enable/disable the account.",
246 firstname
=> { type
=> 'string', optional
=> 1 },
247 lastname
=> { type
=> 'string', optional
=> 1 },
248 email
=> { type
=> 'string', optional
=> 1, format
=> 'email-opt' },
249 comment
=> { type
=> 'string', optional
=> 1 },
251 description
=> "Account expiration date (seconds since epoch). '0' means no expiration date.",
258 returns
=> { type
=> 'null' },
262 my ($username, $ruid, $realm) =
263 PVE
::AccessControl
::verify_username
($param->{userid
});
265 PVE
::AccessControl
::lock_user_config
(
268 my $usercfg = cfs_read_file
("user.cfg");
270 PVE
::AccessControl
::check_user_exist
($usercfg, $username);
272 $usercfg->{users
}->{$username}->{enable
} = $param->{enable
} if defined($param->{enable
});
274 $usercfg->{users
}->{$username}->{expire
} = $param->{expire
} if defined($param->{expire
});
276 PVE
::AccessControl
::delete_user_group
($username, $usercfg)
277 if (!$param->{append
} && defined($param->{groups
}));
279 if ($param->{groups
}) {
280 foreach my $group (split_list
($param->{groups
})) {
281 if ($usercfg->{groups
}->{$group}) {
282 PVE
::AccessControl
::add_user_group
($username, $usercfg, $group);
284 die "no such group '$group'\n";
289 $usercfg->{users
}->{$username}->{firstname
} = $param->{firstname
} if defined($param->{firstname
});
290 $usercfg->{users
}->{$username}->{lastname
} = $param->{lastname
} if defined($param->{lastname
});
291 $usercfg->{users
}->{$username}->{email
} = $param->{email
} if defined($param->{email
});
292 $usercfg->{users
}->{$username}->{comment
} = $param->{comment
} if defined($param->{comment
});
294 cfs_write_file
("user.cfg", $usercfg);
295 }, "update user failed");
300 __PACKAGE__-
>register_method ({
301 name
=> 'delete_user',
305 description
=> "Delete user.",
308 [ 'userid-param', 'Realm.AllocateUser'],
309 [ 'userid-group', ['User.Modify']],
313 additionalProperties
=> 0,
315 userid
=> get_standard_option
('userid'),
318 returns
=> { type
=> 'null' },
322 my $rpcenv = PVE
::RPCEnvironment
::get
();
323 my $authuser = $rpcenv->get_user();
325 my ($userid, $ruid, $realm) =
326 PVE
::AccessControl
::verify_username
($param->{userid
});
328 PVE
::AccessControl
::lock_user_config
(
331 my $usercfg = cfs_read_file
("user.cfg");
333 my $domain_cfg = cfs_read_file
('domains.cfg');
334 if (my $cfg = $domain_cfg->{ids
}->{$realm}) {
335 my $plugin = PVE
::Auth
::Plugin-
>lookup($cfg->{type
});
336 $plugin->delete_user($cfg, $realm, $ruid);
339 delete $usercfg->{users
}->{$userid};
341 PVE
::AccessControl
::delete_user_group
($userid, $usercfg);
342 PVE
::AccessControl
::delete_user_acl
($userid, $usercfg);
344 cfs_write_file
("user.cfg", $usercfg);
345 }, "delete user failed");