ldap-acis 0.2.1

LDAP Access Control Instructions (ACI) system built on acls-rs
Documentation
//! Builder pattern for constructing ACIs.

use super::{Aci, BindRule, Scope, TargetAttrFilter, TargetFilter};
use crate::operation::OperationType;

/// Builder for constructing ACIs.
#[derive(Debug)]
pub struct AciBuilder {
    name: String,
    target_dn: Option<String>,
    scope: Scope,
    target_filter: TargetFilter,
    target_attributes: Vec<String>,
    target_attributes_excluded: bool,
    target_attr_filters: Vec<TargetAttrFilter>,
    target_from: Option<String>,
    target_to: Option<String>,
    permissions: Vec<OperationType>,
    bind_rule: BindRule,
    grant: bool,
}

impl Default for AciBuilder {
    fn default() -> Self {
        Self {
            name: String::new(),
            target_dn: None,
            scope: Scope::Base,
            target_filter: TargetFilter::All,
            target_attributes: Vec::new(),
            target_attributes_excluded: false,
            target_attr_filters: Vec::new(),
            target_from: None,
            target_to: None,
            permissions: Vec::new(),
            bind_rule: BindRule::Authenticated,
            grant: true,
        }
    }
}

impl AciBuilder {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            scope: Scope::Base,
            target_filter: TargetFilter::All,
            bind_rule: BindRule::Authenticated,
            grant: true,
            ..Default::default()
        }
    }

    pub fn target_dn(mut self, dn: impl Into<String>) -> Self {
        self.target_dn = Some(dn.into());
        self
    }

    pub fn target_scope(mut self, scope: Scope) -> Self {
        self.scope = scope;
        self
    }

    pub fn target_filter(mut self, filter: TargetFilter) -> Self {
        self.target_filter = filter;
        self
    }

    pub fn target_attribute(mut self, attr: impl Into<String>) -> Self {
        self.target_attributes.push(attr.into());
        self
    }

    pub fn target_attributes(mut self, attrs: Vec<impl Into<String>>) -> Self {
        self.target_attributes
            .extend(attrs.into_iter().map(|a| a.into()));
        self
    }

    pub fn target_attributes_excluded(mut self, excluded: bool) -> Self {
        self.target_attributes_excluded = excluded;
        self
    }

    pub fn target_attr_filter(mut self, filter: TargetAttrFilter) -> Self {
        self.target_attr_filters.push(filter);
        self
    }

    pub fn target_attr_filters(mut self, filters: Vec<TargetAttrFilter>) -> Self {
        self.target_attr_filters.extend(filters);
        self
    }

    pub fn target_from(mut self, dn: impl Into<String>) -> Self {
        self.target_from = Some(dn.into());
        self
    }

    pub fn target_to(mut self, dn: impl Into<String>) -> Self {
        self.target_to = Some(dn.into());
        self
    }

    pub fn permission(mut self, perm: OperationType) -> Self {
        self.permissions.push(perm);
        self
    }

    pub fn permissions(mut self, perms: Vec<OperationType>) -> Self {
        self.permissions.extend(perms);
        self
    }

    pub fn bind_rule(mut self, rule: BindRule) -> Self {
        self.bind_rule = rule;
        self
    }

    pub fn grant(mut self, grant: bool) -> Self {
        self.grant = grant;
        self
    }

    pub fn deny(self) -> Self {
        self.grant(false)
    }

    pub fn build(self) -> Aci {
        Aci {
            name: self.name,
            target_dn: self.target_dn,
            scope: self.scope,
            target_filter: self.target_filter,
            target_attributes: self.target_attributes,
            target_attributes_excluded: self.target_attributes_excluded,
            target_attr_filters: self.target_attr_filters,
            target_from: self.target_from,
            target_to: self.target_to,
            permissions: self.permissions,
            bind_rule: self.bind_rule,
            grant: self.grant,
        }
    }
}