diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 059595e619..932d7a3d13 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2951,6 +2951,33 @@ function PMA_getUserGroupCount() return $user_group_count; } +/** + * Returns name of user group that user is part of + * + * @param string $username User name + * + * @return mixed usergroup if found or null if not found + */ +function PMA_getUserGroupForUser($username) +{ + $cfgRelation = PMA_getRelationsParam(); + $user_table = Util::backquote($cfgRelation['db']) + . '.' . Util::backquote($cfgRelation['users']); + $sql_query = 'SELECT `usergroup` FROM ' . $user_table + . ' WHERE `username` = \'' . $username . '\'' + . ' LIMIT 1'; + + $usergroup = $GLOBALS['dbi']->fetchValue( + $sql_query, 0, 0, $GLOBALS['controllink'] + ); + + if ($usergroup === false) { + return null; + } + + return $usergroup; +} + /** * This function return the extra data array for the ajax behavior * @@ -3129,8 +3156,15 @@ function PMA_getChangeLoginInformationHtmlForm($username, $hostname) . '' . "\n" . '' . "\n" - . '
' . "\n" + . 'value="' . htmlspecialchars($hostname) . '" />' . "\n"; + + $usergroup = PMA_getUserGroupForUser($username); + if ($usergroup !== null) { + $html_output .= '' . "\n"; + } + + $html_output .= '
' . "\n" . '' . "\n" . __('Change login information / Copy user account') . '' . "\n" @@ -4292,6 +4326,12 @@ function PMA_addUser( ); } + // Copy the user group while copying a user + $old_usergroup = + $_REQUEST['old_usergroup'] ? $_REQUEST['old_usergroup'] : null; + PMA_setUserGroup($_REQUEST['username'], $old_usergroup); + + if (isset($create_user_real)) { $queries[] = $create_user_real; } diff --git a/test/libraries/PMA_server_privileges_test.php b/test/libraries/PMA_server_privileges_test.php index 62c9030cd1..abf318bf01 100644 --- a/test/libraries/PMA_server_privileges_test.php +++ b/test/libraries/PMA_server_privileges_test.php @@ -1638,6 +1638,7 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase { $username = "pma_username"; $hostname = "pma_hostname"; + $GLOBALS['cfgRelation']['menuswork'] = true; $dbi_old = $GLOBALS['dbi']; $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface') @@ -1650,6 +1651,11 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase $dbi->expects($this->any())->method('fetchResult') ->will($this->returnValue($fields_info)); + $expected_userGroup = "pma_usergroup"; + + $dbi->expects($this->any())->method('fetchValue') + ->will($this->returnValue($expected_userGroup)); + $GLOBALS['dbi'] = $dbi; //PMA_getChangeLoginInformationHtmlForm @@ -1677,6 +1683,12 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase $html ); + $this->assertContains( + '', + $html + ); + //Create a new user with the same privileges $this->assertContains( "Create a new user account with the same privileges", @@ -1686,6 +1698,38 @@ class PMA_ServerPrivileges_Test extends PHPUnit_Framework_TestCase $GLOBALS['dbi'] = $dbi_old; } + /** + * Test for PMA_getUserGroupForUser + * + * @return void + */ + public function testPMAGetUserGroupForUser() + { + $username = "pma_username"; + $hostname = "pma_hostname"; + $GLOBALS['cfgRelation']['menuswork'] = true; + + $dbi_old = $GLOBALS['dbi']; + $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + $expected_userGroup = "pma_usergroup"; + + $dbi->expects($this->any())->method('fetchValue') + ->will($this->returnValue($expected_userGroup)); + + $GLOBALS['dbi'] = $dbi; + + $returned_userGroup = PMA_getUserGroupForUser($username); + + $this->assertEquals( + $expected_userGroup, + $returned_userGroup + ); + + $GLOBALS['dbi'] = $dbi_old; + } + /** * Test for PMA_getLinkToDbAndTable *