Skip to content

Commit 86b4f71

Browse files
committed
Add "modify" option to add, remove and replace attributes
1 parent edcf933 commit 86b4f71

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,15 @@ connection.add(distinguishedName: "CN=judy,CN=User,DC=perfect,DC=com", attribute
237237
Function `LDAP.modify()` can modify attributes from a specific DN with parameters below:
238238
- distinguishedName: String, specific DN
239239
- attributes:[String:[String]], attributes as an dictionary to modify. In this dictionary, every attribute, as a unique key in the dictionary, could have a series of values as an array.
240+
- method: specify if an attribute should be added, removed or replaced (default)
241+
- add: LDAP_MOD_ADD | LDAP_MOD_BVALUES
242+
- remove: LDAP_MOD_DELETE | LDAP_MOD_BVALUES
243+
- replace: LDAP_MOD_REPLACE | LDAP_MOD_BVALUES
240244

241245
Both asynchronous modify() and synchronous modify() share the same parameters above, take example:
242246

243247
``` swift
244-
// try an modify() synchronously.
248+
// try and modify() synchronously.
245249
do {
246250
try connection.modify(distinguishedName: "CN=judy,CN=User,DC=perfect,DC=com", attributes: ["codePage":["437"]])
247251
}catch (let err) {
@@ -254,6 +258,24 @@ connection.modify(distinguishedName: "CN=judy,CN=User,DC=perfect,DC=com", attrib
254258
}
255259
```
256260

261+
Example: Add and remove user from group
262+
263+
``` swift
264+
// add user to group
265+
do {
266+
try connection.modify(distinguishedName: "CN=employee_group,CN=Group,DC=perfect,DC=com", attributes: ["member":["CN=judy,CN=User,DC=perfect,DC=com"]], method: LDAP_MOD_ADD | LDAP_MOD_BVALUES)
267+
}catch (let err) {
268+
// failed for some reason
269+
}
270+
271+
// remove user from group
272+
do {
273+
try connection.modify(distinguishedName: "CN=employee_group,CN=Group,DC=perfect,DC=com", attributes: ["member":["CN=judy,CN=User,DC=perfect,DC=com"]], method: LDAP_MOD_DELETE | LDAP_MOD_BVALUES)
274+
}catch (let err) {
275+
// failed for some reason
276+
}
277+
```
278+
257279
### Delete Attributes (⚠️EXPERIMENTAL⚠️)
258280

259281
Function `LDAP.delete()` can delete attributes from a specific DN with only one parameter:

Sources/PerfectLDAP/PerfectLDAP.swift

+13-5
Original file line numberDiff line numberDiff line change
@@ -757,16 +757,20 @@ public class LDAP {
757757
/// - parameters:
758758
/// - distinguishedName: specific DN
759759
/// - attributes: attributes as an dictionary to modify
760+
/// - method: specify if an attribute should be added, removed or replaced (default)
761+
/// add: LDAP_MOD_ADD | LDAP_MOD_BVALUES
762+
/// remove: LDAP_MOD_DELETE | LDAP_MOD_BVALUES
763+
/// replace: LDAP_MOD_REPLACE | LDAP_MOD_BVALUES
760764
/// - throws:
761765
/// - Exception with message, such as no permission, or object class violation, etc.
762-
763-
public func modify(distinguishedName: String, attributes: [String:[String]]) throws {
766+
767+
public func modify(distinguishedName: String, attributes: [String:[String]], method: Int32 = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES) throws {
764768

765769
// map the keys to an array
766770
let keys:[String] = attributes.keys.map { $0 }
767771

768772
// map the key array to a modification array
769-
let mods:[LDAPMod] = keys.map { self.modAlloc(method: LDAP_MOD_REPLACE | LDAP_MOD_BVALUES, key: $0, values: attributes[$0]!)}
773+
let mods:[LDAPMod] = keys.map { self.modAlloc(method: method, key: $0, values: attributes[$0]!)}
770774

771775
// get the pointers
772776
let pMods = mods.asUnsafeNullTerminatedPointers()
@@ -787,12 +791,16 @@ public class LDAP {
787791
/// - distinguishedName: specific DN
788792
/// - attributes: attributes as an dictionary to modify
789793
/// - completion: callback once done. If something wrong, an error message will pass to the closure.
794+
/// - method: specify if an attribute should be added, removed or replaced (default)
795+
/// add: LDAP_MOD_ADD | LDAP_MOD_BVALUES
796+
/// remove: LDAP_MOD_DELETE | LDAP_MOD_BVALUES
797+
/// replace: LDAP_MOD_REPLACE | LDAP_MOD_BVALUES
790798

791-
public func modify(distinguishedName: String, attributes: [String:[String]],completion: @escaping (String?)-> Void) {
799+
public func modify(distinguishedName: String, attributes: [String:[String]],completion: @escaping (String?)-> Void, method: Int32 = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES) {
792800
threading.async {
793801
do {
794802
// perform adding
795-
try self.modify(distinguishedName: distinguishedName, attributes: attributes)
803+
try self.modify(distinguishedName: distinguishedName, attributes: attributes, method)
796804

797805
// if nothing wrong, callback
798806
completion(nil)

0 commit comments

Comments
 (0)