[pve-devel] [PATCH access-control] add ls command for pvum user

Stoiko Ivanov s.ivanov at proxmox.com
Tue May 8 17:25:35 CEST 2018


fixes #1502

---
 PVE/CLI/pveum.pm | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/PVE/CLI/pveum.pm b/PVE/CLI/pveum.pm
index a4e584d..107b759 100755
--- a/PVE/CLI/pveum.pm
+++ b/PVE/CLI/pveum.pm
@@ -18,6 +18,7 @@ use PVE::API2::ACL;
 use PVE::API2::AccessControl;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::CLIHandler;
+use List::Util qw(reduce);
 
 use base qw(PVE::CLIHandler);
 
@@ -37,25 +38,84 @@ sub read_password {
     return $input;
 }
 
+sub print_index {
+    my ($formatopts, $data) = @_;
+    my ($formatstring, @keylist, @titles, %defaults);
+    @keylist = map { $_->{'key'} } @$formatopts;
+    foreach my $col ( @$formatopts ) {
+	my ($key, $title, $cutoff) = @$col{ qw(key name cutoff) };
+	$title = $key if !defined $title;
+	$defaults{$key} = $col->{'default'};
+	push @titles, ($title);
+	my $titlelen = length $title;
+	my @value_lengths = map { length($_->{$key}) // 1 } @$data;
+	my $longest = reduce { $a > $b ? $a : $b  } @value_lengths;
+	$longest = $cutoff if (defined $cutoff && $cutoff < $longest);
+	my $printlenmax = $longest > $titlelen? $longest : $titlelen;
+	my $printalign = $longest > $titlelen ? '-' : '';
+	if (defined $cutoff) {
+	    $formatstring .= "%$printalign$printlenmax".'s ';
+	} else {
+	    $formatstring .= "%s\n";
+	}
+    }
+    printf $formatstring, @titles;
+    foreach my $line (sort { $a->{$keylist[0]} cmp $b->{$keylist[0]} } @$data){
+        printf $formatstring, map { $line->{$_} // $defaults{$_} } @keylist;
+    }
+}
+
+sub print_entry {
+    my $entry = shift;
+    foreach my $item (sort keys %$entry) {
+	if (ref($entry->{$item}) eq 'ARRAY'){
+	    printf "%s: [ %s ]\n", $item, join(", ", @{$entry->{$item}});
+	} else {
+	    printf "%s: %s\n", $item, $entry->{$item};
+	}
+    }
+}
+
 our $cmddef = {
     user => {
 	add    => [ 'PVE::API2::User', 'create_user', ['userid'] ],
 	modify => [ 'PVE::API2::User', 'update_user', ['userid'] ],
 	delete => [ 'PVE::API2::User', 'delete_user', ['userid'] ],
+	list   => [ 'PVE::API2::User', 'index', [], {}, sub {
+	    print_index( [ { key => 'userid', name => 'Name', cutoff => '70' },
+			    { key => 'enabled', default => 1 } ], shift);
+	    } ],
+	dump   => [ 'PVE::API2::User', 'read_user', ['userid'], undef, \&print_entry ],
     },
     group => {
 	add    => [ 'PVE::API2::Group', 'create_group', ['groupid'] ],
 	modify => [ 'PVE::API2::Group', 'update_group', ['groupid'] ],
 	delete => [ 'PVE::API2::Group', 'delete_group', ['groupid'] ],
+	list   => [ 'PVE::API2::Group', 'index', [], {}, sub {
+	    print_index( [ { key => 'groupid', name => 'Name'}], shift);
+	} ],
+	dump   => [ 'PVE::API2::Group', 'read_group', ['groupid'], {}, \&print_entry ],
     },
     role => {
 	add    => [ 'PVE::API2::Role', 'create_role', ['roleid'] ],
 	modify => [ 'PVE::API2::Role', 'update_role', ['roleid'] ],
 	delete => [ 'PVE::API2::Role', 'delete_role', ['roleid'] ],
+	list   => [ 'PVE::API2::Role', 'index', [], {}, sub {
+	    print_index( [ { key => 'roleid', name => 'Name', cutoff => 30 },
+			    { key => 'special', name => 'Built-In', cutoff => 1 },
+			    { key => 'privs', name => 'Privileges' } ], shift);
+	    } ],
     },
     acl => {
 	modify => [ 'PVE::API2::ACL', 'update_acl', ['path'], { delete => 0 }],
 	delete => [ 'PVE::API2::ACL', 'update_acl', ['path'], { delete => 1 }],
+	list   => [ 'PVE::API2::ACL', 'read_acl', [], {}, sub {
+	    print_index( [ { key => 'type', cutoff => 5 },
+			    { key => 'ugid', name => 'id', cutoff => 30 },
+			    { key => 'roleid', name => 'role', cutoff => 30 },
+			    { key => 'propagate', cutoff => 1 },
+			    { key => 'path' } ], shift);
+	    } ],
     },
     ticket => [ 'PVE::API2::AccessControl', 'create_ticket', ['username'], undef,
 		sub {
-- 
2.11.0




More information about the pve-devel mailing list