Browse Source
LDAP: move PHP LDAP functions calls to an LDAP Wrapper for better isolation and mock testing
remotes/origin/stable6
LDAP: move PHP LDAP functions calls to an LDAP Wrapper for better isolation and mock testing
remotes/origin/stable6
3 changed files with 147 additions and 11 deletions
-
22apps/user_ldap/lib/access.php
-
50apps/user_ldap/lib/backendbase.php
-
86apps/user_ldap/lib/ldap.php
@ -0,0 +1,50 @@ |
|||
<?php |
|||
|
|||
/** |
|||
* ownCloud – LDAP BackendBase |
|||
* |
|||
* @author Arthur Schiwon |
|||
* @copyright 2013 Arthur Schiwon blizzz@owncloud.com |
|||
* |
|||
* This library is free software; you can redistribute it and/or |
|||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
|||
* License as published by the Free Software Foundation; either |
|||
* version 3 of the License, or any later version. |
|||
* |
|||
* This library is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public |
|||
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
|||
* |
|||
*/ |
|||
|
|||
namespace OCA\user_ldap\lib; |
|||
|
|||
abstract class BackendBase { |
|||
protected $ldap; |
|||
|
|||
public function __construct() { |
|||
$this->ldap = new LDAP(); |
|||
} |
|||
|
|||
/** |
|||
* @brief sets the LDAP Wrapper to be used |
|||
* |
|||
* @param $ldapWrapper an instance of the Wrapper |
|||
* @return true on success, otherwise false |
|||
* |
|||
* The LDAP Wrapper must implement the PHP LDAP functions, which are used |
|||
* in the LDAP backend |
|||
*/ |
|||
public function setLDAPWrapper($ldapWrapper) { |
|||
if(is_object($ldapWrapper)) { |
|||
unset($this->ldap); |
|||
$this->ldap = $ldapWrapper; |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
} |
|||
@ -0,0 +1,86 @@ |
|||
<?php |
|||
|
|||
/** |
|||
* ownCloud – LDAP Wrapper |
|||
* |
|||
* @author Arthur Schiwon |
|||
* @copyright 2013 Arthur Schiwon blizzz@owncloud.com |
|||
* |
|||
* This library is free software; you can redistribute it and/or |
|||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
|||
* License as published by the Free Software Foundation; either |
|||
* version 3 of the License, or any later version. |
|||
* |
|||
* This library is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public |
|||
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
|||
* |
|||
*/ |
|||
|
|||
namespace OCA\user_ldap\lib; |
|||
|
|||
class LDAP { |
|||
protected $curFunc = ''; |
|||
protected $curArgs = array(); |
|||
|
|||
//Simple wrapper for the ldap functions
|
|||
public function __call($name, $arguments) { |
|||
$func = 'ldap_' . $name; |
|||
if(function_exists($func)) { |
|||
$this->preFunctionCall($func, $arguments); |
|||
$result = call_user_func_array($func, $arguments); |
|||
$this->postFunctionCall(); |
|||
return $result; |
|||
} |
|||
} |
|||
|
|||
public function control_paged_result_response($linkResource, $resultResource, &$cookie) { |
|||
$this->preFunctionCall('ldap_control_paged_result_response', |
|||
array($linkResource, $resultResource, $cookie)); |
|||
$result = ldap_control_paged_result_response( |
|||
$linkResource, $resultResource, $cookie); |
|||
$this->postFunctionCall(); |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
public function areLDAPFunctionsAvailable() { |
|||
return function_exists('ldap_connect'); |
|||
} |
|||
|
|||
public function hasPagedResultSupport() { |
|||
$hasSupport = function_exists('ldap_control_paged_result') |
|||
&& function_exists('ldap_control_paged_result_response'); |
|||
return $hasSupport; |
|||
} |
|||
|
|||
private function preFunctionCall($functionName, $args) { |
|||
$this->curFunc = $functionName; |
|||
$this->curArgs = $args; |
|||
} |
|||
|
|||
private function postFunctionCall() { |
|||
if(is_resource($this->curArgs[0])) { |
|||
$errorCode = ldap_errno($this->curArgs[0]); |
|||
$errorMsg = ldap_error($this->curArgs[0]); |
|||
if($errorCode !== 0) { |
|||
if($this->curFunc === 'ldap_sort' && $errorCode === -4) { |
|||
//You can safely ignore that decoding error.
|
|||
//… says https://bugs.php.net/bug.php?id=18023
|
|||
} else if($this->curFunc === 'ldap_get_entries' && $errorCode === -4) { |
|||
} else if ($errorCode === 32) { |
|||
//for now
|
|||
} else { |
|||
throw new \Exception('LDAP error '.$errorMsg.' (' .$errorCode.') after calling '.$this->curFunc.' with arguments '.print_r($this->curArgs, true)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
$this->curFunc = ''; |
|||
$this->curArgs = array(); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue