Align privilege tables in edit-db-perm and show-db-perm, fixes #5
This commit is contained in:
parent
4a94265eae
commit
44e23413ff
|
@ -255,17 +255,17 @@ async fn show_databases(
|
||||||
table.add_row(row![
|
table.add_row(row![
|
||||||
row.db,
|
row.db,
|
||||||
row.user,
|
row.user,
|
||||||
row.select_priv,
|
c->yn(row.select_priv),
|
||||||
row.insert_priv,
|
c->yn(row.insert_priv),
|
||||||
row.update_priv,
|
c->yn(row.update_priv),
|
||||||
row.delete_priv,
|
c->yn(row.delete_priv),
|
||||||
row.create_priv,
|
c->yn(row.create_priv),
|
||||||
row.drop_priv,
|
c->yn(row.drop_priv),
|
||||||
row.alter_priv,
|
c->yn(row.alter_priv),
|
||||||
row.index_priv,
|
c->yn(row.index_priv),
|
||||||
row.create_tmp_table_priv,
|
c->yn(row.create_tmp_table_priv),
|
||||||
row.lock_tables_priv,
|
c->yn(row.lock_tables_priv),
|
||||||
row.references_priv
|
c->yn(row.references_priv),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
table.printstd();
|
table.printstd();
|
||||||
|
@ -385,23 +385,29 @@ fn parse_permission_data_from_editor(content: String) -> anyhow::Result<Vec<Data
|
||||||
.collect::<anyhow::Result<Vec<DatabasePrivileges>>>()
|
.collect::<anyhow::Result<Vec<DatabasePrivileges>>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn display_permissions_as_editor_line(privs: &DatabasePrivileges) -> String {
|
fn format_privileges_line(
|
||||||
vec![
|
privs: &DatabasePrivileges,
|
||||||
privs.db.as_str(),
|
username_len: usize,
|
||||||
privs.user.as_str(),
|
database_name_len: usize,
|
||||||
yn(privs.select_priv),
|
) -> String {
|
||||||
yn(privs.insert_priv),
|
// Format a privileges line by padding each value with spaces
|
||||||
yn(privs.update_priv),
|
// The first two fields are padded to the length of the longest username and database name
|
||||||
yn(privs.delete_priv),
|
// The remaining fields are padded to the length of the corresponding field name
|
||||||
yn(privs.create_priv),
|
|
||||||
yn(privs.drop_priv),
|
DATABASE_PRIVILEGE_FIELDS
|
||||||
yn(privs.alter_priv),
|
.into_iter()
|
||||||
yn(privs.index_priv),
|
.map(|field| match field {
|
||||||
yn(privs.create_tmp_table_priv),
|
"db" => format!("{:width$}", privs.db, width = database_name_len),
|
||||||
yn(privs.lock_tables_priv),
|
"user" => format!("{:width$}", privs.user, width = username_len),
|
||||||
yn(privs.references_priv),
|
privilege => format!(
|
||||||
]
|
"{:width$}",
|
||||||
.join("\t")
|
yn(privs.get_privilege_by_name(privilege)),
|
||||||
|
width = db_priv_field_human_readable_name(privilege).len()
|
||||||
|
),
|
||||||
|
})
|
||||||
|
.join(" ")
|
||||||
|
.trim()
|
||||||
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn edit_permissions(
|
async fn edit_permissions(
|
||||||
|
@ -441,34 +447,65 @@ async fn edit_permissions(
|
||||||
#
|
#
|
||||||
# Lines starting with '#' are comments and will be ignored.
|
# Lines starting with '#' are comments and will be ignored.
|
||||||
"#};
|
"#};
|
||||||
let header = DATABASE_PRIVILEGE_FIELDS
|
|
||||||
.map(db_priv_field_human_readable_name)
|
|
||||||
.join("\t");
|
|
||||||
let example_line = {
|
|
||||||
let unix_user = get_current_unix_user()?;
|
|
||||||
let permissions_bools = [
|
|
||||||
true, true, true, true, false, false, false, false, false, false, false,
|
|
||||||
]
|
|
||||||
.map(yn)
|
|
||||||
.join("\t");
|
|
||||||
|
|
||||||
format!(
|
let unix_user = get_current_unix_user()?;
|
||||||
"# {}_db\t{}_user\t{}",
|
let example_user = format!("{}_user", unix_user.name);
|
||||||
unix_user.name, unix_user.name, permissions_bools
|
let example_db = format!("{}_db", unix_user.name);
|
||||||
)
|
|
||||||
};
|
let longest_username = permission_data
|
||||||
|
.iter()
|
||||||
|
.map(|p| p.user.len())
|
||||||
|
.max()
|
||||||
|
.unwrap_or(example_user.len());
|
||||||
|
|
||||||
|
let longest_database_name = permission_data
|
||||||
|
.iter()
|
||||||
|
.map(|p| p.db.len())
|
||||||
|
.max()
|
||||||
|
.unwrap_or(example_db.len());
|
||||||
|
|
||||||
|
let mut header: Vec<_> = DATABASE_PRIVILEGE_FIELDS
|
||||||
|
.into_iter()
|
||||||
|
.map(db_priv_field_human_readable_name)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// Pad the first two columns with spaces to align the permissions.
|
||||||
|
header[0] = format!("{:width$}", header[0], width = longest_database_name);
|
||||||
|
header[1] = format!("{:width$}", header[1], width = longest_username);
|
||||||
|
|
||||||
|
let example_line = format_privileges_line(
|
||||||
|
&DatabasePrivileges {
|
||||||
|
db: example_db,
|
||||||
|
user: example_user,
|
||||||
|
select_priv: true,
|
||||||
|
insert_priv: true,
|
||||||
|
update_priv: true,
|
||||||
|
delete_priv: true,
|
||||||
|
create_priv: false,
|
||||||
|
drop_priv: false,
|
||||||
|
alter_priv: false,
|
||||||
|
index_priv: false,
|
||||||
|
create_tmp_table_priv: false,
|
||||||
|
lock_tables_priv: false,
|
||||||
|
references_priv: false,
|
||||||
|
},
|
||||||
|
longest_username,
|
||||||
|
longest_database_name,
|
||||||
|
);
|
||||||
|
|
||||||
let result = edit::edit_with_builder(
|
let result = edit::edit_with_builder(
|
||||||
format!(
|
format!(
|
||||||
"{}\n{}\n{}",
|
"{}\n{}\n{}",
|
||||||
comment,
|
comment,
|
||||||
header,
|
header.join(" "),
|
||||||
if permission_data.is_empty() {
|
if permission_data.is_empty() {
|
||||||
example_line
|
format!("# {}", example_line)
|
||||||
} else {
|
} else {
|
||||||
permission_data
|
permission_data
|
||||||
.iter()
|
.iter()
|
||||||
.map(display_permissions_as_editor_line)
|
.map(|perm| {
|
||||||
|
format_privileges_line(perm, longest_username, longest_database_name)
|
||||||
|
})
|
||||||
.join("\n")
|
.join("\n")
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in New Issue