Skip to content

Commit

Permalink
Refactor Server/Privileges
Browse files Browse the repository at this point in the history
Use dependency injection

Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
  • Loading branch information
MauricioFauth committed Sep 16, 2018
1 parent b5e836d commit aeba92f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 30 deletions.
11 changes: 10 additions & 1 deletion libraries/classes/Display/ChangePassword.php
Expand Up @@ -10,6 +10,8 @@
namespace PhpMyAdmin\Display;

use PhpMyAdmin\Message;
use PhpMyAdmin\Relation;
use PhpMyAdmin\RelationCleanup;
use PhpMyAdmin\Server\Privileges;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
Expand All @@ -34,7 +36,14 @@ class ChangePassword
*/
public static function getHtml($mode, $username, $hostname)
{
$serverPrivileges = new Privileges(new Template());
$relation = new Relation($GLOBALS['dbi']);
$serverPrivileges = new Privileges(
new Template(),
$GLOBALS['dbi'],
$relation,
new RelationCleanup($GLOBALS['dbi'], $relation)
);

/**
* autocomplete feature of IE kills the "onchange" event handler and it
* must be replaced by the "onpropertychange" one in this case
Expand Down
48 changes: 27 additions & 21 deletions libraries/classes/Server/Privileges.php
Expand Up @@ -42,17 +42,29 @@ class Privileges
*/
public $dbi;

/**
* @var Relation $relation
*/
public $relation;

/**
* Privileges constructor.
*
* @param Template $template Template instance
* @param Template $template Template object
* @param DatabaseInterface $dbi DatabaseInterface object
* @param Relation $relation Relation object
* @param RelationCleanup $relationCleanup RelationCleanup object
*/
public function __construct(Template $template)
{
public function __construct(
Template $template,
DatabaseInterface $dbi,
Relation $relation,
RelationCleanup $relationCleanup
) {
$this->template = $template;
$this->dbi = $GLOBALS['dbi'];
$relation = new Relation($this->dbi);
$this->relationCleanup = new RelationCleanup($this->dbi, $relation);
$this->dbi = $dbi;
$this->relation = $relation;
$this->relationCleanup = $relationCleanup;
}

/**
Expand Down Expand Up @@ -557,8 +569,7 @@ public function getSqlQueryForDisplayPrivTable($db, $table, $username, $hostname
*/
public function getHtmlToChooseUserGroup($username)
{
$relation = new Relation($this->dbi);
$cfgRelation = $relation->getRelationsParam();
$cfgRelation = $this->relation->getRelationsParam();
$groupTable = Util::backquote($cfgRelation['db'])
. "." . Util::backquote($cfgRelation['usergroups']);
$userTable = Util::backquote($cfgRelation['db'])
Expand All @@ -578,7 +589,7 @@ public function getHtmlToChooseUserGroup($username)

$allUserGroups = ['' => ''];
$sql_query = "SELECT DISTINCT `usergroup` FROM " . $groupTable;
$result = $relation->queryAsControlUser($sql_query, false);
$result = $this->relation->queryAsControlUser($sql_query, false);
if ($result) {
while ($row = $this->dbi->fetchRow($result)) {
$allUserGroups[$row[0]] = $row[0];
Expand All @@ -604,8 +615,7 @@ public function getHtmlToChooseUserGroup($username)
public function setUserGroup($username, $userGroup)
{
$userGroup = is_null($userGroup) ? '' : $userGroup;
$relation = new Relation($this->dbi);
$cfgRelation = $relation->getRelationsParam();
$cfgRelation = $this->relation->getRelationsParam();
if (empty($cfgRelation['db']) || empty($cfgRelation['users']) || empty($cfgRelation['usergroups'])) {
return;
}
Expand Down Expand Up @@ -637,7 +647,7 @@ public function setUserGroup($username, $userGroup)
}
}
if (isset($upd_query)) {
$relation->queryAsControlUser($upd_query);
$this->relation->queryAsControlUser($upd_query);
}
}

Expand Down Expand Up @@ -2957,8 +2967,7 @@ public function getUserGroupEditLink($username)
*/
public function getUserGroupCount()
{
$relation = new Relation($this->dbi);
$cfgRelation = $relation->getRelationsParam();
$cfgRelation = $this->relation->getRelationsParam();
$user_group_table = Util::backquote($cfgRelation['db'])
. '.' . Util::backquote($cfgRelation['usergroups']);
$sql_query = 'SELECT COUNT(*) FROM ' . $user_group_table;
Expand All @@ -2981,8 +2990,7 @@ public function getUserGroupCount()
*/
public function getUserGroupForUser($username)
{
$relation = new Relation($this->dbi);
$cfgRelation = $relation->getRelationsParam();
$cfgRelation = $this->relation->getRelationsParam();

if (empty($cfgRelation['db'])
|| empty($cfgRelation['users'])
Expand Down Expand Up @@ -3026,7 +3034,6 @@ public function getExtraDataForAjaxBehavior(
$hostname,
$username
) {
$relation = new Relation($this->dbi);
if (isset($GLOBALS['dbname'])) {
//if (preg_match('/\\\\(?:_|%)/i', $dbname)) {
if (preg_match('/(?<!\\\\)(?:_|%)/i', $GLOBALS['dbname'])) {
Expand Down Expand Up @@ -3080,7 +3087,7 @@ public function getExtraDataForAjaxBehavior(

// if $cfg['Servers'][$i]['users'] and $cfg['Servers'][$i]['usergroups'] are
// enabled
$cfgRelation = $relation->getRelationsParam();
$cfgRelation = $this->relation->getRelationsParam();
if (!empty($cfgRelation['users']) && !empty($cfgRelation['usergroups'])) {
$new_user_string .= '<td class="usrGroup"></td>';
}
Expand Down Expand Up @@ -3687,14 +3694,13 @@ public function getUsersOverview($result, array $db_rights, $pmaThemeImage, $tex
*/
public function getHtmlTableBodyForUserRights(array $db_rights)
{
$relation = new Relation($this->dbi);
$cfgRelation = $relation->getRelationsParam();
$cfgRelation = $this->relation->getRelationsParam();
$user_group_count = 0;
if ($cfgRelation['menuswork']) {
$users_table = Util::backquote($cfgRelation['db'])
. "." . Util::backquote($cfgRelation['users']);
$sql_query = 'SELECT * FROM ' . $users_table;
$result = $relation->queryAsControlUser($sql_query, false);
$result = $this->relation->queryAsControlUser($sql_query, false);
$group_assignment = [];
if ($result) {
while ($row = $this->dbi->fetchAssoc($result)) {
Expand Down
10 changes: 9 additions & 1 deletion libraries/classes/Twig/ServerPrivilegesExtension.php
Expand Up @@ -9,6 +9,8 @@

namespace PhpMyAdmin\Twig;

use PhpMyAdmin\Relation;
use PhpMyAdmin\RelationCleanup;
use PhpMyAdmin\Server\Privileges;
use PhpMyAdmin\Template;
use Twig\Extension\AbstractExtension;
Expand All @@ -28,7 +30,13 @@ class ServerPrivilegesExtension extends AbstractExtension
*/
public function getFunctions()
{
$serverPrivileges = new Privileges(new Template());
$relation = new Relation($GLOBALS['dbi']);
$serverPrivileges = new Privileges(
new Template(),
$GLOBALS['dbi'],
$relation,
new RelationCleanup($GLOBALS['dbi'], $relation)
);
return [
new TwigFunction(
'format_privilege',
Expand Down
8 changes: 5 additions & 3 deletions libraries/classes/UserPassword.php
Expand Up @@ -29,11 +29,13 @@ class UserPassword
private $serverPrivileges;

/**
* Constructor
* UserPassword constructor.
*
* @param Privileges $serverPrivileges Privileges object
*/
public function __construct()
public function __construct(Privileges $serverPrivileges)
{
$this->serverPrivileges = new Privileges(new Template());
$this->serverPrivileges = $serverPrivileges;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions server_privileges.php
Expand Up @@ -10,8 +10,8 @@
use PhpMyAdmin\Core;
use PhpMyAdmin\Message;
use PhpMyAdmin\Relation;
use PhpMyAdmin\RelationCleanup;
use PhpMyAdmin\Response;
use PhpMyAdmin\Server\Common;
use PhpMyAdmin\Server\Privileges;
use PhpMyAdmin\Server\Users;
use PhpMyAdmin\Template;
Expand Down Expand Up @@ -39,7 +39,8 @@
$scripts->addFile('vendor/zxcvbn.js');

$template = new Template();
$serverPrivileges = new Privileges($template);
$relationCleanup = new RelationCleanup($GLOBALS['dbi'], $relation);
$serverPrivileges = new Privileges($template, $GLOBALS['dbi'], $relation, $relationCleanup);

if ((isset($_REQUEST['viewing_mode'])
&& $_REQUEST['viewing_mode'] == 'server')
Expand Down
11 changes: 10 additions & 1 deletion test/classes/Server/PrivilegesTest.php
Expand Up @@ -10,6 +10,8 @@
namespace PhpMyAdmin\Tests\Server;

use PhpMyAdmin\Core;
use PhpMyAdmin\Relation;
use PhpMyAdmin\RelationCleanup;
use PhpMyAdmin\Server\Privileges;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
Expand Down Expand Up @@ -82,7 +84,13 @@ protected function setUp()
$GLOBALS['text_dir'] = "text_dir";
$GLOBALS['is_reload_priv'] = true;

$this->serverPrivileges = new Privileges(new Template());
$relation = new Relation($GLOBALS['dbi']);
$this->serverPrivileges = new Privileges(
new Template(),
$GLOBALS['dbi'],
$relation,
new RelationCleanup($GLOBALS['dbi'], $relation)
);

//$_POST
$_POST['pred_password'] = 'none';
Expand Down Expand Up @@ -138,6 +146,7 @@ protected function setUp()

$GLOBALS['dbi'] = $dbi;
$this->serverPrivileges->dbi = $dbi;
$this->serverPrivileges->relation->dbi = $dbi;
$GLOBALS['is_grantuser'] = true;
$GLOBALS['is_createuser'] = true;
$GLOBALS['is_reload_priv'] = true;
Expand Down
10 changes: 9 additions & 1 deletion user_password.php
Expand Up @@ -10,7 +10,11 @@

use PhpMyAdmin\Display\ChangePassword;
use PhpMyAdmin\Message;
use PhpMyAdmin\Server\Privileges;
use PhpMyAdmin\Relation;
use PhpMyAdmin\RelationCleanup;
use PhpMyAdmin\Response;
use PhpMyAdmin\Template;
use PhpMyAdmin\UserPassword;

/**
Expand All @@ -24,7 +28,11 @@
$scripts->addFile('server_privileges.js');
$scripts->addFile('vendor/zxcvbn.js');

$userPassword = new UserPassword();
$template = new Template();
$relation = new Relation($GLOBALS['dbi']);
$relationCleanup = new RelationCleanup($GLOBALS['dbi'], $relation);
$serverPrivileges = new Privileges($template, $GLOBALS['dbi'], $relation, $relationCleanup);
$userPassword = new UserPassword($serverPrivileges);

/**
* Displays an error message and exits if the user isn't allowed to use this
Expand Down

0 comments on commit aeba92f

Please sign in to comment.