2020-05-21 08:53:06 +00:00
|
|
|
use anyhow::Error;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
2020-10-16 09:18:02 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-05-21 08:53:06 +00:00
|
|
|
use proxmox::api::{api, cli::*, RpcEnvironment, ApiHandler};
|
|
|
|
|
|
|
|
use proxmox_backup::config;
|
|
|
|
use proxmox_backup::tools;
|
|
|
|
use proxmox_backup::api2;
|
2020-10-16 09:18:02 +00:00
|
|
|
use proxmox_backup::api2::types::{ACL_PATH_SCHEMA, Authid, Userid};
|
2020-05-21 08:53:06 +00:00
|
|
|
|
2021-01-14 12:59:08 +00:00
|
|
|
fn render_expire(value: &Value, _record: &Value) -> Result<String, Error> {
|
|
|
|
let never = String::from("never");
|
|
|
|
if value.is_null() { return Ok(never); }
|
|
|
|
let text = match value.as_i64() {
|
|
|
|
Some(epoch) if epoch == 0 => never,
|
|
|
|
Some(epoch) => {
|
|
|
|
if let Ok(epoch_string) = proxmox::tools::time::strftime_local("%c", epoch as i64) {
|
|
|
|
epoch_string
|
|
|
|
} else {
|
|
|
|
epoch.to_string()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => value.to_string(),
|
|
|
|
};
|
|
|
|
Ok(text)
|
|
|
|
}
|
|
|
|
|
2020-05-21 08:53:06 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// List configured users.
|
|
|
|
fn list_users(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
|
|
|
|
let info = &api2::access::user::API_METHOD_LIST_USERS;
|
|
|
|
let mut data = match info.handler {
|
|
|
|
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let options = default_table_format_options()
|
|
|
|
.column(ColumnConfig::new("userid"))
|
|
|
|
.column(
|
|
|
|
ColumnConfig::new("enable")
|
|
|
|
.renderer(tools::format::render_bool_with_default_true)
|
|
|
|
)
|
|
|
|
.column(
|
|
|
|
ColumnConfig::new("expire")
|
2021-01-14 12:59:08 +00:00
|
|
|
.renderer(render_expire)
|
2020-05-21 08:53:06 +00:00
|
|
|
)
|
|
|
|
.column(ColumnConfig::new("firstname"))
|
|
|
|
.column(ColumnConfig::new("lastname"))
|
|
|
|
.column(ColumnConfig::new("email"))
|
|
|
|
.column(ColumnConfig::new("comment"));
|
|
|
|
|
2020-12-18 11:26:07 +00:00
|
|
|
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
|
2020-05-21 08:53:06 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:49:04 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
userid: {
|
|
|
|
type: Userid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// List tokens associated with user.
|
|
|
|
fn list_tokens(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
|
|
|
|
let info = &api2::access::user::API_METHOD_LIST_TOKENS;
|
|
|
|
let mut data = match info.handler {
|
|
|
|
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let options = default_table_format_options()
|
|
|
|
.column(ColumnConfig::new("tokenid"))
|
|
|
|
.column(
|
|
|
|
ColumnConfig::new("enable")
|
|
|
|
.renderer(tools::format::render_bool_with_default_true)
|
|
|
|
)
|
|
|
|
.column(
|
|
|
|
ColumnConfig::new("expire")
|
2021-01-14 12:59:08 +00:00
|
|
|
.renderer(render_expire)
|
2020-10-15 12:49:04 +00:00
|
|
|
)
|
|
|
|
.column(ColumnConfig::new("comment"));
|
|
|
|
|
2020-12-18 11:26:07 +00:00
|
|
|
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
|
2020-10-15 12:49:04 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-16 09:18:02 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
2020-10-30 14:18:41 +00:00
|
|
|
"auth-id": {
|
2020-10-16 09:18:02 +00:00
|
|
|
type: Authid,
|
|
|
|
},
|
|
|
|
path: {
|
|
|
|
schema: ACL_PATH_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)]
|
|
|
|
/// List permissions of user/token.
|
|
|
|
fn list_permissions(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
|
|
|
|
let info = &api2::access::API_METHOD_LIST_PERMISSIONS;
|
2021-01-25 13:43:00 +00:00
|
|
|
let data = match info.handler {
|
2020-10-16 09:18:02 +00:00
|
|
|
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if output_format == "text" {
|
|
|
|
println!("Privileges with (*) have the propagate flag set\n");
|
|
|
|
let data:HashMap<String, HashMap<String, bool>> = serde_json::from_value(data)?;
|
|
|
|
let mut paths:Vec<String> = data.keys().cloned().collect();
|
|
|
|
paths.sort_unstable();
|
|
|
|
for path in paths {
|
|
|
|
println!("Path: {}", path);
|
|
|
|
let priv_map = data.get(&path).unwrap();
|
|
|
|
let mut privs:Vec<String> = priv_map.keys().cloned().collect();
|
|
|
|
if privs.is_empty() {
|
|
|
|
println!("- NoAccess");
|
|
|
|
} else {
|
|
|
|
privs.sort_unstable();
|
|
|
|
for privilege in privs {
|
|
|
|
if *priv_map.get(&privilege).unwrap() {
|
|
|
|
println!("- {} (*)", privilege);
|
|
|
|
} else {
|
|
|
|
println!("- {}", privilege);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-25 13:43:00 +00:00
|
|
|
format_and_print_result(&data, &output_format);
|
2020-10-16 09:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-21 08:53:06 +00:00
|
|
|
pub fn user_commands() -> CommandLineInterface {
|
|
|
|
|
|
|
|
let cmd_def = CliCommandMap::new()
|
|
|
|
.insert("list", CliCommand::new(&&API_METHOD_LIST_USERS))
|
|
|
|
.insert(
|
|
|
|
"create",
|
|
|
|
// fixme: howto handle password parameter?
|
|
|
|
CliCommand::new(&api2::access::user::API_METHOD_CREATE_USER)
|
|
|
|
.arg_param(&["userid"])
|
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
"update",
|
|
|
|
CliCommand::new(&api2::access::user::API_METHOD_UPDATE_USER)
|
|
|
|
.arg_param(&["userid"])
|
2020-10-23 11:33:21 +00:00
|
|
|
.completion_cb("userid", config::user::complete_userid)
|
2020-05-21 08:53:06 +00:00
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
"remove",
|
|
|
|
CliCommand::new(&api2::access::user::API_METHOD_DELETE_USER)
|
|
|
|
.arg_param(&["userid"])
|
2020-10-23 11:33:21 +00:00
|
|
|
.completion_cb("userid", config::user::complete_userid)
|
2020-10-15 12:49:04 +00:00
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
"list-tokens",
|
|
|
|
CliCommand::new(&&API_METHOD_LIST_TOKENS)
|
|
|
|
.arg_param(&["userid"])
|
|
|
|
.completion_cb("userid", config::user::complete_userid)
|
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
"generate-token",
|
|
|
|
CliCommand::new(&api2::access::user::API_METHOD_GENERATE_TOKEN)
|
|
|
|
.arg_param(&["userid", "tokenname"])
|
|
|
|
.completion_cb("userid", config::user::complete_userid)
|
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
"delete-token",
|
|
|
|
CliCommand::new(&api2::access::user::API_METHOD_DELETE_TOKEN)
|
|
|
|
.arg_param(&["userid", "tokenname"])
|
|
|
|
.completion_cb("userid", config::user::complete_userid)
|
|
|
|
.completion_cb("tokenname", config::user::complete_token_name)
|
2020-10-16 09:18:02 +00:00
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
"permissions",
|
|
|
|
CliCommand::new(&&API_METHOD_LIST_PERMISSIONS)
|
2020-10-30 14:18:41 +00:00
|
|
|
.arg_param(&["auth-id"])
|
|
|
|
.completion_cb("auth-id", config::user::complete_authid)
|
2020-10-16 09:18:02 +00:00
|
|
|
.completion_cb("path", config::datastore::complete_acl_path)
|
2020-05-21 08:53:06 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
cmd_def.into()
|
|
|
|
}
|