From a67e3e08e0fe40aba32badfa05a9ba90ce94e467 Mon Sep 17 00:00:00 2001 From: Jonas Sulzer Date: Thu, 27 Dec 2018 12:14:40 +0100 Subject: [PATCH 01/10] changing discouraged operators Signed-off-by: Jonas Sulzer --- lib/imap.php | 10 +++++----- lib/smb.php | 3 +-- lib/webdavauth.php | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/imap.php b/lib/imap.php index 046ba94..c162400 100644 --- a/lib/imap.php +++ b/lib/imap.php @@ -65,12 +65,12 @@ class OC_User_IMAP extends \OCA\user_external\Base { if(count($users) === 1) { $username = $uid; $uid = $users[0]; - // Check if we only want logins from ONE domain and strip the domain part from UID - }elseif($this->domain != '') { + // Check if we only want logins from ONE domain and strip the domain part from UID + }elseif($this->domain !== '') { $pieces = explode('@', $uid); - if(count($pieces) == 1) { + if(count($pieces) === 1) { $username = $uid . "@" . $this->domain; - }elseif((count($pieces) == 2) and ($pieces[1] == $this->domain)) { + }elseif((count($pieces) === 2) and ($pieces[1] === $this->domain)) { $username = $uid; $uid = $pieces[0]; }else{ @@ -79,7 +79,7 @@ class OC_User_IMAP extends \OCA\user_external\Base { }else{ $username = $uid; } - + $mbox = @imap_open($this->mailbox, $username, $password, OP_HALFOPEN, 1); imap_errors(); imap_alerts(); diff --git a/lib/smb.php b/lib/smb.php index f2595a1..7d16d18 100644 --- a/lib/smb.php +++ b/lib/smb.php @@ -53,7 +53,7 @@ class OC_User_SMB extends \OCA\user_external\Base{ } else if (strpos($lastline, 'NT_STATUS_BAD_NETWORK_NAME') !== false) { //login on minor error goto login; - } else if ($retval != 0) { + } else if ($retval !== 0) { //some other error OCP\Util::writeLog( 'user_external', 'ERROR: smbclient error: ' . trim($lastline), @@ -91,4 +91,3 @@ class OC_User_SMB extends \OCA\user_external\Base{ return false; } } - diff --git a/lib/webdavauth.php b/lib/webdavauth.php index 65ea7b7..a77adfe 100644 --- a/lib/webdavauth.php +++ b/lib/webdavauth.php @@ -34,7 +34,7 @@ class WebDavAuth extends Base { list($protocol, $path) = $arr; $url= $protocol.'://'.urlencode($uid).':'.urlencode($password).'@'.$path; $headers = get_headers($url); - if($headers==false) { + if($headers === false) { \OCP\Util::writeLog('OC_USER_WEBDAVAUTH', 'Not possible to connect to WebDAV Url: "'.$protocol.'://'.$path.'" ', 3); return false; From db5ab31ee42bb8f5a8ef58bd5b035b154b2bb4cb Mon Sep 17 00:00:00 2001 From: Jonas Sulzer Date: Thu, 27 Dec 2018 13:02:49 +0100 Subject: [PATCH 02/10] change deprecated logging method Signed-off-by: Jonas Sulzer --- lib/ftp.php | 6 +++--- lib/imap.php | 2 +- lib/smb.php | 12 ++++++------ lib/webdavauth.php | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/ftp.php b/lib/ftp.php index 1652b82..ce9ae19 100644 --- a/lib/ftp.php +++ b/lib/ftp.php @@ -46,9 +46,9 @@ class OC_User_FTP extends \OCA\user_external\Base{ */ public function checkPassword($uid, $password) { if (false === array_search($this->protocol, stream_get_wrappers())) { - OCP\Util::writeLog( - 'user_external', - 'ERROR: Stream wrapper not available: ' . $this->protocol, OCP\Util::ERROR + OC::$server->getLogger()->error( + 'ERROR: Stream wrapper not available: ' . $this->protocol, + ['app' => 'user_external'] ); return false; } diff --git a/lib/imap.php b/lib/imap.php index c162400..a236290 100644 --- a/lib/imap.php +++ b/lib/imap.php @@ -42,7 +42,7 @@ class OC_User_IMAP extends \OCA\user_external\Base { */ public function checkPassword($uid, $password) { if (!function_exists('imap_open')) { - OCP\Util::writeLog('user_external', 'ERROR: PHP imap extension is not installed', OCP\Util::ERROR); + OC::$server->getLogger()->error('ERROR: PHP imap extension is not installed', ['app' => 'user_external']); return false; } diff --git a/lib/smb.php b/lib/smb.php index 7d16d18..d0da14f 100644 --- a/lib/smb.php +++ b/lib/smb.php @@ -42,9 +42,9 @@ class OC_User_SMB extends \OCA\user_external\Base{ $command = self::SMBCLIENT.' '.escapeshellarg('//' . $this->host . '/dummy').' -U'.$uidEscaped.'%'.$password; $lastline = exec($command, $output, $retval); if ($retval === 127) { - OCP\Util::writeLog( - 'user_external', 'ERROR: smbclient executable missing', - OCP\Util::ERROR + OC::$server->getLogger()->error( + 'ERROR: smbclient executable missing', + ['app' => 'user_external'] ); return false; } else if (strpos($lastline, self::LOGINERROR) !== false) { @@ -55,9 +55,9 @@ class OC_User_SMB extends \OCA\user_external\Base{ goto login; } else if ($retval !== 0) { //some other error - OCP\Util::writeLog( - 'user_external', 'ERROR: smbclient error: ' . trim($lastline), - OCP\Util::ERROR + OC::$server->getLogger()->error( + 'ERROR: smbclient error: ' . trim($lastline), + ['app' => 'user_external'] ); return false; } else { diff --git a/lib/webdavauth.php b/lib/webdavauth.php index a77adfe..747f9ac 100644 --- a/lib/webdavauth.php +++ b/lib/webdavauth.php @@ -28,14 +28,14 @@ class WebDavAuth extends Base { public function checkPassword($uid, $password) { $arr = explode('://', $this->webDavAuthUrl, 2); if( ! isset($arr) OR count($arr) !== 2) { - \OCP\Util::writeLog('OC_USER_WEBDAVAUTH', 'Invalid Url: "'.$this->webDavAuthUrl.'" ', 3); + OC::$server->getLogger()->error('ERROR: Invalid WebdavUrl: "'.$this->webDavAuthUrl.'" ', ['app' => 'user_external']); return false; } list($protocol, $path) = $arr; $url= $protocol.'://'.urlencode($uid).':'.urlencode($password).'@'.$path; $headers = get_headers($url); if($headers === false) { - \OCP\Util::writeLog('OC_USER_WEBDAVAUTH', 'Not possible to connect to WebDAV Url: "'.$protocol.'://'.$path.'" ', 3); + OC::$server->getLogger()->error('ERROR: Not possible to connect to WebDAV Url: "'.$protocol.'://'.$path.'" ', ['app' => 'user_external']); return false; } From 4eea136bf2365b23127751f6de2adaf5d5c3b627 Mon Sep 17 00:00:00 2001 From: Jonas Sulzer Date: Thu, 27 Dec 2018 13:37:42 +0100 Subject: [PATCH 03/10] appinfo changes Signed-off-by: Jonas Sulzer --- appinfo/info.xml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index 694b7b6..af303bb 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -4,22 +4,23 @@ External user support Use external user authentication methods like IMAP, SMB and FTP Use external user authentication methods like IMAP, SMB and FTP - tools - integration - AGPL + 0.5.0 + agpl Robin Appelman https://docs.nextcloud.com/server/15/admin_manual/configuration_user/user_auth_ftp_smb_imap.html + tools + integration https://github.com/nextcloud/user_external/ https://github.com/nextcloud/user_external/issues https://github.com/nextcloud/user_external.git - + - 0.5.0 + true From ecef285f41aa9d615379448efc5e9415160910cf Mon Sep 17 00:00:00 2001 From: Jonas Sulzer Date: Sat, 5 Jan 2019 00:00:31 +0100 Subject: [PATCH 04/10] resolve conflicts Signed-off-by: Jonas Sulzer --- appinfo/info.xml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index af303bb..d51c75a 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -16,11 +16,10 @@ https://github.com/nextcloud/user_external/issues https://github.com/nextcloud/user_external.git - + - true From 59e86461009876e6a2e22aabbc45334461b25a99 Mon Sep 17 00:00:00 2001 From: Jonas Sulzer Date: Thu, 10 Jan 2019 14:12:49 +0100 Subject: [PATCH 05/10] use \OC::$server->getDatabaseConnection()->executeQuery() where it works Signed-off-by: Jonas Sulzer --- lib/base.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/base.php b/lib/base.php index a817338..8f2be81 100644 --- a/lib/base.php +++ b/lib/base.php @@ -40,7 +40,7 @@ abstract class Base extends \OC\User\Backend{ * @return bool */ public function deleteUser($uid) { - OC_DB::executeAudited( + \OC::$server->getDatabaseConnection()->executeQuery( 'DELETE FROM `*PREFIX*users_external` WHERE `uid` = ? AND `backend` = ?', array($uid, $this->backend) ); @@ -136,7 +136,7 @@ abstract class Base extends \OC\User\Backend{ if (!$this->userExists($uid)) { return false; } - OC_DB::executeAudited( + \OC::$server->getDatabaseConnection()->executeQuery( 'UPDATE `*PREFIX*users_external` SET `displayname` = ?' . ' WHERE LOWER(`uid`) = ? AND `backend` = ?', array($displayName, $uid, $this->backend) @@ -154,7 +154,7 @@ abstract class Base extends \OC\User\Backend{ protected function storeUser($uid) { if (!$this->userExists($uid)) { - OC_DB::executeAudited( + \OC::$server->getDatabaseConnection()->executeQuery( 'INSERT INTO `*PREFIX*users_external` ( `uid`, `backend` )' . ' VALUES( ?, ? )', array($uid, $this->backend) From cfd23bc01e5a741353f339299efc105c713c0f20 Mon Sep 17 00:00:00 2001 From: Jonas Sulzer Date: Thu, 10 Jan 2019 23:22:24 +0100 Subject: [PATCH 06/10] use \OC::$server->getDatabaseConnection()->executeQuery() with fetch() where possible Signed-off-by: Jonas Sulzer --- lib/base.php | 8 ++++---- lib/imap.php | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/base.php b/lib/base.php index 8f2be81..39830a9 100644 --- a/lib/base.php +++ b/lib/base.php @@ -55,11 +55,11 @@ abstract class Base extends \OC\User\Backend{ * @return string display name */ public function getDisplayName($uid) { - $user = OC_DB::executeAudited( + $user = \OC::$server->getDatabaseConnection()->executeQuery( 'SELECT `displayname` FROM `*PREFIX*users_external`' . ' WHERE `uid` = ? AND `backend` = ?', array($uid, $this->backend) - )->fetchRow(); + )->fetch(); $displayName = trim($user['displayname'], ' '); if (!empty($displayName)) { return $displayName; @@ -170,11 +170,11 @@ abstract class Base extends \OC\User\Backend{ * @return boolean */ public function userExists($uid) { - $result = OC_DB::executeAudited( + $result = \OC::$server->getDatabaseConnection()->executeQuery( 'SELECT COUNT(*) FROM `*PREFIX*users_external`' . ' WHERE LOWER(`uid`) = LOWER(?) AND `backend` = ?', array($uid, $this->backend) ); - return $result->fetchOne() > 0; + return $result->fetch() > 0; } } diff --git a/lib/imap.php b/lib/imap.php index a236290..cf96cbf 100644 --- a/lib/imap.php +++ b/lib/imap.php @@ -52,13 +52,13 @@ class OC_User_IMAP extends \OCA\user_external\Base { $uid = str_replace("%40","@",$uid); } - $result = OC_DB::executeAudited( + $result = \OC::$server->getDatabaseConnection()->executeQuery( 'SELECT `userid` FROM `*PREFIX*preferences` WHERE `appid`=? AND `configkey`=? AND `configvalue`=?', array('settings','email',$uid) ); $users = array(); - while ($row = $result->fetchRow()) { + while ($row = $result->fetch()) { $users[] = $row['userid']; } From 757c651995a09bb0e5bfeda0064722cdf57cbc36 Mon Sep 17 00:00:00 2001 From: Jonas Sulzer Date: Fri, 11 Jan 2019 14:08:10 +0100 Subject: [PATCH 07/10] use $stmt = \OC::$server->getDatabaseConnection()->prepare() for the remaining two Signed-off-by: Jonas Sulzer --- lib/base.php | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/lib/base.php b/lib/base.php index 39830a9..52793fc 100644 --- a/lib/base.php +++ b/lib/base.php @@ -6,7 +6,6 @@ * See the COPYING-README file. */ namespace OCA\user_external; -use \OC_DB; /** * Base class for external auth implementations that stores users @@ -74,19 +73,17 @@ abstract class Base extends \OC\User\Backend{ * @return array with all displayNames (value) and the corresponding uids (key) */ public function getDisplayNames($search = '', $limit = null, $offset = null) { - $result = OC_DB::executeAudited( - array( - 'sql' => 'SELECT `uid`, `displayname` FROM `*PREFIX*users_external`' - . ' WHERE (LOWER(`displayname`) LIKE LOWER(?) ' - . ' OR LOWER(`uid`) LIKE LOWER(?)) AND `backend` = ?', - 'limit' => $limit, - 'offset' => $offset - ), - array('%' . $search . '%', '%' . $search . '%', $this->backend) + $stmt = \OC::$server->getDatabaseConnection()->prepare( + 'SELECT `uid`, `displayname` FROM `*PREFIX*users_external`' + . ' WHERE (LOWER(`displayname`) LIKE LOWER(?) ' + . ' OR LOWER(`uid`) LIKE LOWER(?)) AND `backend` = ?', + $limit, $offset ); + $stmt->execute(['%' . $search . '%', '%' . $search . '%', $this->backend]); + $displayNames = array(); - while ($row = $result->fetchRow()) { + while ($row = $stmt->fetch()) { $displayNames[$row['uid']] = $row['displayname']; } @@ -99,17 +96,16 @@ abstract class Base extends \OC\User\Backend{ * @return array with all uids */ public function getUsers($search = '', $limit = null, $offset = null) { - $result = OC_DB::executeAudited( - array( - 'sql' => 'SELECT `uid` FROM `*PREFIX*users_external`' - . ' WHERE LOWER(`uid`) LIKE LOWER(?) AND `backend` = ?', - 'limit' => $limit, - 'offset' => $offset - ), - array($search . '%', $this->backend) + $stmt = \OC::$server->getDatabaseConnection()->prepare( + 'SELECT `uid` FROM `*PREFIX*users_external`' + . ' WHERE LOWER(`uid`) LIKE LOWER(?) AND `backend` = ?', + $limit, $offset ); + + $stmt->execute([$search . '%', $this->backend]); + $users = array(); - while ($row = $result->fetchRow()) { + while ($row = $stmt->fetch()) { $users[] = $row['uid']; } return $users; From 3011974bebab2f2c97da2652ed191850fa33aeaf Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 11 Jan 2019 14:14:08 +0100 Subject: [PATCH 08/10] Fix order of info.xml attributes Signed-off-by: Joas Schilling --- appinfo/info.xml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index d51c75a..5c186b1 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -1,5 +1,6 @@ - + user_external External user support Use external user authentication methods like IMAP, SMB and FTP @@ -7,6 +8,10 @@ 0.5.0 agpl Robin Appelman + + + + https://docs.nextcloud.com/server/15/admin_manual/configuration_user/user_auth_ftp_smb_imap.html @@ -18,8 +23,4 @@ - - - - From b3565eda6c1a072c7902296dab99ed405894b450 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 11 Jan 2019 14:50:01 +0100 Subject: [PATCH 09/10] Use the querybuilder instead Signed-off-by: Joas Schilling --- lib/base.php | 118 ++++++++++++++++++++++++++++++++------------------- lib/imap.php | 24 ++++++----- 2 files changed, 89 insertions(+), 53 deletions(-) diff --git a/lib/base.php b/lib/base.php index 52793fc..c4da57f 100644 --- a/lib/base.php +++ b/lib/base.php @@ -39,10 +39,11 @@ abstract class Base extends \OC\User\Backend{ * @return bool */ public function deleteUser($uid) { - \OC::$server->getDatabaseConnection()->executeQuery( - 'DELETE FROM `*PREFIX*users_external` WHERE `uid` = ? AND `backend` = ?', - array($uid, $this->backend) - ); + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query->delete('users_external') + ->where($query->expr()->eq('uid', $query->createNamedParameter($uid))) + ->andWhere($query->expr()->eq('backend', $query->createNamedParameter($this->backend))); + $query->execute(); return true; } @@ -54,11 +55,15 @@ abstract class Base extends \OC\User\Backend{ * @return string display name */ public function getDisplayName($uid) { - $user = \OC::$server->getDatabaseConnection()->executeQuery( - 'SELECT `displayname` FROM `*PREFIX*users_external`' - . ' WHERE `uid` = ? AND `backend` = ?', - array($uid, $this->backend) - )->fetch(); + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query->select('displayname') + ->from('users_external') + ->where($query->expr()->eq('uid', $query->createNamedParameter($uid))) + ->andWhere($query->expr()->eq('backend', $query->createNamedParameter($this->backend))); + $result = $query->execute(); + $user = $result->fetch(); + $result->closeCursor(); + $displayName = trim($user['displayname'], ' '); if (!empty($displayName)) { return $displayName; @@ -73,19 +78,27 @@ abstract class Base extends \OC\User\Backend{ * @return array with all displayNames (value) and the corresponding uids (key) */ public function getDisplayNames($search = '', $limit = null, $offset = null) { - $stmt = \OC::$server->getDatabaseConnection()->prepare( - 'SELECT `uid`, `displayname` FROM `*PREFIX*users_external`' - . ' WHERE (LOWER(`displayname`) LIKE LOWER(?) ' - . ' OR LOWER(`uid`) LIKE LOWER(?)) AND `backend` = ?', - $limit, $offset - ); - $stmt->execute(['%' . $search . '%', '%' . $search . '%', $this->backend]); + $connection = \OC::$server->getDatabaseConnection(); + $query = $connection->getQueryBuilder(); + $query->select('uid', 'displayname') + ->from('users_external') + ->where($query->expr()->iLike('displayname', $query->createNamedParameter('%' . $connection->escapeLikeParameter($search) . '%'))) + ->andWhere($query->expr()->iLike('uid', $query->createNamedParameter('%' . $connection->escapeLikeParameter($search) . '%'))) + ->andWhere($query->expr()->eq('backend', $query->createNamedParameter($this->backend))); + if ($limit) { + $query->setMaxResults($limit); + } + if ($offset) { + $query->setFirstResult($offset); + } + $result = $query->execute(); - $displayNames = array(); - while ($row = $stmt->fetch()) { + $displayNames = []; + while ($row = $result->fetch()) { $displayNames[$row['uid']] = $row['displayname']; } + $result->closeCursor(); return $displayNames; } @@ -96,18 +109,26 @@ abstract class Base extends \OC\User\Backend{ * @return array with all uids */ public function getUsers($search = '', $limit = null, $offset = null) { - $stmt = \OC::$server->getDatabaseConnection()->prepare( - 'SELECT `uid` FROM `*PREFIX*users_external`' - . ' WHERE LOWER(`uid`) LIKE LOWER(?) AND `backend` = ?', - $limit, $offset - ); + $connection = \OC::$server->getDatabaseConnection(); + $query = $connection->getQueryBuilder(); + $query->select('uid') + ->from('users_external') + ->where($query->expr()->iLike('uid', $query->createNamedParameter($connection->escapeLikeParameter($search) . '%'))) + ->andWhere($query->expr()->eq('backend', $query->createNamedParameter($this->backend))); + if ($limit) { + $query->setMaxResults($limit); + } + if ($offset) { + $query->setFirstResult($offset); + } + $result = $query->execute(); - $stmt->execute([$search . '%', $this->backend]); - - $users = array(); - while ($row = $stmt->fetch()) { + $users = []; + while ($row = $result->fetch()) { $users[] = $row['uid']; } + $result->closeCursor(); + return $users; } @@ -132,11 +153,14 @@ abstract class Base extends \OC\User\Backend{ if (!$this->userExists($uid)) { return false; } - \OC::$server->getDatabaseConnection()->executeQuery( - 'UPDATE `*PREFIX*users_external` SET `displayname` = ?' - . ' WHERE LOWER(`uid`) = ? AND `backend` = ?', - array($displayName, $uid, $this->backend) - ); + + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query->update('users_external') + ->set('displayname', $query->createNamedParameter($displayName)) + ->where($query->expr()->eq('uid', $query->createNamedParameter($uid))) + ->andWhere($query->expr()->eq('backend', $query->createNamedParameter($this->backend))); + $query->execute(); + return true; } @@ -150,11 +174,14 @@ abstract class Base extends \OC\User\Backend{ protected function storeUser($uid) { if (!$this->userExists($uid)) { - \OC::$server->getDatabaseConnection()->executeQuery( - 'INSERT INTO `*PREFIX*users_external` ( `uid`, `backend` )' - . ' VALUES( ?, ? )', - array($uid, $this->backend) - ); + + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query->insert('users_external') + ->values([ + 'uid' => $query->createNamedParameter($uid), + 'backend' => $query->createNamedParameter($this->backend), + ]); + $query->execute(); } } @@ -166,11 +193,16 @@ abstract class Base extends \OC\User\Backend{ * @return boolean */ public function userExists($uid) { - $result = \OC::$server->getDatabaseConnection()->executeQuery( - 'SELECT COUNT(*) FROM `*PREFIX*users_external`' - . ' WHERE LOWER(`uid`) = LOWER(?) AND `backend` = ?', - array($uid, $this->backend) - ); - return $result->fetch() > 0; + $connection = \OC::$server->getDatabaseConnection(); + $query = $connection->getQueryBuilder(); + $query->select($query->func()->count('*', 'num_users')) + ->from('users_external') + ->where($query->expr()->iLike('uid', $query->createNamedParameter($connection->escapeLikeParameter($uid)))) + ->andWhere($query->expr()->eq('backend', $query->createNamedParameter($this->backend))); + $result = $query->execute(); + $users = $result->fetchColumn(); + $result->closeCursor(); + + return $users > 0; } } diff --git a/lib/imap.php b/lib/imap.php index cf96cbf..604674d 100644 --- a/lib/imap.php +++ b/lib/imap.php @@ -52,25 +52,29 @@ class OC_User_IMAP extends \OCA\user_external\Base { $uid = str_replace("%40","@",$uid); } - $result = \OC::$server->getDatabaseConnection()->executeQuery( - 'SELECT `userid` FROM `*PREFIX*preferences` WHERE `appid`=? AND `configkey`=? AND `configvalue`=?', - array('settings','email',$uid) - ); + $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $query->select('userid') + ->from('preferences') + ->where($query->expr()->eq('appid', $query->createNamedParameter('settings'))) + ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('email'))) + ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter($uid))); + $result = $query->execute(); - $users = array(); + $users = []; while ($row = $result->fetch()) { $users[] = $row['userid']; } + $result->closeCursor(); if(count($users) === 1) { $username = $uid; $uid = $users[0]; // Check if we only want logins from ONE domain and strip the domain part from UID - }elseif($this->domain !== '') { + }elseif($this->domain !== '') { $pieces = explode('@', $uid); if(count($pieces) === 1) { $username = $uid . "@" . $this->domain; - }elseif((count($pieces) === 2) and ($pieces[1] === $this->domain)) { + }elseif((count($pieces) === 2) && ($pieces[1] === $this->domain)) { $username = $uid; $uid = $pieces[0]; }else{ @@ -83,13 +87,13 @@ class OC_User_IMAP extends \OCA\user_external\Base { $mbox = @imap_open($this->mailbox, $username, $password, OP_HALFOPEN, 1); imap_errors(); imap_alerts(); - if($mbox !== FALSE) { + if($mbox !== false) { imap_close($mbox); $uid = mb_strtolower($uid); $this->storeUser($uid); return $uid; - }else{ - return false; } + + return false; } } From 8c5e80b21d415a5a0505e742b2ae62006c345701 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 11 Jan 2019 17:06:16 +0100 Subject: [PATCH 10/10] Login via Email Address is handled by the LoginController Signed-off-by: Joas Schilling --- lib/imap.php | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/lib/imap.php b/lib/imap.php index 604674d..a412da2 100644 --- a/lib/imap.php +++ b/lib/imap.php @@ -52,39 +52,21 @@ class OC_User_IMAP extends \OCA\user_external\Base { $uid = str_replace("%40","@",$uid); } - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - $query->select('userid') - ->from('preferences') - ->where($query->expr()->eq('appid', $query->createNamedParameter('settings'))) - ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('email'))) - ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter($uid))); - $result = $query->execute(); - - $users = []; - while ($row = $result->fetch()) { - $users[] = $row['userid']; - } - $result->closeCursor(); - - if(count($users) === 1) { + if ($this->domain !== '') { + $pieces = explode('@', $uid); + if (count($pieces) === 1) { + $username = $uid . '@' . $this->domain; + } else if(count($pieces) === 2 && $pieces[1] === $this->domain) { + $username = $uid; + $uid = $pieces[0]; + } else { + return false; + } + } else { $username = $uid; - $uid = $users[0]; - // Check if we only want logins from ONE domain and strip the domain part from UID - }elseif($this->domain !== '') { - $pieces = explode('@', $uid); - if(count($pieces) === 1) { - $username = $uid . "@" . $this->domain; - }elseif((count($pieces) === 2) && ($pieces[1] === $this->domain)) { - $username = $uid; - $uid = $pieces[0]; - }else{ - return false; - } - }else{ - $username = $uid; } - $mbox = @imap_open($this->mailbox, $username, $password, OP_HALFOPEN, 1); + $mbox = @imap_open($this->mailbox, $username, $password, OP_HALFOPEN, 1); imap_errors(); imap_alerts(); if($mbox !== false) {