1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//! LDAP request

use std::time::Duration;

use rasn_ldap::{ChangeOperation, ModifyRequestChanges};

use crate::{
    error::Error,
    filter::parse_filter,
    model::{SearchRequestDerefAliases, SearchRequestScope},
    Attribute,
};

/// LDAP search request builder
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SearchRequestBuilder {
    base_dn: String,
    scope: SearchRequestScope,
    deref_aliases: SearchRequestDerefAliases,
    size_limit: u32,
    time_limit: Duration,
    types_only: bool,
    filter: String,
    attributes: Vec<String>,
}

impl SearchRequestBuilder {
    pub(crate) fn new() -> Self {
        Self {
            base_dn: Default::default(),
            scope: SearchRequestScope::BaseObject,
            deref_aliases: SearchRequestDerefAliases::NeverDerefAliases,
            size_limit: 0,
            time_limit: Duration::default(),
            types_only: false,
            filter: Default::default(),
            attributes: Vec::new(),
        }
    }

    /// Set base DN
    pub fn base_dn<S: AsRef<str>>(mut self, base_dn: S) -> Self {
        self.base_dn = base_dn.as_ref().to_owned();
        self
    }

    /// Set search scope
    pub fn scope(mut self, scope: SearchRequestScope) -> Self {
        self.scope = scope;
        self
    }

    /// Set aliases dereference policy
    pub fn deref_aliases(mut self, deref_aliases: SearchRequestDerefAliases) -> Self {
        self.deref_aliases = deref_aliases;
        self
    }

    /// Set search size limit
    pub fn size_limit(mut self, size_limit: u32) -> Self {
        self.size_limit = size_limit;
        self
    }

    /// Set search time limit
    pub fn time_limit(mut self, time_limit: Duration) -> Self {
        self.time_limit = time_limit;
        self
    }

    /// Set flag indicating to only search types
    pub fn types_only(mut self, types_only: bool) -> Self {
        self.types_only = types_only;
        self
    }

    /// Set search filter
    pub fn filter<S: AsRef<str>>(mut self, filter: S) -> Self {
        self.filter = filter.as_ref().to_owned();
        self
    }

    /// Specify attributes to return
    pub fn attributes<I, S>(mut self, attributes: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        self.attributes
            .extend(attributes.into_iter().map(|a| a.as_ref().to_owned()));
        self
    }

    /// Add attribute to return
    pub fn attribute<S>(mut self, attribute: S) -> Self
    where
        S: AsRef<str>,
    {
        self.attributes.push(attribute.as_ref().to_owned());
        self
    }

    /// Create a search request
    pub fn build(self) -> Result<SearchRequest, Error> {
        Ok(SearchRequest(rasn_ldap::SearchRequest::new(
            self.base_dn.into(),
            self.scope,
            self.deref_aliases,
            self.size_limit,
            self.time_limit.as_secs() as u32,
            self.types_only,
            parse_filter(self.filter)?,
            self.attributes.into_iter().map(Into::into).collect(),
        )))
    }
}

/// Search request
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SearchRequest(pub(crate) rasn_ldap::SearchRequest);

impl SearchRequest {
    /// Create search request  builder
    pub fn builder() -> SearchRequestBuilder {
        SearchRequestBuilder::new()
    }

    /// Create search request to query root DSE object
    pub fn root_dse() -> Self {
        Self::builder().filter("(objectClass=*)").build().unwrap()
    }
}

impl From<SearchRequest> for rasn_ldap::SearchRequest {
    fn from(req: SearchRequest) -> Self {
        req.0
    }
}

/// Search request
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ModifyRequest(pub(crate) rasn_ldap::ModifyRequest);

impl ModifyRequest {
    /// Create modification request builder for a given object DN
    pub fn builder<S: AsRef<str>>(object: S) -> ModifyRequestBuilder {
        ModifyRequestBuilder::new(object)
    }
}

impl From<ModifyRequest> for rasn_ldap::ModifyRequest {
    fn from(req: ModifyRequest) -> Self {
        req.0
    }
}

/// LDAP search request builder
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ModifyRequestBuilder {
    object: String,
    operations: Vec<(ChangeOperation, Attribute)>,
}

impl ModifyRequestBuilder {
    pub(crate) fn new<S: AsRef<str>>(object: S) -> Self {
        Self {
            object: object.as_ref().to_owned(),
            operations: Vec::new(),
        }
    }

    /// Append add operation to the request builder
    pub fn add_op(mut self, attribute: Attribute) -> Self {
        self.operations.push((ChangeOperation::Add, attribute));
        self
    }

    /// Append delete operation to the request builder
    pub fn delete_op(mut self, attribute: Attribute) -> Self {
        self.operations.push((ChangeOperation::Delete, attribute));
        self
    }

    /// Append replace operation to the request builder
    pub fn replace_op(mut self, attribute: Attribute) -> Self {
        self.operations.push((ChangeOperation::Replace, attribute));
        self
    }

    /// Build the modification request
    pub fn build(self) -> ModifyRequest {
        let req = rasn_ldap::ModifyRequest {
            object: self.object.into_bytes().into(),
            changes: self
                .operations
                .into_iter()
                .map(|(operation, attribute)| ModifyRequestChanges {
                    operation,
                    modification: attribute.into(),
                })
                .collect(),
        };
        ModifyRequest(req)
    }
}