[pve-devel] r4856 - pve-manager/pve2/lib/PVE/API2

svn-commits at proxmox.com svn-commits at proxmox.com
Thu Jul 1 11:08:21 CEST 2010


Author: dietmar
Date: 2010-07-01 09:08:21 +0000 (Thu, 01 Jul 2010)
New Revision: 4856

Added:
   pve-manager/pve2/lib/PVE/API2/AccessControl.pm
   pve-manager/pve2/lib/PVE/API2/User.pm
Log:


Added: pve-manager/pve2/lib/PVE/API2/AccessControl.pm
===================================================================
--- pve-manager/pve2/lib/PVE/API2/AccessControl.pm	                        (rev 0)
+++ pve-manager/pve2/lib/PVE/API2/AccessControl.pm	2010-07-01 09:08:21 UTC (rev 4856)
@@ -0,0 +1,56 @@
+package PVE::API2::AccessControl;
+
+use strict;
+use warnings;
+
+use PVE::SafeSyslog;
+use Apache2::Const qw(:http);
+
+use PVE::RESTHandler;
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method ({
+    subclass => "PVE::API2::User",  
+    match_re => [ 'users' ],
+});
+
+__PACKAGE__->register_method ({
+    name => 'index', 
+    match_re => [], 
+    method => 'GET',
+    description => "Directory index.",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => "object",
+	    properties => {
+		subdir => { type => 'string' },
+	    },
+	},
+	links => [ { rel => 'index', href => "{subdir}" } ],
+    },
+}); 
+sub index {
+    my ($conn, $resp, $param) = @_;
+    
+    my $res = [];
+
+    my $ma = __PACKAGE__->method_attributes();
+
+    foreach my $info (@$ma) {
+	next if !$info->{subclass};
+
+	my $subpath = $info->{match_re}->[0];
+
+	push @$res, { subdir => $subpath };
+    }
+
+    return $res;
+}
+
+1;

Added: pve-manager/pve2/lib/PVE/API2/User.pm
===================================================================
--- pve-manager/pve2/lib/PVE/API2/User.pm	                        (rev 0)
+++ pve-manager/pve2/lib/PVE/API2/User.pm	2010-07-01 09:08:21 UTC (rev 4856)
@@ -0,0 +1,152 @@
+package PVE::API2::User;
+
+use strict;
+use warnings;
+use PVE::AccessControl;
+use PVE::Config;
+
+use PVE::SafeSyslog;
+use Apache2::Const qw(:http);
+use Data::Dumper; # fixme: remove
+
+use PVE::RESTHandler;
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method ({
+    name => 'index', 
+    match_re => [], 
+    method => 'GET',
+    description => "User index.",
+    parameters => {
+	additionalProperties => 0,
+	properties => {},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => "object",
+	    properties => {
+		id => { type => 'string' },
+	    },
+	},
+	links => [ { rel => 'index', href => "{id}" } ],
+    },
+}); 
+sub index {
+    my ($conn, $resp, $param) = @_;
+    
+    my $res = [];
+
+    my $usercfg = PVE::Config::read_file("usercfg");
+ 
+    foreach my $user (keys %{$usercfg->{users}}) {
+	next if $user eq 'root';
+
+	push @$res, { id => $user };
+    }
+
+    return $res;
+}
+
+__PACKAGE__->register_method ({
+    name => 'create_user', 
+    protected => 1,
+    match_re => [ '\S+' ], 
+    method => 'POST',
+    description => "Create new user.",
+    parameters => {
+	type => "object",
+	properties => {
+	    password => { type => 'string', optional => 1 },
+	    groups => { type => 'string', optional => 1 },
+	},
+    },
+    returns => { type => 'null' },
+}); 
+sub create_user {
+    my ($conn, $resp, $param) = @_;
+
+    my ($userid) = $conn->{rel_uri} =~ m|/([^/]+)$|;
+
+    $param->{create} = 1;
+    PVE::AccessControl::modify_user($userid, $param);
+
+    # fixme: maybe it is better to return the user data ?
+
+    return undef;
+}
+
+__PACKAGE__->register_method ({
+    name => 'get_user', 
+    match_re => [ '\S+' ], 
+    method => 'GET',
+    description => "Get user configuration.",
+    parameters => {},
+    returns => {},
+}); 
+sub get_user {
+    my ($conn, $resp, $param) = @_;
+
+    my ($userid) = $conn->{rel_uri} =~ m|/([^/]+)$|;
+
+    my $usercfg = PVE::Config::read_file("usercfg");
+ 
+    my $data = $usercfg->{users}->{$userid};
+    die "no such user\n" if !$data;
+
+    return $data;
+}
+
+__PACKAGE__->register_method ({
+    name => 'update_user', 
+    protected => 1,
+    match_re => [ '\S+' ], 
+    method => 'PUT',
+    description => "Update user configuration.",
+    parameters => {
+	type => "object",
+	properties => {
+	    password => { type => 'string', optional => 1 },
+	    groups => { type => 'string', optional => 1 },
+	    append => { 
+		type => 'boolean', 
+		optional => 1,
+		requires => 'groups',
+	    },
+	    lock => { type => 'boolean', optional => 1 },
+	    unlock => { type => 'boolean', optional => 1 },
+	    name => { type => 'string', optional => 1 },
+	    comment => { type => 'string', optional => 1 },
+	},
+    },
+    returns => {},
+}); 
+sub update_user {
+    my ($conn, $resp, $param) = @_;
+
+    my ($userid) = $conn->{rel_uri} =~ m|/([^/]+)$|;
+
+    return PVE::AccessControl::modify_user($userid, $param);
+}
+
+__PACKAGE__->register_method ({
+    name => 'delete_user', 
+    protected => 1,
+    match_re => [ '\S+' ], 
+    method => 'DELETE',
+    description => "Delete user.",
+    parameters => {},
+    returns => { type => 'null' },
+}); 
+sub delete_user {
+    my ($conn, $resp) = @_;
+
+    my ($userid) = $conn->{rel_uri} =~ m|/([^/]+)$|;
+    
+    PVE::AccessControl::delete_user($userid);  
+
+    return undef;
+}
+
+1;



More information about the pve-devel mailing list