Skip to content

Commit

Permalink
Upgrade to Swift 4.0
Browse files Browse the repository at this point in the history
 Using a web site LDAP server to perform tests.
  • Loading branch information
RockfordWei committed Nov 23, 2017
1 parent 65d3756 commit ce93952
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 164 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/.build
/Packages
/*.xcodeproj
/*.resolved
/*.pins
/*.sh
59 changes: 0 additions & 59 deletions LICENSE.zh_CN

This file was deleted.

25 changes: 20 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "PerfectLDAP",
dependencies:[
.Package(url: "https://github.com/PerfectSideRepos/Perfect-ICONV.git", majorVersion: 1),
.Package(url:"https://github.com/PerfectlySoft/Perfect-Thread.git", majorVersion: 2),
.Package(url:"https://github.com/PerfectlySoft/Perfect-libSASL.git", majorVersion: 1),
.Package(url:"https://github.com/PerfectlySoft/Perfect-OpenLDAP.git", majorVersion: 1)
products: [
.library(
name: "PerfectLDAP",
targets: ["PerfectLDAP"]),
],
dependencies: [
.package(url: "https://github.com/PerfectSideRepos/Perfect-ICONV.git", from: "3.0.0"),
.package(url: "https://github.com/PerfectlySoft/Perfect-libSASL.git", from: "1.0.0"),
.package(url: "https://github.com/PerfectlySoft/Perfect-OpenLDAP.git", from: "1.0.0"),
],
targets: [
.target(
name: "PerfectLDAP",
dependencies: ["PerfectICONV"]),
.testTarget(
name: "PerfectLDAPTests",
dependencies: ["PerfectLDAP"]),
]
)
86 changes: 38 additions & 48 deletions Sources/PerfectLDAP.swift → Sources/PerfectLDAP/PerfectLDAP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ import SASL
/// C library of OpenLDAP
import OpenLDAP

/// Threading Library
import PerfectThread

/// Iconv
import PerfectICONV

import Dispatch

/// Perfect LDAP Module
public class LDAP {

Expand All @@ -37,6 +36,8 @@ public class LDAP {
case BASE = 0, SINGLE_LEVEL = 1, SUBTREE = 2, CHILDREN = 3, DEFAULT = -1
}//end

let threading = DispatchQueue(label: "ldap.thread.\(time(nil))")

/// Authentication Model
public enum AuthType:String {
/// username@domain & password
Expand Down Expand Up @@ -128,7 +129,6 @@ public class LDAP {
/// - errno: Int32, the error number return by most ldap_XXX functions
/// - returns:
/// a short text of explaination in English. *NOTE* string pointer of err2string is static so don't free it
@discardableResult
public static func error(_ errno: Int32) -> String {
return String(cString: ldap_err2string(errno))
}//end error
Expand Down Expand Up @@ -186,7 +186,7 @@ public class LDAP {
/// - returns:
/// encoded string
public func string(pstr: UnsafeMutablePointer<Int8>) -> String {
let ber = berval(bv_len: strlen(pstr), bv_val: pstr)
let ber = berval(bv_len: ber_len_t(strlen(pstr)), bv_val: pstr)
return self.string(ber: ber)
}//end ber

Expand Down Expand Up @@ -250,7 +250,7 @@ public class LDAP {
/// - info: login data
/// - throws:
/// Exception message
@discardableResult

public func login(info: Login?) throws {

// load login data
Expand All @@ -271,7 +271,7 @@ public class LDAP {
if inf.mechanism == .SIMPLE {
// simple auth just use password and binddn to login
cred.bv_val = ber_strdup(inf.password)
cred.bv_len = strlen(cred.bv_val)
cred.bv_len = ber_len_t(strlen(cred.bv_val))
r = ldap_sasl_bind_s(self.ldap, inf.binddn, nil, &cred, nil, nil, nil)
ber_memfree(cred.bv_val)
if r == 0 {
Expand Down Expand Up @@ -331,7 +331,7 @@ public class LDAP {
pcursor = pcursor?.successor()
}//end while
return 0
}, unsafeBitCast(UnsafeMutablePointer(mutating: &pinf), to: UnsafeMutableRawPointer.self))
}, UnsafeMutableRawPointer(UnsafeMutablePointer(mutating: &pinf)))

if r == 0 {
return
Expand All @@ -345,7 +345,7 @@ public class LDAP {
/// - info: login data
/// - completion: callback handler with a boolean parameter indicating whether login succeeded or not.
public func login(info: Login, completion: @escaping (String?)->Void) {
Threading.dispatch {
threading.async {
do {
try self.login(info: info)
completion(nil)
Expand Down Expand Up @@ -601,7 +601,6 @@ public class LDAP {
/// - sortedBy: an array of tuple, which tells each field to sort in what order
/// - returns:
/// the sorting language, as a string
@discardableResult
public static func sortingString( sortedBy: [(String, SortingOrder)] = [] ) -> String {
return sortedBy.reduce("") { previous, next in
let str = next.1 == .ASC ? next.0 : "-" + next.0
Expand All @@ -619,7 +618,6 @@ public class LDAP {
/// ResultSet. See ResultSet
/// - throws:
/// Exception.message
@discardableResult
public func search(base:String = "", filter:String = "(objectclass=*)", scope:Scope = .BASE, attributes: [String] = [], sortedBy: String = "") throws -> [String:[
String:Any]] {

Expand All @@ -629,14 +627,14 @@ public class LDAP {
var sortKeyList = UnsafeMutablePointer<UnsafeMutablePointer<LDAPSortKey>?>(bitPattern: 0)
let sortString = ber_strdup(sortedBy)
var r = ldap_create_sort_keylist(&sortKeyList, sortString)
ber_memfree(sortString)
if r != 0 {
defer { ber_memfree(sortString) }
guard r == 0 else {
throw Exception.message(LDAP.error(r))
}//end if

r = ldap_create_sort_control(self.ldap, sortKeyList, 0, &serverControl)
ldap_free_sort_keylist(sortKeyList)
if r != 0 {
defer { ldap_free_sort_keylist(sortKeyList) }
guard r == 0 else {
throw Exception.message(LDAP.error(r))
}//end if
}//end if
Expand All @@ -655,18 +653,17 @@ public class LDAP {
return result
}//end

// validate the query
guard r == 0 && msg != nil else {
throw Exception.message(LDAP.error(r))
}//next
if let m = msg {
// process the result set
let rs = ResultSet(ldap: self, chain: m)

// process the result set
let rs = ResultSet(ldap: self, chain: msg!)
// release the memory
ldap_msgfree(m)

// release the memory
ldap_msgfree(msg)
return rs.dictionary
}

return rs.dictionary
throw Exception.message(LDAP.error(r))
}//end search

/// asynchronized search
Expand All @@ -676,9 +673,9 @@ public class LDAP {
/// - scope: See Scope, BASE, SINGLE_LEVEL, SUBTREE or CHILDREN
/// - sortedBy: a sorting string, may be generated by LDAP.sortingString()
/// - completion: callback with a parameter of ResultSet, nil if failed
@discardableResult

public func search(base:String = "", filter:String = "(objectclass=*)", scope:Scope = .BASE, sortedBy: String = "", completion: @escaping ([String:[String:Any]])-> Void) {
Threading.dispatch {
threading.async {
var rs: [String:[String:Any]] = [:]
do {
rs = try self.search(base: base, filter: filter, scope: scope, sortedBy: sortedBy)
Expand All @@ -696,7 +693,6 @@ public class LDAP {
/// - values: attribute values as an array
/// - returns:
/// an LDAPMod structure
@discardableResult
internal func modAlloc(method: Int32, key: String, values: [String]) -> LDAPMod {
let pValues = values.map { self.string(str: $0) }
let pointers = pValues.asUnsafeNullTerminatedPointers()
Expand All @@ -709,7 +705,7 @@ public class LDAP {
/// - attributes: attributes as an dictionary to add
/// - throws:
/// - Exception with message, such as no permission, or object class violation, etc.
@discardableResult

public func add(distinguishedName: String, attributes: [String:[String]]) throws {

// map the keys to an array
Expand All @@ -727,22 +723,20 @@ public class LDAP {
// release memory
ldap_mods_free(pMods, 0)

if r == 0 {
return
guard r == 0 else {
throw Exception.message(LDAP.error(r))
}//end if

throw Exception.message(LDAP.error(r))
}//end func

/// add an attribute to a DN
/// - parameters:
/// - distinguishedName: specific DN
/// - attributes: attributes as an dictionary to add
/// - completion: callback once done. If something wrong, an error message will pass to the closure.
@discardableResult

public func add(distinguishedName: String, attributes: [String:[String]],completion: @escaping (String?)-> Void) {

Threading.dispatch {
threading.async {
do {
// perform adding
try self.add(distinguishedName: distinguishedName, attributes: attributes)
Expand All @@ -765,7 +759,7 @@ public class LDAP {
/// - attributes: attributes as an dictionary to modify
/// - throws:
/// - Exception with message, such as no permission, or object class violation, etc.
@discardableResult

public func modify(distinguishedName: String, attributes: [String:[String]]) throws {

// map the keys to an array
Expand All @@ -783,21 +777,19 @@ public class LDAP {
// release memory
ldap_mods_free(pMods, 0)

if r == 0 {
return
guard r == 0 else {
throw Exception.message(LDAP.error(r))
}//end if

throw Exception.message(LDAP.error(r))
}//end func

/// modify an attribute to a DN
/// - parameters:
/// - 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.
@discardableResult

public func modify(distinguishedName: String, attributes: [String:[String]],completion: @escaping (String?)-> Void) {
Threading.dispatch {
threading.async {
do {
// perform adding
try self.modify(distinguishedName: distinguishedName, attributes: attributes)
Expand All @@ -820,27 +812,25 @@ public class LDAP {
/// - attributes: attributes as an dictionary to delete
/// - throws:
/// - Exception with message, such as no permission, or object class violation, etc.
@discardableResult

public func delete(distinguishedName: String) throws {

// perform deletion
let r = ldap_delete_ext_s(self.ldap, distinguishedName, nil, nil)

if r == 0 {
return
guard r == 0 else {
throw Exception.message(LDAP.error(r))
}//end if

throw Exception.message(LDAP.error(r))
}

/// delete an attribute to a DN
/// - parameters:
/// - distinguishedName: specific DN
/// - attributes: attributes as an dictionary to delete
/// - completion: callback once done. If something wrong, an error message will pass to the closure.
@discardableResult

public func delete(distinguishedName: String, completion: @escaping (String?)-> Void) {
Threading.dispatch {
threading.async {
do {
// perform adding
try self.delete(distinguishedName: distinguishedName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ extension Array {
}//end map

let pointers = UnsafeMutablePointer<UnsafeMutablePointer<Element>?>.allocate(capacity: self.count + 1)
pointers.initialize(from: pArray)
for i in 0 ..< self.count {
pointers.advanced(by: i).pointee = pArray[i]
}
pointers.advanced(by: self.count).pointee = nil
return pointers
}//func
Expand Down
Loading

0 comments on commit ce93952

Please sign in to comment.