4 use std
::collections
::HashMap
;
6 use proxmox_router
::{cli::*, ApiHandler, RpcEnvironment}
;
7 use proxmox_schema
::api
;
9 use pbs_api_types
::{ACL_PATH_SCHEMA, Authid, Userid}
;
11 use proxmox_backup
::api2
;
13 fn render_expire(value
: &Value
, _record
: &Value
) -> Result
<String
, Error
> {
14 let never
= String
::from("never");
15 if value
.is_null() { return Ok(never); }
16 let text
= match value
.as_i64() {
17 Some(epoch
) if epoch
== 0 => never
,
19 if let Ok(epoch_string
) = proxmox_time
::strftime_local("%c", epoch
as i64) {
25 None
=> value
.to_string(),
34 schema
: OUTPUT_FORMAT
,
40 /// List configured users.
41 fn list_users(param
: Value
, rpcenv
: &mut dyn RpcEnvironment
) -> Result
<Value
, Error
> {
43 let output_format
= get_output_format(¶m
);
45 let info
= &api2
::access
::user
::API_METHOD_LIST_USERS
;
46 let mut data
= match info
.handler
{
47 ApiHandler
::Sync(handler
) => (handler
)(param
, info
, rpcenv
)?
,
51 let options
= default_table_format_options()
52 .column(ColumnConfig
::new("userid"))
54 ColumnConfig
::new("enable")
55 .renderer(pbs_tools
::format
::render_bool_with_default_true
)
58 ColumnConfig
::new("expire")
59 .renderer(render_expire
)
61 .column(ColumnConfig
::new("firstname"))
62 .column(ColumnConfig
::new("lastname"))
63 .column(ColumnConfig
::new("email"))
64 .column(ColumnConfig
::new("comment"));
66 format_and_print_result_full(&mut data
, &info
.returns
, &output_format
, &options
);
75 schema
: OUTPUT_FORMAT
,
84 /// List tokens associated with user.
85 fn list_tokens(param
: Value
, rpcenv
: &mut dyn RpcEnvironment
) -> Result
<Value
, Error
> {
87 let output_format
= get_output_format(¶m
);
89 let info
= &api2
::access
::user
::API_METHOD_LIST_TOKENS
;
90 let mut data
= match info
.handler
{
91 ApiHandler
::Sync(handler
) => (handler
)(param
, info
, rpcenv
)?
,
95 let options
= default_table_format_options()
96 .column(ColumnConfig
::new("tokenid"))
98 ColumnConfig
::new("enable")
99 .renderer(pbs_tools
::format
::render_bool_with_default_true
)
102 ColumnConfig
::new("expire")
103 .renderer(render_expire
)
105 .column(ColumnConfig
::new("comment"));
107 format_and_print_result_full(&mut data
, &info
.returns
, &output_format
, &options
);
117 schema
: OUTPUT_FORMAT
,
124 schema
: ACL_PATH_SCHEMA
,
130 /// List permissions of user/token.
131 fn list_permissions(param
: Value
, rpcenv
: &mut dyn RpcEnvironment
) -> Result
<Value
, Error
> {
133 let output_format
= get_output_format(¶m
);
135 let info
= &api2
::access
::API_METHOD_LIST_PERMISSIONS
;
136 let data
= match info
.handler
{
137 ApiHandler
::Sync(handler
) => (handler
)(param
, info
, rpcenv
)?
,
141 if output_format
== "text" {
142 println
!("Privileges with (*) have the propagate flag set\n");
143 let data
:HashMap
<String
, HashMap
<String
, bool
>> = serde_json
::from_value(data
)?
;
144 let mut paths
:Vec
<String
> = data
.keys().cloned().collect();
145 paths
.sort_unstable();
147 println
!("Path: {}", path
);
148 let priv_map
= data
.get(&path
).unwrap();
149 let mut privs
:Vec
<String
> = priv_map
.keys().cloned().collect();
150 if privs
.is_empty() {
151 println
!("- NoAccess");
153 privs
.sort_unstable();
154 for privilege
in privs
{
155 if *priv_map
.get(&privilege
).unwrap() {
156 println
!("- {} (*)", privilege
);
158 println
!("- {}", privilege
);
164 format_and_print_result(&data
, &output_format
);
171 pub fn user_commands() -> CommandLineInterface
{
173 let cmd_def
= CliCommandMap
::new()
174 .insert("list", CliCommand
::new(&API_METHOD_LIST_USERS
))
177 // fixme: howto handle password parameter?
178 CliCommand
::new(&api2
::access
::user
::API_METHOD_CREATE_USER
)
179 .arg_param(&["userid"])
183 CliCommand
::new(&api2
::access
::user
::API_METHOD_UPDATE_USER
)
184 .arg_param(&["userid"])
185 .completion_cb("userid", pbs_config
::user
::complete_userid
)
189 CliCommand
::new(&api2
::access
::user
::API_METHOD_DELETE_USER
)
190 .arg_param(&["userid"])
191 .completion_cb("userid", pbs_config
::user
::complete_userid
)
195 CliCommand
::new(&API_METHOD_LIST_TOKENS
)
196 .arg_param(&["userid"])
197 .completion_cb("userid", pbs_config
::user
::complete_userid
)
201 CliCommand
::new(&api2
::access
::user
::API_METHOD_GENERATE_TOKEN
)
202 .arg_param(&["userid", "token-name"])
203 .completion_cb("userid", pbs_config
::user
::complete_userid
)
207 CliCommand
::new(&api2
::access
::user
::API_METHOD_DELETE_TOKEN
)
208 .arg_param(&["userid", "token-name"])
209 .completion_cb("userid", pbs_config
::user
::complete_userid
)
210 .completion_cb("token-name", pbs_config
::user
::complete_token_name
)
214 CliCommand
::new(&API_METHOD_LIST_PERMISSIONS
)
215 .arg_param(&["auth-id"])
216 .completion_cb("auth-id", pbs_config
::user
::complete_authid
)
217 .completion_cb("path", pbs_config
::datastore
::complete_acl_path
)