From: Lukas Wagner <l.wagner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH v3 proxmox 3/6] ldap: add helpers for constructing LDAP filters
Date: Tue, 24 Jan 2023 11:03:34 +0100 [thread overview]
Message-ID: <20230124100337.152394-4-l.wagner@proxmox.com> (raw)
In-Reply-To: <20230124100337.152394-1-l.wagner@proxmox.com>
Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
proxmox-ldap/src/lib.rs | 79 +++++++++++++++++++++++++++++++++++++++--
1 file changed, 76 insertions(+), 3 deletions(-)
diff --git a/proxmox-ldap/src/lib.rs b/proxmox-ldap/src/lib.rs
index 48eb863..ac164db 100644
--- a/proxmox-ldap/src/lib.rs
+++ b/proxmox-ldap/src/lib.rs
@@ -1,13 +1,12 @@
use std::{
+ fmt::{Display, Formatter},
fs,
path::{Path, PathBuf},
time::Duration,
};
use anyhow::{bail, Error};
-use ldap3::{
- Ldap, LdapConnAsync, LdapConnSettings, LdapResult, Scope, SearchEntry,
-};
+use ldap3::{Ldap, LdapConnAsync, LdapConnSettings, LdapResult, Scope, SearchEntry};
use native_tls::{Certificate, TlsConnector, TlsConnectorBuilder};
use serde::{Deserialize, Serialize};
@@ -226,3 +225,77 @@ impl LdapConnection {
bail!("user not found")
}
}
+
+#[allow(dead_code)]
+enum FilterElement<'a> {
+ And(Vec<FilterElement<'a>>),
+ Or(Vec<FilterElement<'a>>),
+ Condition(&'a str, &'a str),
+ Not(Box<FilterElement<'a>>),
+ Verbatim(&'a str),
+}
+
+impl<'a> Display for FilterElement<'a> {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ fn write_children(f: &mut Formatter<'_>, children: &[FilterElement]) -> std::fmt::Result {
+ for child in children {
+ write!(f, "{child}")?;
+ }
+
+ Ok(())
+ }
+
+ match self {
+ FilterElement::And(children) => {
+ write!(f, "(&")?;
+ write_children(f, children)?;
+ write!(f, ")")?;
+ }
+ FilterElement::Or(children) => {
+ write!(f, "(|")?;
+ write_children(f, children)?;
+ write!(f, ")")?;
+ }
+ FilterElement::Not(element) => {
+ write!(f, "(!{})", element)?;
+ }
+ FilterElement::Condition(attr, value) => {
+ write!(f, "({attr}={value})")?;
+ }
+ FilterElement::Verbatim(verbatim) => write!(f, "{verbatim}")?,
+ }
+
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::FilterElement::*;
+
+ #[test]
+ fn test_filter_elements_to_string() {
+ assert_eq!("(uid=john)", Condition("uid", "john").to_string());
+ assert_eq!(
+ "(!(uid=john))",
+ Not(Box::new(Condition("uid", "john"))).to_string()
+ );
+
+ assert_eq!("(foo=bar)", &Verbatim("(foo=bar)").to_string());
+
+ let filter_string = And(vec![
+ Condition("givenname", "john"),
+ Condition("sn", "doe"),
+ Or(vec![
+ Condition("email", "john@foo"),
+ Condition("email", "john@bar"),
+ ]),
+ ])
+ .to_string();
+
+ assert_eq!(
+ "(&(givenname=john)(sn=doe)(|(email=john@foo)(email=john@bar)))",
+ &filter_string
+ );
+ }
+}
--
2.30.2
next prev parent reply other threads:[~2023-01-24 10:04 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-24 10:03 [pbs-devel] [PATCH v3 proxmox 0/6] introduce proxmox-ldap crate Lukas Wagner
2023-01-24 10:03 ` [pbs-devel] [PATCH v3 proxmox 1/6] ldap: create new `proxmox-ldap` crate Lukas Wagner
2023-01-24 10:03 ` [pbs-devel] [PATCH v3 proxmox 2/6] ldap: add basic user auth functionality Lukas Wagner
2023-01-24 10:03 ` Lukas Wagner [this message]
2023-01-24 10:03 ` [pbs-devel] [PATCH v3 proxmox 4/6] ldap: allow searching for LDAP entities Lukas Wagner
2023-01-24 10:03 ` [pbs-devel] [PATCH v3 proxmox 5/6] ldap: tests: add LDAP integration tests Lukas Wagner
2023-01-24 10:03 ` [pbs-devel] [PATCH v3 proxmox 6/6] ldap: add debian packaging Lukas Wagner
2023-02-08 13:32 ` [pbs-devel] applied-series: [PATCH v3 proxmox 0/6] introduce proxmox-ldap crate Wolfgang Bumiller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230124100337.152394-4-l.wagner@proxmox.com \
--to=l.wagner@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal