Don't validate prefixes and postfixes for db item ownership

This commit is contained in:
Oystein Kristoffer Tveit 2024-08-17 22:30:30 +02:00
parent 54a38255e6
commit f957bbd9da
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
3 changed files with 11 additions and 38 deletions

View File

@ -160,7 +160,9 @@ pub fn generate_editor_content_from_privilege_data(
database_name: Option<&str>, database_name: Option<&str>,
) -> String { ) -> String {
let example_user = format!("{}_user", unix_user); let example_user = format!("{}_user", unix_user);
let example_db = database_name.unwrap_or(&format!("{}_db", unix_user)).to_string(); let example_db = database_name
.unwrap_or(&format!("{}_db", unix_user))
.to_string();
// NOTE: `.max()`` fails when the iterator is empty. // NOTE: `.max()`` fails when the iterator is empty.
// In this case, we know that the only fields in the // In this case, we know that the only fields in the

View File

@ -95,8 +95,7 @@ impl OwnerValidationError {
.join("\n"), .join("\n"),
) )
.to_owned(), .to_owned(),
OwnerValidationError::StringEmpty => format!(
_ => format!(
"'{}' is not a valid {} name.", "'{}' is not a valid {} name.",
name, name,
db_or_user.lowercased() db_or_user.lowercased()
@ -113,12 +112,6 @@ pub enum OwnerValidationError {
// The name is empty, which is invalid // The name is empty, which is invalid
StringEmpty, StringEmpty,
// The name is in the format "_<postfix>", which is invalid
MissingPrefix,
// The name is in the format "<prefix>_", which is invalid
MissingPostfix,
} }
pub type CreateDatabasesOutput = BTreeMap<String, Result<(), CreateDatabaseError>>; pub type CreateDatabasesOutput = BTreeMap<String, Result<(), CreateDatabaseError>>;

View File

@ -43,18 +43,14 @@ pub fn validate_ownership_by_prefixes(
return Err(OwnerValidationError::StringEmpty); return Err(OwnerValidationError::StringEmpty);
} }
if name.starts_with('_') { if prefixes
return Err(OwnerValidationError::MissingPrefix); .iter()
} .filter(|p| name.starts_with(*p))
.collect::<Vec<_>>()
let (prefix, _) = match name.split_once('_') { .is_empty()
Some(pair) => pair, {
None => return Err(OwnerValidationError::MissingPostfix),
};
if !prefixes.iter().any(|g| g == prefix) {
return Err(OwnerValidationError::NoMatch); return Err(OwnerValidationError::NoMatch);
} };
Ok(()) Ok(())
} }
@ -115,24 +111,6 @@ mod tests {
Err(OwnerValidationError::StringEmpty) Err(OwnerValidationError::StringEmpty)
); );
assert_eq!(
validate_ownership_by_prefixes("user", &prefixes),
Err(OwnerValidationError::MissingPostfix)
);
assert_eq!(
validate_ownership_by_prefixes("something", &prefixes),
Err(OwnerValidationError::MissingPostfix)
);
assert_eq!(
validate_ownership_by_prefixes("user-testdb", &prefixes),
Err(OwnerValidationError::MissingPostfix)
);
assert_eq!(
validate_ownership_by_prefixes("_testdb", &prefixes),
Err(OwnerValidationError::MissingPrefix)
);
assert_eq!( assert_eq!(
validate_ownership_by_prefixes("user_testdb", &prefixes), validate_ownership_by_prefixes("user_testdb", &prefixes),
Ok(()) Ok(())