Skip to content

Commit

Permalink
Merge pull request #6 from Lagerregal/master
Browse files Browse the repository at this point in the history
Add "modify" option to add, remove and replace attributes
  • Loading branch information
RockfordWei authored May 21, 2020
2 parents edcf933 + 6b6ab7a commit e351ef5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,15 @@ connection.add(distinguishedName: "CN=judy,CN=User,DC=perfect,DC=com", attribute
Function `LDAP.modify()` can modify attributes from a specific DN with parameters below:
- distinguishedName: String, specific DN
- 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.
- method: specify if an attribute should be added, removed or replaced (default)
- add: LDAP_MOD_ADD | LDAP_MOD_BVALUES
- remove: LDAP_MOD_DELETE | LDAP_MOD_BVALUES
- replace: LDAP_MOD_REPLACE | LDAP_MOD_BVALUES

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

``` swift
// try an modify() synchronously.
// try and modify() synchronously.
do {
try connection.modify(distinguishedName: "CN=judy,CN=User,DC=perfect,DC=com", attributes: ["codePage":["437"]])
}catch (let err) {
Expand All @@ -254,6 +258,24 @@ connection.modify(distinguishedName: "CN=judy,CN=User,DC=perfect,DC=com", attrib
}
```

Example: Add and remove user from group

``` swift
// add user to group
do {
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)
}catch (let err) {
// failed for some reason
}

// remove user from group
do {
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)
}catch (let err) {
// failed for some reason
}
```

### Delete Attributes (⚠️EXPERIMENTAL⚠️)

Function `LDAP.delete()` can delete attributes from a specific DN with only one parameter:
Expand Down
18 changes: 13 additions & 5 deletions Sources/PerfectLDAP/PerfectLDAP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -757,16 +757,20 @@ public class LDAP {
/// - parameters:
/// - distinguishedName: specific DN
/// - attributes: attributes as an dictionary to modify
/// - method: specify if an attribute should be added, removed or replaced (default)
/// add: LDAP_MOD_ADD | LDAP_MOD_BVALUES
/// remove: LDAP_MOD_DELETE | LDAP_MOD_BVALUES
/// replace: LDAP_MOD_REPLACE | LDAP_MOD_BVALUES
/// - throws:
/// - Exception with message, such as no permission, or object class violation, etc.
public func modify(distinguishedName: String, attributes: [String:[String]]) throws {

public func modify(distinguishedName: String, attributes: [String:[String]], method: Int32 = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES) throws {

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

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

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

public func modify(distinguishedName: String, attributes: [String:[String]],completion: @escaping (String?)-> Void) {
public func modify(distinguishedName: String, attributes: [String:[String]],completion: @escaping (String?)-> Void, method: Int32 = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES) {
threading.async {
do {
// perform adding
try self.modify(distinguishedName: distinguishedName, attributes: attributes)
try self.modify(distinguishedName: distinguishedName, attributes: attributes, method: method)

// if nothing wrong, callback
completion(nil)
Expand Down

1 comment on commit e351ef5

@sayerbartlett
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's great to see this functionality added to the master branch now!
I implemented basically the same thing a while back on a fork. Would it be appropriate to use an enum for the options instead?
E.g. sayerbartlett@2f7e400

Please sign in to comment.