From 3a21b2ddafd3d56c254a5c717b03067691f73f8e Mon Sep 17 00:00:00 2001 From: Patrick Valsecchi Date: Tue, 19 Dec 2017 09:31:07 +0100 Subject: [PATCH 1/7] Add support for groups --- ajax/settings.php | 2 +- appinfo/app.php | 3 +++ lib/helper.php | 17 ++++++++++++++++- templates/settings.php | 10 ++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/ajax/settings.php b/ajax/settings.php index ee085a6..a25fbfa 100644 --- a/ajax/settings.php +++ b/ajax/settings.php @@ -80,7 +80,7 @@ if(isset($_POST['appname']) && ($_POST['appname'] === 'user_sql') && isset($_POS $columns = array(); foreach($params as $param) { - if(strpos($param, 'col_') === 0) + if(strpos($param, 'col_') === 0 && strpos($param, 'col_group_') !== 0) { if(isset($_POST[$param]) && $_POST[$param] !== '') $columns[] = $_POST[$param]; diff --git a/appinfo/app.php b/appinfo/app.php index 2617fb9..28ac922 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -22,10 +22,13 @@ */ require_once __DIR__ . '/../user_sql.php'; +require_once __DIR__ . '/../group_sql.php'; \OCP\App::registerAdmin('user_sql','settings'); $backend = new \OCA\user_sql\OC_USER_SQL; +$group_backend = new \OCA\user_sql\OC_GROUP_SQL; // register user backend \OC_User::useBackend($backend); +\OC::$server->getGroupManager()->addBackend($group_backend); diff --git a/lib/helper.php b/lib/helper.php index 7a3023f..e19bc6b 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -64,7 +64,10 @@ class Helper { 'set_mail_sync_mode', 'set_enable_gethome', 'set_gethome_mode', - 'set_gethome' + 'set_gethome', + 'sql_group_table', + 'col_group_username', + 'col_group_name' ); return $params; @@ -173,6 +176,18 @@ class Helper { case 'mysqlPassword': $query = "SELECT PASSWORD(:pw);"; break; + + case 'getUserGroups': + $query = "SELECT ".$this->settings['col_group_name']." FROM ".$this->settings['sql_group_table']." WHERE ".$this->settings['col_group_username']." = :uid"; + break; + + case 'getGroups': + $query = "SELECT distinct ".$this->settings['col_group_name']." FROM ".$this->settings['sql_group_table']; + break; + + case 'getGroupUsers': + $query = "SELECT distinct ".$this->settings['col_group_username']." FROM ".$this->settings['sql_group_table']." WHERE ".$this->settings['col_group_name']." = :gid"; + break; } if(isset($limits['limit']) && $limits['limit'] !== null) diff --git a/templates/settings.php b/templates/settings.php index c555c5c..11dd10b 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -20,6 +20,7 @@ $cfgClass = $ocVersion >= 7 ? 'section' : 'personalblock';
  • t('E-Mail Settings')); ?>
  • t('Domain Settings')); ?>
  • t('getHome Settings')); ?>
  • +
  • t('Groups Settings')); ?>
  • @@ -155,6 +156,15 @@ $cfgClass = $ocVersion >= 7 ? 'section' : 'personalblock'; t('You can use the placeholders %%u to specify the user ID (before appending the default domain), %%ud to specify the user ID (after appending the default domain) and %%d to specify the default domain')); ?>

    +
    +

    + +

    + +

    + +
    + From f95ca2753afcc97570fe5f431e166cd3eacd8cd6 Mon Sep 17 00:00:00 2001 From: Patrick Valsecchi Date: Tue, 19 Dec 2017 11:01:44 +0100 Subject: [PATCH 2/7] Allow views and tables --- js/settings.js | 4 ++-- lib/helper.php | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/js/settings.js b/js/settings.js index a77bffb..034cc2e 100644 --- a/js/settings.js +++ b/js/settings.js @@ -17,7 +17,7 @@ user_sql.adminSettingsUI = function() $('#sqlDiv').tabs(); // Attach auto-completion to all column fields - $('#col_username, #col_password, #col_displayname, #col_active, #col_email, #col_gethome').autocomplete({ + $('#col_username, #col_password, #col_displayname, #col_active, #col_email, #col_gethome, #col_group_name, #col_group_username').autocomplete({ source: function(request, response) { var post = $('#sqlForm').serializeArray(); @@ -56,7 +56,7 @@ user_sql.adminSettingsUI = function() }); // Attach auto-completion to all table fields - $('#sql_table').autocomplete({ + $('#sql_table, #sql_group_table').autocomplete({ source: function(request, response) { var post = $('#sqlForm').serializeArray(); diff --git a/lib/helper.php b/lib/helper.php index e19bc6b..9d9c80a 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -314,13 +314,19 @@ class Helper { try { $conn = $cm -> getConnection($sql_driver, $parameters); $platform = $conn -> getDatabasePlatform(); - $query = $platform -> getListTablesSQL(); - $result = $conn -> executeQuery($query); + + $queries = array( + 'Tables_in_'.$parameters['dbname'] => $platform -> getListTablesSQL(), + 'TABLE_NAME' => $platform -> getListViewsSQL($parameters['dbname'])); $ret = array(); - while($row = $result -> fetch()) + foreach($queries as $field => $query) { - $name = $row['Tables_in_'.$parameters['dbname']]; - $ret[] = $name; + $result = $conn -> executeQuery($query); + while($row = $result -> fetch()) + { + $name = $row[$field]; + $ret[] = $name; + } } return $ret; } From 9c4cebd6ebb64e9000681c588b479f79b2c49530 Mon Sep 17 00:00:00 2001 From: Patrick Valsecchi Date: Tue, 19 Dec 2017 11:32:08 +0100 Subject: [PATCH 3/7] Specify OwnCloud version --- appinfo/info.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/appinfo/info.xml b/appinfo/info.xml index 2b557d1..d2b6911 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -15,5 +15,6 @@ auth + From c8f1051193fd20b3b8c7464307be39f7dd4ac9c1 Mon Sep 17 00:00:00 2001 From: Patrick Valsecchi Date: Tue, 19 Dec 2017 12:19:12 +0100 Subject: [PATCH 4/7] Better settings validation --- ajax/settings.php | 24 ++++++++++++++++++++++-- lib/helper.php | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ajax/settings.php b/ajax/settings.php index a25fbfa..eabdfd3 100644 --- a/ajax/settings.php +++ b/ajax/settings.php @@ -75,20 +75,40 @@ if(isset($_POST['appname']) && ($_POST['appname'] === 'user_sql') && isset($_POS 'data' => array('message' => $l -> t('The selected SQL table '.$_POST['sql_table'].' does not exist!')))); break; } + if(!empty($_POST['sql_group_table']) && !$helper->verifyTable($parameters, $_POST['sql_driver'], $_POST['sql_group_table'])) + { + $response->setData(array('status' => 'error', + 'data' => array('message' => $l -> t('The selected SQL table '.$_POST['sql_group_table'].' does not exist!')))); + break; + } // Retrieve all column settings $columns = array(); + $group_columns = array(); foreach($params as $param) { - if(strpos($param, 'col_') === 0 && strpos($param, 'col_group_') !== 0) + if(strpos($param, 'col_') === 0) { if(isset($_POST[$param]) && $_POST[$param] !== '') - $columns[] = $_POST[$param]; + { + if(strpos($param, 'col_group_') === 0) + { + $group_columns[] = $_POST[$param]; + } + else + { + $columns[] = $_POST[$param]; + } + } } } // Check if the columns exist $status = $helper->verifyColumns($parameters, $_POST['sql_driver'], $_POST['sql_table'], $columns); + if(!empty($_POST['sql_group_table']) && $status === true) + { + $status = $helper->verifyColumns($parameters, $_POST['sql_driver'], $_POST['sql_group_table'], $group_columns); + } if($status !== true) { $response->setData(array('status' => 'error', diff --git a/lib/helper.php b/lib/helper.php index 9d9c80a..18177a3 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -280,7 +280,7 @@ class Helper { if(!in_array($col, $columns, true)) { $res = false; - $err .= $col.' '; + $err .= $table.'.'.$col.' '; } } if($res) From f6d6ebbb706cd37504eedb8fc6fac8028bebe86b Mon Sep 17 00:00:00 2001 From: Patrick Valsecchi Date: Tue, 19 Dec 2017 14:24:06 +0100 Subject: [PATCH 5/7] Fix email address for OwnCloud --- group_sql.php | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ user_sql.php | 17 ++++++++++- 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 group_sql.php diff --git a/group_sql.php b/group_sql.php new file mode 100644 index 0000000..641beff --- /dev/null +++ b/group_sql.php @@ -0,0 +1,79 @@ + helper = new \OCA\user_sql\lib\Helper(); + $domain = \OC::$server->getRequest()->getServerHost(); + $this -> settings = $this -> helper -> loadSettingsForDomain($domain); + $this -> helper -> connectToDb($this -> settings); + return false; + } + + public function getUserGroups($uid) { + if(empty($this -> settings['sql_group_table'])) + { + \OCP\Util::writeLog('OC_USER_SQL', "Group table not configured", \OCP\Util::DEBUG); + return []; + } + $rows = $this -> helper -> runQuery('getUserGroups', array('uid' => $uid), false, true); + if($rows === false) + { + \OCP\Util::writeLog('OC_USER_SQL', "Found no group", \OCP\Util::DEBUG); + return []; + } + $groups = array(); + foreach($rows as $row) + { + $groups[] = $row[$this -> settings['col_group_name']]; + } + return $groups; + } + + public function getGroups($search = '', $limit = null, $offset = null) { + if(empty($this -> settings['sql_group_table'])) + { + return []; + } + $rows = $this -> helper -> runQuery('getGroups', array(), false, true); + if($rows === false) + { + return []; + } + $groups = array(); + foreach($rows as $row) + { + $groups[] = $row[$this -> settings['col_group_name']]; + } + return $groups; + } + + public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { + if(empty($this -> settings['sql_group_table'])) + { + \OCP\Util::writeLog('OC_USER_SQL', "Group table not configured", \OCP\Util::DEBUG); + return []; + } + $rows = $this -> helper -> runQuery('getGroupUsers', array('gid' => $gid), false, true); + if($rows === false) + { + \OCP\Util::writeLog('OC_USER_SQL', "Found no users for group", \OCP\Util::DEBUG); + return []; + } + $users = array(); + foreach($rows as $row) + { + $users[] = $row[$this -> settings['col_group_username']]; + } + return $users; + } +} +?> diff --git a/user_sql.php b/user_sql.php index 9283c66..f824a8d 100644 --- a/user_sql.php +++ b/user_sql.php @@ -31,7 +31,13 @@ namespace OCA\user_sql; use \OCA\user_sql\lib\Helper; -class OC_USER_SQL extends \OC_User_Backend implements \OCP\IUserBackend, \OCP\UserInterface +if(!interface_exists('OCP\\User\\IProvidesEMailBackend')) +{ + // hack for nextcloud + eval("namespace OCP\User; interface IProvidesEMailBackend {}"); +} + +class OC_USER_SQL extends \OC_User_Backend implements \OCP\IUserBackend, \OCP\UserInterface, \OCP\User\IProvidesEMailBackend { protected $cache; protected $settings; @@ -116,6 +122,15 @@ class OC_USER_SQL extends \OC_User_Backend implements \OCP\IUserBackend, \OCP\Us return true; } + /** + * Only used by OwnCloud to get the email address + */ + public function getEMailAddress($uid) { + $this->doEmailSync($uid); + $email = $this->ocConfig->getUserValue($uid, 'settings', 'email', ''); + return $email; + } + /** * This maps the username to the specified domain name. * It can only append a default domain name. From 031b5882554266ab709299bb157f66c18b7e7e34 Mon Sep 17 00:00:00 2001 From: Patrick Valsecchi Date: Tue, 19 Dec 2017 20:13:03 +0100 Subject: [PATCH 6/7] Add countUsersInGroup --- group_sql.php | 15 +++++++++++++++ lib/helper.php | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/group_sql.php b/group_sql.php index 641beff..e1c1a26 100644 --- a/group_sql.php +++ b/group_sql.php @@ -75,5 +75,20 @@ class OC_GROUP_SQL extends \OC_Group_Backend implements \OCP\GroupInterface } return $users; } + + public function countUsersInGroup($gid, $search = '') { + if(empty($this -> settings['sql_group_table'])) + { + return 0; + } + $search = "%".$search."%"; + $count = $this -> helper -> runQuery('countUsersInGroup', array('gid' => $gid, 'search' => $search)); + if($count === false) + { + return 0; + } else { + return intval(reset($count)); + } + } } ?> diff --git a/lib/helper.php b/lib/helper.php index 18177a3..5de5d6a 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -188,6 +188,10 @@ class Helper { case 'getGroupUsers': $query = "SELECT distinct ".$this->settings['col_group_username']." FROM ".$this->settings['sql_group_table']." WHERE ".$this->settings['col_group_name']." = :gid"; break; + + case 'countUsersInGroup': + $query = "SELECT count(".$this->settings['col_group_username'].") FROM ".$this->settings['sql_group_table']." WHERE ".$this->settings['col_group_name']." = :gid AND ".$this->settings['col_group_username']." LIKE :search"; + break; } if(isset($limits['limit']) && $limits['limit'] !== null) From 96c9bfeba8c924fb0d8bb709518ea1d04d41331b Mon Sep 17 00:00:00 2001 From: Patrick Valsecchi Date: Tue, 19 Dec 2017 20:21:06 +0100 Subject: [PATCH 7/7] Better getGroups implementation --- group_sql.php | 5 +++-- lib/helper.php | 2 +- user_sql.php | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/group_sql.php b/group_sql.php index e1c1a26..08c8d76 100644 --- a/group_sql.php +++ b/group_sql.php @@ -42,8 +42,9 @@ class OC_GROUP_SQL extends \OC_Group_Backend implements \OCP\GroupInterface if(empty($this -> settings['sql_group_table'])) { return []; - } - $rows = $this -> helper -> runQuery('getGroups', array(), false, true); + } + $search = "%".$search."%"; + $rows = $this -> helper -> runQuery('getGroups', array('search' => $search), false, true, array('limit' => $limit, 'offset' => $offset)); if($rows === false) { return []; diff --git a/lib/helper.php b/lib/helper.php index 5de5d6a..0074f1d 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -182,7 +182,7 @@ class Helper { break; case 'getGroups': - $query = "SELECT distinct ".$this->settings['col_group_name']." FROM ".$this->settings['sql_group_table']; + $query = "SELECT distinct ".$this->settings['col_group_name']." FROM ".$this->settings['sql_group_table']." WHERE ".$this->settings['col_group_name']." LIKE :search"; break; case 'getGroupUsers': diff --git a/user_sql.php b/user_sql.php index cfb8404..4186b33 100644 --- a/user_sql.php +++ b/user_sql.php @@ -404,7 +404,7 @@ class OC_USER_SQL extends \OC_User_Backend implements \OCP\IUserBackend, \OCP\Us } else { - $search = "%".$this -> doUserDomainMapping("")."%"; + $search = "%".$this -> doUserDomainMapping("")."%"; } $rows = $this -> helper -> runQuery('getUsers', array('search' => $search), false, true, array('limit' => $limit, 'offset' => $offset));