[! use strict; use Proxmox::Form; use Proxmox::Utils; use Proxmox::Config::System; use Proxmox::ConfigServer; use Proxmox::HTMLTable; use Proxmox::HTMLDropDown; use HTML::Entities; !] [- my $frm = Proxmox::Form->new (\%fdat); my $system_cfg = $fdat{__system_config}; my $cinfo = $fdat{__cinfo}; my $cluster = $system_cfg->get ('administration', 'cluster'); my $url = $req_rec->uri; my $keys = ""; my $out = ""; my $roleinfo = Proxmox::Utils::roleinfo(); my $conn = Proxmox::ConfigClient::connect (); sub canwrite { my $account = shift; return 0 if $account eq 'root' && $udat{auth_username} ne 'root'; return $udat{AM} eq 'w'; } if ($udat{action} eq 'delete') { if (canwrite ($fdat {aa})) { $conn->delete_user ($fdat {aa}); } else { $udat{popup_error} = Proxmox::Utils::msg ('nowr'); } } if ($frm->submit) { my $account; if ($fdat{mode} == 1) { $account = Proxmox::Utils::trim ($fdat{aa}); } else { $account = Proxmox::Utils::trim ($fdat{name}); } if (canwrite ($account) || ($fdat{mode} == 1 && $udat{auth_username} eq $account)) { my $err; my $comment = encode_entities (Proxmox::Utils::trim ($fdat{comment})); my $userinfo = $conn->userlist ()->result; eval { Proxmox::Utils::check_field (__('Username'), $account, 'NOTEMPTY', 'NOWHITESPACES','CHAREXCL:\W'); if ($fdat{mode} == 2) { die __("User already exists") . "\n" if $userinfo->{$account}->{group}; Proxmox::Utils::check_field (__("Password"), $fdat{newpwd1}, 'NOTEMPTY'); } die __("Passwords does not match") . "\n" if $fdat{newpwd1} ne $fdat{newpwd2}; die __("Password is too short") . "\n" if $fdat{newpwd1} && length ($fdat{newpwd1}) < 5; Proxmox::Utils::check_field (__("Comment"), $comment, "CHAREXCL:\[^\\w\\s;,&-\]"); }; $err = $@; if (!$err) { my $res; if ($fdat{mode} == 1) { if (canwrite ($account)) { $res = $conn->modify_user ($account, $fdat{role}, $fdat{newpwd1}, $comment)->result; } else { $res = $conn->modify_user ($account, undef, $fdat{newpwd1})->result; } } else { $res = $conn->create_user ($account, $fdat{role}, $fdat{newpwd1}, $comment)->result; } if ($res) { $udat{popup_error} = sprintf (__("Unable to create user: %s"), $res); } else { $conn->ssh_authorized_keys ($fdat{sshkeys}) if !$cluster; } } else { $udat{popup_error} = $err; $fdat{edit} = $fdat{mode}; } } else { $udat{popup_error} = Proxmox::Utils::msg ('nowr'); } } $keys = $conn->ssh_authorized_keys ()->result; if ($fdat{edit} == 1 || ($fdat{edit} == 2 && $udat{AM} eq 'w')) { my $account = $fdat{aa}; my $data = {}; if ($fdat{edit} == 1) { my $userinfo = $conn->userlist ()->result; $data->{role} = $userinfo->{$account}->{group}; $data->{comment} = decode_entities ($userinfo->{$account}->{comment}); } if ($fdat{edit} == 1) { $frm->add_element("section1", "hsection", undef, __("Edit user")); $frm->add_element("name", "rotext", $account, __("Username")); } else { $frm->add_element("section1", "hsection", undef, __("Create new user")); $frm->add_element("name", "text", '', __("Username")); } if ($account eq 'root') { $frm->add_element("rorole", "rotext", $roleinfo->{root}, __("Role")); $frm->add_element("role", "hidden", 'root'); } else { my $roles = []; if (canwrite ($account)) { foreach my $r (keys %$roleinfo) { next if $r eq 'root'; next if $r eq 'quser'; push @$roles, [ $r, $roleinfo->{$r} ]; } } else { push @$roles, [ $data->{role}, $roleinfo->{$data->{role}} ]; } $frm->add_element("role", "dropdown", $data->{role} || '', __("Role"), $roles); } if (canwrite ($account) || ($fdat{edit} == 1 && $udat{auth_username} eq $account)) { $frm->add_element("newpwd1","password", "", __("Password")); $frm->add_element("newpwd2", "password", "", __("Confirm Password")); } if ($account ne 'root') { my $em = canwrite ($account) ? 'text' : 'rotext'; $frm->add_element("comment", $em, $data->{comment} || '', __("Comment")); } if (!$cluster && ($account eq 'root')) { my $keys = $conn->ssh_authorized_keys ()->result; $frm->add_element("ssh", "section", "", __("SSH Access")); $frm->add_element("sshkeys", "textarea", "$keys", "|" . __("Allowed SSH Public Keys:")); } $frm->add_element("m3", "hidden"); $frm->add_element("aa", "hidden", $account); $frm->add_element("mode", "hidden", $fdat{edit}); $out .= $frm->out_form; } else { if ($fdat{edit}) { $udat{popup_error} = Proxmox::Utils::msg ('nowr'); } my $ddown = Proxmox::HTMLDropDown->new (); $ddown->add_item("menu0","$url?edit=2", __("Create new user")); $ddown->add_item("menu1","$url?edit=1", __("Edit user")); $ddown->add_item("menu1","$url?action=delete", __("Delete")); $ddown->add_item("menu2","$url?edit=1", __("Edit user")); my @cellwidth = ( '20px', '10px', '100px', '150px', '370px'); my @header = ( '1', '20px', ' ', '1', '10px', ' ', '1', '100px', __('Username'), '1', '150px', __('Role'), '1', '370px', __('Comment'), ); my $table = Proxmox::HTMLTable->new (\@cellwidth); $table->add_headline (\@header); my $menu = $ddown->out_symbol ("menu2","","&aa=root"); $table->set_row_link ("$url?edit=1&aa=root"); $table->add_row ('', $menu, '', 'root', $roleinfo->{'root'}, 'Superuser'); my $userinfo = $conn->userlist ()->result; foreach my $u (sort keys %$userinfo) { next if $u eq 'root'; my $role = $roleinfo->{$userinfo->{$u}->{group}}; next if !$role; $menu = $ddown->out_symbol ("menu1","","&aa=$u"); $table->set_row_link ("$url?edit=1&aa=$u"); $table->add_row ('', $menu, '', $u, $role, $userinfo->{$u}->{comment}); } $out .= $ddown->out_dropdown_menu("menu0"); $out .= $ddown->out_dropdown_menu("menu1"); $out .= $ddown->out_dropdown_menu("menu2"); $out .= "