diff --git a/lib/plugins/authpdo/_test/sqlite.test.php b/lib/plugins/authpdo/_test/sqlite.test.php index aa8a4c25bf50cb4b7c5f9c7e7ea36332598b2eda..3941d709479138ad8893f5a5a4cdf1b78b9a8712 100644 --- a/lib/plugins/authpdo/_test/sqlite.test.php +++ b/lib/plugins/authpdo/_test/sqlite.test.php @@ -14,8 +14,8 @@ class testable_auth_plugin_authpdo extends auth_plugin_authpdo { return parent::_selectGroups(); } - public function _insertGroup($group) { - return parent::_insertGroup($group); + public function addGroup($group) { + return parent::addGroup($group); } } @@ -48,6 +48,27 @@ class sqlite_plugin_authpdo_test extends DokuWikiTest { $conf['plugin']['authpdo']['insert-user'] = 'INSERT INTO user (login, pass, name, mail) VALUES (:user, :hash, :name, :mail)'; $conf['plugin']['authpdo']['delete-user'] = 'DELETE FROM user WHERE id = :uid'; + $conf['plugin']['authpdo']['list-users'] = 'SELECT DISTINCT login as user + FROM user U, member M, "group" G + WHERE U.id = M.uid + AND M.gid = G.id + AND G."group" LIKE :group + AND U.login LIKE :user + AND U.name LIKE :name + AND U.mail LIKE :mail + ORDER BY login + LIMIT :start,:limit'; + + $conf['plugin']['authpdo']['count-users'] = 'SELECT COUNT(DISTINCT login) as count + FROM user U, member M, "group" G + WHERE U.id = M.uid + AND M.gid = G.id + AND G."group" LIKE :group + AND U.login LIKE :user + AND U.name LIKE :name + AND U.mail LIKE :mail'; + + $conf['plugin']['authpdo']['update-user-login'] = 'UPDATE user SET login = :newlogin WHERE id = :uid'; $conf['plugin']['authpdo']['update-user-info'] = 'UPDATE user SET name = :name, mail = :mail WHERE id = :uid'; $conf['plugin']['authpdo']['update-user-pass'] = 'UPDATE user SET pass = :hash WHERE id = :uid'; @@ -71,7 +92,7 @@ class sqlite_plugin_authpdo_test extends DokuWikiTest { $this->assertArrayHasKey('admin', $groups); $this->assertEquals(2, $groups['admin']['gid']); - $ok = $auth->_insertGroup('test'); + $ok = $auth->addGroup('test'); $this->assertTrue($ok); $groups = $auth->_selectGroups(); $this->assertArrayHasKey('test', $groups); @@ -125,9 +146,35 @@ class sqlite_plugin_authpdo_test extends DokuWikiTest { $info = $auth->getUserData('tester'); $this->assertEquals(array('admin', 'another', 'user'), $info['grps']); + // list users + $users = $auth->retrieveUsers(); + $this->assertEquals(array('admin', 'tester', 'user'), $users); + + $users = $auth->retrieveUsers(1); // offset + $this->assertEquals(array('tester', 'user'), $users); + + $users = $auth->retrieveUsers(1, 1); // offset + limit + $this->assertEquals(array('tester'), $users); + + $users = $auth->retrieveUsers(0, -1, array('group' => 'admin')); // full group + $this->assertEquals(array('admin', 'tester'), $users); + $count = $auth->getUserCount(array('group' => 'admin')); + $this->assertEquals(2, $count); + + $users = $auth->retrieveUsers(0, -1, array('group' => 'dmi')); // substring + $this->assertEquals(array('admin', 'tester'), $users); + $count = $auth->getUserCount(array('group' => 'dmi')); + $this->assertEquals(2, $count); + + $users = $auth->retrieveUsers(0, -1, array('user' => 'dmi')); // substring + $this->assertEquals(array('admin'), $users); + $count = $auth->getUserCount(array('user' => 'dmi')); + $this->assertEquals(1, $count); + // delete user $num = $auth->deleteUsers(array('tester', 'foobar')); $this->assertEquals(1, $num); + } } diff --git a/lib/plugins/authpdo/_test/test.sqlite3 b/lib/plugins/authpdo/_test/test.sqlite3 index 403bf5f7224ab809f443f1af2b4ffc5e0ed9aa85..8d3a9ba612c92804c33a14d8723689b7ca3a959f 100644 Binary files a/lib/plugins/authpdo/_test/test.sqlite3 and b/lib/plugins/authpdo/_test/test.sqlite3 differ diff --git a/lib/plugins/authpdo/auth.php b/lib/plugins/authpdo/auth.php index 7833083b9c23fcf360669f79b9027b2e5bf0db1b..f566497e166d839fd7dab6fe53739a889a2f065a 100644 --- a/lib/plugins/authpdo/auth.php +++ b/lib/plugins/authpdo/auth.php @@ -165,7 +165,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin { $allgroups = $this->_selectGroups(); foreach($grps as $group) { if(!isset($allgroups[$group])) { - $ok = $this->_insertGroup($group); + $ok = $this->addGroup($group); if($ok === false) goto FAIL; } } @@ -248,7 +248,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin { $added = 0; foreach($changes['grps'] as $group) { if(!isset($allgroups[$group])) { - $ok = $this->_insertGroup($group); + $ok = $this->addGroup($group); if($ok === false) goto FAIL; $added++; } @@ -302,37 +302,71 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin { * @param array $filter array of field/pattern pairs, null for no filter * @return array list of userinfo (refer getUserData for internal userinfo details) */ - //public function retrieveUsers($start = 0, $limit = -1, $filter = null) { - // FIXME implement - // return array(); - //} + public function retrieveUsers($start = 0, $limit = -1, $filter = null) { + if($limit < 0) $limit = 10000; // we don't support no limit + if(is_null($filter)) $filter = array(); + + foreach(array('user','name','mail','group') as $key) { + if(!isset($filter[$key])) { + $filter[$key] = '%'; + } else { + $filter[$key] = '%'.$filter[$key].'%'; + } + } + $filter['start'] = $start; + $filter['end'] = $start + $limit; + $filter['limit'] = $limit; + + $result = $this->_query($this->getConf('list-users'), $filter); + if(!$result) return array(); + $users = array(); + foreach($result as $row) { + if(!isset($row['user'])) { + $this->_debug("Statement did not return 'user' attribute", -1, __LINE__); + return array(); + } + $users[] = $row['user']; + } + return $users; + } /** * Return a count of the number of user which meet $filter criteria - * [should be implemented whenever retrieveUsers is implemented] - * - * Set getUserCount capability when implemented * * @param array $filter array of field/pattern pairs, empty array for no filter * @return int */ - //public function getUserCount($filter = array()) { - // FIXME implement - // return 0; - //} + public function getUserCount($filter = array()) { + if(is_null($filter)) $filter = array(); + + foreach(array('user','name','mail','group') as $key) { + if(!isset($filter[$key])) { + $filter[$key] = '%'; + } else { + $filter[$key] = '%'.$filter[$key].'%'; + } + } + + $result = $this->_query($this->getConf('count-users'), $filter); + if(!$result || !isset($result[0]['count'])) { + $this->_debug("Statement did not return 'count' attribute", -1, __LINE__); + } + return isset($result[0]['count']); + } /** - * Define a group [implement only where required/possible] - * - * Set addGroup capability when implemented + * Create a new group with the given name * - * @param string $group - * @return bool + * @param string $group + * @return bool */ - //public function addGroup($group) { - // FIXME implement - // return false; - //} + public function addGroup($group) { + $sql = $this->getConf('insert-group'); + + $result = $this->_query($sql, array(':group' => $group)); + if($result === false) return false; + return true; + } /** * Retrieve groups @@ -477,19 +511,6 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin { return $groups; } - /** - * Create a new group with the given name - * - * @param string $group - * @return bool - */ - protected function _insertGroup($group) { - $sql = $this->getConf('insert-group'); - - $result = $this->_query($sql, array(':group' => $group)); - if($result === false) return false; - return true; - } /** * Adds the user to the group diff --git a/lib/plugins/authpdo/conf/default.php b/lib/plugins/authpdo/conf/default.php index 762c3aeb523bf6458c2480eb8f2732a8069181f0..50dfbd0c90778bf2284258cc5c561654220b3058 100644 --- a/lib/plugins/authpdo/conf/default.php +++ b/lib/plugins/authpdo/conf/default.php @@ -40,6 +40,27 @@ $conf['insert-user'] = ''; */ $conf['delete-user'] = ''; +/** + * list user names matching the given criteria + * + * Make sure the list is distinct and sorted by user name. Apply the given limit and offset + * + * input: :user, :name, :mail, :group, :start, :end, :limit + * out: user + */ +$conf['list-users'] = ''; + +/** + * count user names matching the given criteria + * + * Make sure the counted list is distinct + * + * input: :user, :name, :mail, :group + * out: count + */ +$conf['list-users'] = ''; + + /** * Update user data (except password and user name) *