Misc 6 #77

Merged
oysteikt merged 9 commits from misc into main 2024-08-19 18:14:14 +02:00
3 changed files with 31 additions and 2 deletions
Showing only changes of commit b21aa0eece - Show all commits

1
Cargo.lock generated
View File

@ -1077,6 +1077,7 @@ dependencies = [
"prettytable", "prettytable",
"rand", "rand",
"ratatui", "ratatui",
"regex",
"sd-notify", "sd-notify",
"serde", "serde",
"serde_json", "serde_json",

View File

@ -49,3 +49,6 @@ codegen-units = 1
[build-dependencies] [build-dependencies]
anyhow = "1.0.82" anyhow = "1.0.82"
[dev-dependencies]
regex = "1.10.6"

View File

@ -5,9 +5,9 @@ use sqlx::prelude::*;
/// that belong to the user or any of the user's groups. /// that belong to the user or any of the user's groups.
pub fn create_user_group_matching_regex(user: &UnixUser) -> String { pub fn create_user_group_matching_regex(user: &UnixUser) -> String {
if user.groups.is_empty() { if user.groups.is_empty() {
format!("{}(_.+)?", user.username) format!("{}_.+", user.username)
} else { } else {
format!("({}|{})(_.+)?", user.username, user.groups.join("|")) format!("({}|{})_.+", user.username, user.groups.join("|"))
} }
} }
@ -24,3 +24,28 @@ pub fn try_get_with_binary_fallback(
.map(|v| String::from_utf8_lossy(&v).to_string()) .map(|v| String::from_utf8_lossy(&v).to_string())
}) })
} }
#[cfg(test)]
mod tests {
use super::*;
use regex::Regex;
#[test]
fn test_create_user_group_matching_regex() {
let user = UnixUser {
username: "user".to_owned(),
groups: vec!["group1".to_owned(), "group2".to_owned()],
};
let regex = create_user_group_matching_regex(&user);
let re = Regex::new(&regex).unwrap();
assert!(re.is_match("user_something"));
assert!(re.is_match("group1_something"));
assert!(re.is_match("group2_something"));
assert!(!re.is_match("other_something"));
assert!(!re.is_match("user"));
assert!(!re.is_match("usersomething"));
}
}