]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/JSONSchema.pm
allow to pass undefined value to template_replace
[pve-common.git] / data / PVE / JSONSchema.pm
index c97454dceb9f77eca491b18f98ce88e94f46aa5c..45f02c537f360ccdef79e749a5653bed1af1e83d 100644 (file)
@@ -740,14 +740,21 @@ my $method_schema = {
            optional => 1,
            additionalProperties => 0,
            properties => {
+               description => {
+                    description => "Describe access permissions.",
+                    optional => 1,
+               },
                 user => {
-                    description => "A simply way to allow access for 'all' users. The special value 'arg' allows access for the user specified in the 'username' parameter. This is useful to allow access to things owned by a user, like changing the user password. Value 'world' is used to allow access without credentials.", 
+                    description => "A simply way to allow access for 'all' authenticated users. Value 'world' is used to allow access without credentials.", 
                     type => 'string', 
-                    enum => ['all', 'arg', 'world'],
+                    enum => ['all', 'world'],
                     optional => 1,
                 },
-                path => { type => 'string', optional => 1, requires => 'privs' },
-                privs => { type => 'array', optional => 1, requires => 'path' },
+                check => {
+                    description => "Array of permission checks (prefix notation).",
+                    type => 'array', 
+                    optional => 1 
+                },
             },
         },
         match_name => {
@@ -849,7 +856,7 @@ sub method_get_child_link {
 # a way to parse command line parameters, using a 
 # schema to configure Getopt::Long
 sub get_options {
-    my ($schema, $args, $uri_param, $pwcallback, $list_param) = @_;
+    my ($schema, $args, $arg_param, $fixed_param, $pwcallback) = @_;
 
     if (!$schema || !$schema->{properties}) {
        raise("too many arguments\n", code => HTTP_BAD_REQUEST)
@@ -857,11 +864,19 @@ sub get_options {
        return {};
     }
 
+    my $list_param;
+    if ($arg_param && !ref($arg_param)) {
+       my $pd = $schema->{properties}->{$arg_param};
+       die "expected list format $pd->{format}"
+           if !($pd && $pd->{format} && $pd->{format} =~ m/-list/);
+       $list_param = $arg_param;
+    }
+
     my @getopt = ();
     foreach my $prop (keys %{$schema->{properties}}) {
        my $pd = $schema->{properties}->{$prop};
        next if $list_param && $prop eq $list_param;
-       next if defined($uri_param->{$prop});
+       next if defined($fixed_param->{$prop});
 
        if ($prop eq 'password' && $pwcallback) {
            # we do not accept plain password on input line, instead
@@ -882,18 +897,23 @@ sub get_options {
     my $opts = {};
     raise("unable to parse option\n", code => HTTP_BAD_REQUEST)
        if !Getopt::Long::GetOptionsFromArray($args, $opts, @getopt);
-    
-    if ($list_param) {
-       my $pd = $schema->{properties}->{$list_param} ||
-           die "no schema for list_param";
 
-       $opts->{$list_param} = $args;
-       $args = [];
+    if (my $acount = scalar(@$args)) {
+       if ($list_param) {
+           $opts->{$list_param} = $args;
+           $args = [];
+       } elsif (ref($arg_param)) {
+           raise("wrong number of arguments\n", code => HTTP_BAD_REQUEST)
+               if scalar(@$arg_param) != $acount; 
+           foreach my $p (@$arg_param) {
+               $opts->{$p} = shift @$args;
+           }
+       } else {
+           raise("too many arguments\n", code => HTTP_BAD_REQUEST)
+               if scalar(@$args) != 0;
+       }
     }
 
-    raise("too many arguments\n", code => HTTP_BAD_REQUEST)
-       if scalar(@$args) != 0;
-
     if (my $pd = $schema->{properties}->{password}) {
        if ($pd->{type} ne 'boolean' && $pwcallback) {
            if ($opts->{password} || !$pd->{optional}) {
@@ -901,7 +921,12 @@ sub get_options {
            }
        }
     }
-    
+
+    $opts = PVE::Tools::decode_utf8_parameters($opts);
+    if ($opts->{description}) {
+       print "TEST: " . PVE::Tools::encode_text($opts->{description}) . "\n";
+    }
+
     foreach my $p (keys %$opts) {
        if (my $pd = $schema->{properties}->{$p}) {
            if ($pd->{type} eq 'boolean') {
@@ -935,8 +960,8 @@ sub get_options {
        }       
     }
 
-    foreach my $p (keys %$uri_param) {
-       $opts->{$p} = $uri_param->{$p};
+    foreach my $p (keys %$fixed_param) {
+       $opts->{$p} = $fixed_param->{$p};
     }
 
     return $opts;