From 9d61acb27d11c5a892670ed9e803d3723635fa55 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 12 May 2016 09:12:14 +0200 Subject: Move User_LDAP to PSR-4 --- apps/user_ldap/appinfo/info.xml | 2 + apps/user_ldap/lib/Jobs/CleanUp.php | 231 +++++++++++++++++++++++ apps/user_ldap/lib/Mapping/AbstractMapping.php | 246 +++++++++++++++++++++++++ apps/user_ldap/lib/Mapping/GroupMapping.php | 39 ++++ apps/user_ldap/lib/Mapping/UserMapping.php | 39 ++++ apps/user_ldap/lib/jobs/cleanup.php | 231 ----------------------- apps/user_ldap/lib/mapping/abstractmapping.php | 246 ------------------------- apps/user_ldap/lib/mapping/groupmapping.php | 39 ---- apps/user_ldap/lib/mapping/usermapping.php | 39 ---- 9 files changed, 557 insertions(+), 555 deletions(-) create mode 100644 apps/user_ldap/lib/Jobs/CleanUp.php create mode 100644 apps/user_ldap/lib/Mapping/AbstractMapping.php create mode 100644 apps/user_ldap/lib/Mapping/GroupMapping.php create mode 100644 apps/user_ldap/lib/Mapping/UserMapping.php delete mode 100644 apps/user_ldap/lib/jobs/cleanup.php delete mode 100644 apps/user_ldap/lib/mapping/abstractmapping.php delete mode 100644 apps/user_ldap/lib/mapping/groupmapping.php delete mode 100644 apps/user_ldap/lib/mapping/usermapping.php diff --git a/apps/user_ldap/appinfo/info.xml b/apps/user_ldap/appinfo/info.xml index b84a33f75a4..9805996b56f 100644 --- a/apps/user_ldap/appinfo/info.xml +++ b/apps/user_ldap/appinfo/info.xml @@ -21,6 +21,8 @@ A user logs into ownCloud with their LDAP or AD credentials, and is granted acce + User_LDAP + OCA\user_ldap\lib\Jobs \OCA\User_LDAP\Jobs\CleanUp diff --git a/apps/user_ldap/lib/Jobs/CleanUp.php b/apps/user_ldap/lib/Jobs/CleanUp.php new file mode 100644 index 00000000000..c9f5f2021eb --- /dev/null +++ b/apps/user_ldap/lib/Jobs/CleanUp.php @@ -0,0 +1,231 @@ + + * @author Morris Jobke + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\User_LDAP\Jobs; + +use \OC\BackgroundJob\TimedJob; +use \OCA\user_ldap\User_LDAP; +use \OCA\user_ldap\User_Proxy; +use \OCA\user_ldap\lib\Helper; +use \OCA\user_ldap\lib\LDAP; +use \OCA\user_ldap\lib\user\DeletedUsersIndex; +use \OCA\User_LDAP\Mapping\UserMapping; + +/** + * Class CleanUp + * + * a Background job to clean up deleted users + * + * @package OCA\user_ldap\lib; + */ +class CleanUp extends TimedJob { + /** @var int $limit amount of users that should be checked per run */ + protected $limit = 50; + + /** @var int $defaultIntervalMin default interval in minutes */ + protected $defaultIntervalMin = 51; + + /** @var User_LDAP|User_Proxy $userBackend */ + protected $userBackend; + + /** @var \OCP\IConfig $ocConfig */ + protected $ocConfig; + + /** @var \OCP\IDBConnection $db */ + protected $db; + + /** @var Helper $ldapHelper */ + protected $ldapHelper; + + /** @var \OCA\User_LDAP\Mapping\UserMapping */ + protected $mapping; + + /** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */ + protected $dui; + + public function __construct() { + $minutes = \OC::$server->getConfig()->getSystemValue( + 'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); + $this->setInterval(intval($minutes) * 60); + } + + /** + * assigns the instances passed to run() to the class properties + * @param array $arguments + */ + public function setArguments($arguments) { + //Dependency Injection is not possible, because the constructor will + //only get values that are serialized to JSON. I.e. whatever we would + //pass in app.php we do add here, except something else is passed e.g. + //in tests. + + if(isset($arguments['helper'])) { + $this->ldapHelper = $arguments['helper']; + } else { + $this->ldapHelper = new Helper(); + } + + if(isset($arguments['ocConfig'])) { + $this->ocConfig = $arguments['ocConfig']; + } else { + $this->ocConfig = \OC::$server->getConfig(); + } + + if(isset($arguments['userBackend'])) { + $this->userBackend = $arguments['userBackend']; + } else { + $this->userBackend = new User_Proxy( + $this->ldapHelper->getServerConfigurationPrefixes(true), + new LDAP(), + $this->ocConfig + ); + } + + if(isset($arguments['db'])) { + $this->db = $arguments['db']; + } else { + $this->db = \OC::$server->getDatabaseConnection(); + } + + if(isset($arguments['mapping'])) { + $this->mapping = $arguments['mapping']; + } else { + $this->mapping = new UserMapping($this->db); + } + + if(isset($arguments['deletedUsersIndex'])) { + $this->dui = $arguments['deletedUsersIndex']; + } else { + $this->dui = new DeletedUsersIndex( + $this->ocConfig, $this->db, $this->mapping); + } + } + + /** + * makes the background job do its work + * @param array $argument + */ + public function run($argument) { + $this->setArguments($argument); + + if(!$this->isCleanUpAllowed()) { + return; + } + $users = $this->mapping->getList($this->getOffset(), $this->limit); + if(!is_array($users)) { + //something wrong? Let's start from the beginning next time and + //abort + $this->setOffset(true); + return; + } + $resetOffset = $this->isOffsetResetNecessary(count($users)); + $this->checkUsers($users); + $this->setOffset($resetOffset); + } + + /** + * checks whether next run should start at 0 again + * @param int $resultCount + * @return bool + */ + public function isOffsetResetNecessary($resultCount) { + return ($resultCount < $this->limit) ? true : false; + } + + /** + * checks whether cleaning up LDAP users is allowed + * @return bool + */ + public function isCleanUpAllowed() { + try { + if($this->ldapHelper->haveDisabledConfigurations()) { + return false; + } + } catch (\Exception $e) { + return false; + } + + $enabled = $this->isCleanUpEnabled(); + + return $enabled; + } + + /** + * checks whether clean up is enabled by configuration + * @return bool + */ + private function isCleanUpEnabled() { + return (bool)$this->ocConfig->getSystemValue( + 'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); + } + + /** + * checks users whether they are still existing + * @param array $users result from getMappedUsers() + */ + private function checkUsers(array $users) { + foreach($users as $user) { + $this->checkUser($user); + } + } + + /** + * checks whether a user is still existing in LDAP + * @param string[] $user + */ + private function checkUser(array $user) { + if($this->userBackend->userExistsOnLDAP($user['name'])) { + //still available, all good + + return; + } + + $this->dui->markUser($user['name']); + } + + /** + * gets the offset to fetch users from the mappings table + * @return int + */ + private function getOffset() { + return intval($this->ocConfig->getAppValue('user_ldap', 'cleanUpJobOffset', 0)); + } + + /** + * sets the new offset for the next run + * @param bool $reset whether the offset should be set to 0 + */ + public function setOffset($reset = false) { + $newOffset = $reset ? 0 : + $this->getOffset() + $this->limit; + $this->ocConfig->setAppValue('user_ldap', 'cleanUpJobOffset', $newOffset); + } + + /** + * returns the chunk size (limit in DB speak) + * @return int + */ + public function getChunkSize() { + return $this->limit; + } + +} diff --git a/apps/user_ldap/lib/Mapping/AbstractMapping.php b/apps/user_ldap/lib/Mapping/AbstractMapping.php new file mode 100644 index 00000000000..1c896a9bbf4 --- /dev/null +++ b/apps/user_ldap/lib/Mapping/AbstractMapping.php @@ -0,0 +1,246 @@ + + * @author Morris Jobke + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\User_LDAP\Mapping; + +/** +* Class AbstractMapping +* @package OCA\User_LDAP\Mapping +*/ +abstract class AbstractMapping { + /** + * @var \OCP\IDBConnection $dbc + */ + protected $dbc; + + /** + * returns the DB table name which holds the mappings + * @return string + */ + abstract protected function getTableName(); + + /** + * @param \OCP\IDBConnection $dbc + */ + public function __construct(\OCP\IDBConnection $dbc) { + $this->dbc = $dbc; + } + + /** + * checks whether a provided string represents an existing table col + * @param string $col + * @return bool + */ + public function isColNameValid($col) { + switch($col) { + case 'ldap_dn': + case 'owncloud_name': + case 'directory_uuid': + return true; + default: + return false; + } + } + + /** + * Gets the value of one column based on a provided value of another column + * @param string $fetchCol + * @param string $compareCol + * @param string $search + * @throws \Exception + * @return string|false + */ + protected function getXbyY($fetchCol, $compareCol, $search) { + if(!$this->isColNameValid($fetchCol)) { + //this is used internally only, but we don't want to risk + //having SQL injection at all. + throw new \Exception('Invalid Column Name'); + } + $query = $this->dbc->prepare(' + SELECT `' . $fetchCol . '` + FROM `'. $this->getTableName() .'` + WHERE `' . $compareCol . '` = ? + '); + + $res = $query->execute(array($search)); + if($res !== false) { + return $query->fetchColumn(); + } + + return false; + } + + /** + * Performs a DELETE or UPDATE query to the database. + * @param \Doctrine\DBAL\Driver\Statement $query + * @param array $parameters + * @return bool true if at least one row was modified, false otherwise + */ + protected function modify($query, $parameters) { + $result = $query->execute($parameters); + return ($result === true && $query->rowCount() > 0); + } + + /** + * Gets the LDAP DN based on the provided name. + * Replaces Access::ocname2dn + * @param string $name + * @return string|false + */ + public function getDNByName($name) { + return $this->getXbyY('ldap_dn', 'owncloud_name', $name); + } + + /** + * Updates the DN based on the given UUID + * @param string $fdn + * @param string $uuid + * @return bool + */ + public function setDNbyUUID($fdn, $uuid) { + $query = $this->dbc->prepare(' + UPDATE `' . $this->getTableName() . '` + SET `ldap_dn` = ? + WHERE `directory_uuid` = ? + '); + + return $this->modify($query, array($fdn, $uuid)); + } + + /** + * Gets the name based on the provided LDAP DN. + * @param string $fdn + * @return string|false + */ + public function getNameByDN($fdn) { + return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn); + } + + /** + * Searches mapped names by the giving string in the name column + * @param string $search + * @return string[] + */ + public function getNamesBySearch($search) { + $query = $this->dbc->prepare(' + SELECT `owncloud_name` + FROM `'. $this->getTableName() .'` + WHERE `owncloud_name` LIKE ? + '); + + $res = $query->execute(array($search)); + $names = array(); + if($res !== false) { + while($row = $query->fetch()) { + $names[] = $row['owncloud_name']; + } + } + return $names; + } + + /** + * Gets the name based on the provided LDAP UUID. + * @param string $uuid + * @return string|false + */ + public function getNameByUUID($uuid) { + return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid); + } + + /** + * Gets the UUID based on the provided LDAP DN + * @param string $dn + * @return false|string + * @throws \Exception + */ + public function getUUIDByDN($dn) { + return $this->getXbyY('directory_uuid', 'ldap_dn', $dn); + } + + /** + * gets a piece of the mapping list + * @param int $offset + * @param int $limit + * @return array + */ + public function getList($offset = null, $limit = null) { + $query = $this->dbc->prepare(' + SELECT + `ldap_dn` AS `dn`, + `owncloud_name` AS `name`, + `directory_uuid` AS `uuid` + FROM `' . $this->getTableName() . '`', + $limit, + $offset + ); + + $query->execute(); + return $query->fetchAll(); + } + + /** + * attempts to map the given entry + * @param string $fdn fully distinguished name (from LDAP) + * @param string $name + * @param string $uuid a unique identifier as used in LDAP + * @return bool + */ + public function map($fdn, $name, $uuid) { + $row = array( + 'ldap_dn' => $fdn, + 'owncloud_name' => $name, + 'directory_uuid' => $uuid + ); + + try { + $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); + // insertIfNotExist returns values as int + return (bool)$result; + } catch (\Exception $e) { + return false; + } + } + + /** + * removes a mapping based on the owncloud_name of the entry + * @param string $name + * @return bool + */ + public function unmap($name) { + $query = $this->dbc->prepare(' + DELETE FROM `'. $this->getTableName() .'` + WHERE `owncloud_name` = ?'); + + return $this->modify($query, array($name)); + } + + /** + * Truncate's the mapping table + * @return bool + */ + public function clear() { + $sql = $this->dbc + ->getDatabasePlatform() + ->getTruncateTableSQL('`' . $this->getTableName() . '`'); + return $this->dbc->prepare($sql)->execute(); + } +} diff --git a/apps/user_ldap/lib/Mapping/GroupMapping.php b/apps/user_ldap/lib/Mapping/GroupMapping.php new file mode 100644 index 00000000000..49bb41b8c76 --- /dev/null +++ b/apps/user_ldap/lib/Mapping/GroupMapping.php @@ -0,0 +1,39 @@ + + * @author Morris Jobke + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\User_LDAP\Mapping; + +/** +* Class UserMapping +* @package OCA\User_LDAP\Mapping +*/ +class GroupMapping extends AbstractMapping { + + /** + * returns the DB table name which holds the mappings + * @return string + */ + protected function getTableName() { + return '*PREFIX*ldap_group_mapping'; + } + +} diff --git a/apps/user_ldap/lib/Mapping/UserMapping.php b/apps/user_ldap/lib/Mapping/UserMapping.php new file mode 100644 index 00000000000..b39f738ea8c --- /dev/null +++ b/apps/user_ldap/lib/Mapping/UserMapping.php @@ -0,0 +1,39 @@ + + * @author Morris Jobke + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\User_LDAP\Mapping; + +/** +* Class UserMapping +* @package OCA\User_LDAP\Mapping +*/ +class UserMapping extends AbstractMapping { + + /** + * returns the DB table name which holds the mappings + * @return string + */ + protected function getTableName() { + return '*PREFIX*ldap_user_mapping'; + } + +} diff --git a/apps/user_ldap/lib/jobs/cleanup.php b/apps/user_ldap/lib/jobs/cleanup.php deleted file mode 100644 index c9f5f2021eb..00000000000 --- a/apps/user_ldap/lib/jobs/cleanup.php +++ /dev/null @@ -1,231 +0,0 @@ - - * @author Morris Jobke - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\User_LDAP\Jobs; - -use \OC\BackgroundJob\TimedJob; -use \OCA\user_ldap\User_LDAP; -use \OCA\user_ldap\User_Proxy; -use \OCA\user_ldap\lib\Helper; -use \OCA\user_ldap\lib\LDAP; -use \OCA\user_ldap\lib\user\DeletedUsersIndex; -use \OCA\User_LDAP\Mapping\UserMapping; - -/** - * Class CleanUp - * - * a Background job to clean up deleted users - * - * @package OCA\user_ldap\lib; - */ -class CleanUp extends TimedJob { - /** @var int $limit amount of users that should be checked per run */ - protected $limit = 50; - - /** @var int $defaultIntervalMin default interval in minutes */ - protected $defaultIntervalMin = 51; - - /** @var User_LDAP|User_Proxy $userBackend */ - protected $userBackend; - - /** @var \OCP\IConfig $ocConfig */ - protected $ocConfig; - - /** @var \OCP\IDBConnection $db */ - protected $db; - - /** @var Helper $ldapHelper */ - protected $ldapHelper; - - /** @var \OCA\User_LDAP\Mapping\UserMapping */ - protected $mapping; - - /** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */ - protected $dui; - - public function __construct() { - $minutes = \OC::$server->getConfig()->getSystemValue( - 'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); - $this->setInterval(intval($minutes) * 60); - } - - /** - * assigns the instances passed to run() to the class properties - * @param array $arguments - */ - public function setArguments($arguments) { - //Dependency Injection is not possible, because the constructor will - //only get values that are serialized to JSON. I.e. whatever we would - //pass in app.php we do add here, except something else is passed e.g. - //in tests. - - if(isset($arguments['helper'])) { - $this->ldapHelper = $arguments['helper']; - } else { - $this->ldapHelper = new Helper(); - } - - if(isset($arguments['ocConfig'])) { - $this->ocConfig = $arguments['ocConfig']; - } else { - $this->ocConfig = \OC::$server->getConfig(); - } - - if(isset($arguments['userBackend'])) { - $this->userBackend = $arguments['userBackend']; - } else { - $this->userBackend = new User_Proxy( - $this->ldapHelper->getServerConfigurationPrefixes(true), - new LDAP(), - $this->ocConfig - ); - } - - if(isset($arguments['db'])) { - $this->db = $arguments['db']; - } else { - $this->db = \OC::$server->getDatabaseConnection(); - } - - if(isset($arguments['mapping'])) { - $this->mapping = $arguments['mapping']; - } else { - $this->mapping = new UserMapping($this->db); - } - - if(isset($arguments['deletedUsersIndex'])) { - $this->dui = $arguments['deletedUsersIndex']; - } else { - $this->dui = new DeletedUsersIndex( - $this->ocConfig, $this->db, $this->mapping); - } - } - - /** - * makes the background job do its work - * @param array $argument - */ - public function run($argument) { - $this->setArguments($argument); - - if(!$this->isCleanUpAllowed()) { - return; - } - $users = $this->mapping->getList($this->getOffset(), $this->limit); - if(!is_array($users)) { - //something wrong? Let's start from the beginning next time and - //abort - $this->setOffset(true); - return; - } - $resetOffset = $this->isOffsetResetNecessary(count($users)); - $this->checkUsers($users); - $this->setOffset($resetOffset); - } - - /** - * checks whether next run should start at 0 again - * @param int $resultCount - * @return bool - */ - public function isOffsetResetNecessary($resultCount) { - return ($resultCount < $this->limit) ? true : false; - } - - /** - * checks whether cleaning up LDAP users is allowed - * @return bool - */ - public function isCleanUpAllowed() { - try { - if($this->ldapHelper->haveDisabledConfigurations()) { - return false; - } - } catch (\Exception $e) { - return false; - } - - $enabled = $this->isCleanUpEnabled(); - - return $enabled; - } - - /** - * checks whether clean up is enabled by configuration - * @return bool - */ - private function isCleanUpEnabled() { - return (bool)$this->ocConfig->getSystemValue( - 'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); - } - - /** - * checks users whether they are still existing - * @param array $users result from getMappedUsers() - */ - private function checkUsers(array $users) { - foreach($users as $user) { - $this->checkUser($user); - } - } - - /** - * checks whether a user is still existing in LDAP - * @param string[] $user - */ - private function checkUser(array $user) { - if($this->userBackend->userExistsOnLDAP($user['name'])) { - //still available, all good - - return; - } - - $this->dui->markUser($user['name']); - } - - /** - * gets the offset to fetch users from the mappings table - * @return int - */ - private function getOffset() { - return intval($this->ocConfig->getAppValue('user_ldap', 'cleanUpJobOffset', 0)); - } - - /** - * sets the new offset for the next run - * @param bool $reset whether the offset should be set to 0 - */ - public function setOffset($reset = false) { - $newOffset = $reset ? 0 : - $this->getOffset() + $this->limit; - $this->ocConfig->setAppValue('user_ldap', 'cleanUpJobOffset', $newOffset); - } - - /** - * returns the chunk size (limit in DB speak) - * @return int - */ - public function getChunkSize() { - return $this->limit; - } - -} diff --git a/apps/user_ldap/lib/mapping/abstractmapping.php b/apps/user_ldap/lib/mapping/abstractmapping.php deleted file mode 100644 index 1c896a9bbf4..00000000000 --- a/apps/user_ldap/lib/mapping/abstractmapping.php +++ /dev/null @@ -1,246 +0,0 @@ - - * @author Morris Jobke - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\User_LDAP\Mapping; - -/** -* Class AbstractMapping -* @package OCA\User_LDAP\Mapping -*/ -abstract class AbstractMapping { - /** - * @var \OCP\IDBConnection $dbc - */ - protected $dbc; - - /** - * returns the DB table name which holds the mappings - * @return string - */ - abstract protected function getTableName(); - - /** - * @param \OCP\IDBConnection $dbc - */ - public function __construct(\OCP\IDBConnection $dbc) { - $this->dbc = $dbc; - } - - /** - * checks whether a provided string represents an existing table col - * @param string $col - * @return bool - */ - public function isColNameValid($col) { - switch($col) { - case 'ldap_dn': - case 'owncloud_name': - case 'directory_uuid': - return true; - default: - return false; - } - } - - /** - * Gets the value of one column based on a provided value of another column - * @param string $fetchCol - * @param string $compareCol - * @param string $search - * @throws \Exception - * @return string|false - */ - protected function getXbyY($fetchCol, $compareCol, $search) { - if(!$this->isColNameValid($fetchCol)) { - //this is used internally only, but we don't want to risk - //having SQL injection at all. - throw new \Exception('Invalid Column Name'); - } - $query = $this->dbc->prepare(' - SELECT `' . $fetchCol . '` - FROM `'. $this->getTableName() .'` - WHERE `' . $compareCol . '` = ? - '); - - $res = $query->execute(array($search)); - if($res !== false) { - return $query->fetchColumn(); - } - - return false; - } - - /** - * Performs a DELETE or UPDATE query to the database. - * @param \Doctrine\DBAL\Driver\Statement $query - * @param array $parameters - * @return bool true if at least one row was modified, false otherwise - */ - protected function modify($query, $parameters) { - $result = $query->execute($parameters); - return ($result === true && $query->rowCount() > 0); - } - - /** - * Gets the LDAP DN based on the provided name. - * Replaces Access::ocname2dn - * @param string $name - * @return string|false - */ - public function getDNByName($name) { - return $this->getXbyY('ldap_dn', 'owncloud_name', $name); - } - - /** - * Updates the DN based on the given UUID - * @param string $fdn - * @param string $uuid - * @return bool - */ - public function setDNbyUUID($fdn, $uuid) { - $query = $this->dbc->prepare(' - UPDATE `' . $this->getTableName() . '` - SET `ldap_dn` = ? - WHERE `directory_uuid` = ? - '); - - return $this->modify($query, array($fdn, $uuid)); - } - - /** - * Gets the name based on the provided LDAP DN. - * @param string $fdn - * @return string|false - */ - public function getNameByDN($fdn) { - return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn); - } - - /** - * Searches mapped names by the giving string in the name column - * @param string $search - * @return string[] - */ - public function getNamesBySearch($search) { - $query = $this->dbc->prepare(' - SELECT `owncloud_name` - FROM `'. $this->getTableName() .'` - WHERE `owncloud_name` LIKE ? - '); - - $res = $query->execute(array($search)); - $names = array(); - if($res !== false) { - while($row = $query->fetch()) { - $names[] = $row['owncloud_name']; - } - } - return $names; - } - - /** - * Gets the name based on the provided LDAP UUID. - * @param string $uuid - * @return string|false - */ - public function getNameByUUID($uuid) { - return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid); - } - - /** - * Gets the UUID based on the provided LDAP DN - * @param string $dn - * @return false|string - * @throws \Exception - */ - public function getUUIDByDN($dn) { - return $this->getXbyY('directory_uuid', 'ldap_dn', $dn); - } - - /** - * gets a piece of the mapping list - * @param int $offset - * @param int $limit - * @return array - */ - public function getList($offset = null, $limit = null) { - $query = $this->dbc->prepare(' - SELECT - `ldap_dn` AS `dn`, - `owncloud_name` AS `name`, - `directory_uuid` AS `uuid` - FROM `' . $this->getTableName() . '`', - $limit, - $offset - ); - - $query->execute(); - return $query->fetchAll(); - } - - /** - * attempts to map the given entry - * @param string $fdn fully distinguished name (from LDAP) - * @param string $name - * @param string $uuid a unique identifier as used in LDAP - * @return bool - */ - public function map($fdn, $name, $uuid) { - $row = array( - 'ldap_dn' => $fdn, - 'owncloud_name' => $name, - 'directory_uuid' => $uuid - ); - - try { - $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); - // insertIfNotExist returns values as int - return (bool)$result; - } catch (\Exception $e) { - return false; - } - } - - /** - * removes a mapping based on the owncloud_name of the entry - * @param string $name - * @return bool - */ - public function unmap($name) { - $query = $this->dbc->prepare(' - DELETE FROM `'. $this->getTableName() .'` - WHERE `owncloud_name` = ?'); - - return $this->modify($query, array($name)); - } - - /** - * Truncate's the mapping table - * @return bool - */ - public function clear() { - $sql = $this->dbc - ->getDatabasePlatform() - ->getTruncateTableSQL('`' . $this->getTableName() . '`'); - return $this->dbc->prepare($sql)->execute(); - } -} diff --git a/apps/user_ldap/lib/mapping/groupmapping.php b/apps/user_ldap/lib/mapping/groupmapping.php deleted file mode 100644 index 49bb41b8c76..00000000000 --- a/apps/user_ldap/lib/mapping/groupmapping.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @author Morris Jobke - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\User_LDAP\Mapping; - -/** -* Class UserMapping -* @package OCA\User_LDAP\Mapping -*/ -class GroupMapping extends AbstractMapping { - - /** - * returns the DB table name which holds the mappings - * @return string - */ - protected function getTableName() { - return '*PREFIX*ldap_group_mapping'; - } - -} diff --git a/apps/user_ldap/lib/mapping/usermapping.php b/apps/user_ldap/lib/mapping/usermapping.php deleted file mode 100644 index b39f738ea8c..00000000000 --- a/apps/user_ldap/lib/mapping/usermapping.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @author Morris Jobke - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\User_LDAP\Mapping; - -/** -* Class UserMapping -* @package OCA\User_LDAP\Mapping -*/ -class UserMapping extends AbstractMapping { - - /** - * returns the DB table name which holds the mappings - * @return string - */ - protected function getTableName() { - return '*PREFIX*ldap_user_mapping'; - } - -} -- cgit v1.2.3