Skip to content

Commit

Permalink
refactor: inject request data into controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
edmondas committed Jan 1, 2024
1 parent 2915bd9 commit 970a91c
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 78 deletions.
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

require __DIR__ . '/vendor/autoload.php';

$router = new BasicRouter($_GET);
$router = new BasicRouter(array_merge($_GET, $_POST));
$router->setDefaultPage('index');
$router->setPages([
'add_perm_templ',
Expand Down
54 changes: 34 additions & 20 deletions lib/Application/Controller/AddPermTemplController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,53 @@
namespace Poweradmin\Application\Controller;

use Poweradmin\BaseController;
use Poweradmin\Infrastructure\Repository\DbPermissionTemplateRepository;
use Poweradmin\LegacyUsers;
use Valitron;

class AddPermTemplController extends BaseController
{
private DbPermissionTemplateRepository $permissionTemplate;

public function __construct(array $request)
{
parent::__construct($request);

$this->permissionTemplate = new DbPermissionTemplateRepository($this->db);
}

public function run(): void
{
$this->checkPermission('templ_perm_add', _("You do not have the permission to add permission templates."));

if ($this->isPost()) {
$this->addPermTempl();
$this->handleFormSubmission();
} else {
$this->showAddPermTempl();
$this->showForm();
}
}

private function handleFormSubmission(): void
{
if (!$this->validateRequest()) {
$this->showFirstValidationError();
return;
}

$this->permissionTemplate->addPermissionTemplate($this->getRequest());
$this->setMessage('list_perm_templ', 'success', _('The permission template has been added successfully.'));
$this->redirect('index.php', ['page' => 'list_perm_templ']);
}

private function showForm(): void
{
$this->render('add_perm_templ.html', [
'perms_avail' => LegacyUsers::get_permissions_by_template_id($this->db)
]);
}

private function addPermTempl(): void
private function validateRequest(): bool
{
$v = new Valitron\Validator($_POST);
$v->rules([
$this->setRequestRules([
'required' => ['templ_name'],
'lengthMax' => [
['templ_name', 128],
Expand All @@ -60,19 +87,6 @@ private function addPermTempl(): void
'array' => ['perm_id'],
]);

if ($v->validate()) {
LegacyUsers::add_perm_templ($this->db, $_POST);
$this->setMessage('list_perm_templ', 'success', _('The permission template has been added successfully.'));
$this->redirect('index.php', ['page'=> 'list_perm_templ']);
} else {
$this->showFirstError($v->errors());
}
}

private function showAddPermTempl(): void
{
$this->render('add_perm_templ.html', [
'perms_avail' => LegacyUsers::get_permissions_by_template_id($this->db)
]);
return $this->doValidateRequest();
}
}
4 changes: 2 additions & 2 deletions lib/Application/Controller/AddRecordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class AddRecordController extends BaseController
{
private LegacyLogger $logger;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$this->logger = new LegacyLogger($this->db);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/AddZoneMasterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class AddZoneMasterController extends BaseController

private LegacyLogger $logger;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$this->logger = new LegacyLogger($this->db);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/AddZoneSlaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class AddZoneSlaveController extends BaseController

private LegacyLogger $logger;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$this->logger = new LegacyLogger($this->db);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/BulkRegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class BulkRegistrationController extends BaseController

private LegacyLogger $logger;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$this->logger = new LegacyLogger($this->db);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/ChangePasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class ChangePasswordController extends BaseController
{
private AuthenticationService $authService;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$sessionService = new SessionService();
$redirectService = new RedirectService();
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/DeleteDomainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class DeleteDomainController extends BaseController

private LegacyLogger $logger;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$this->logger = new LegacyLogger($this->db);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/DeleteDomainsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class DeleteDomainsController extends BaseController

private LegacyLogger $logger;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$this->logger = new LegacyLogger($this->db);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/DeleteRecordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class DeleteRecordController extends BaseController

private LegacyLogger $logger;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$this->logger = new LegacyLogger($this->db);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Application/Controller/EditPermTemplController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private function editPermTempl(): void
LegacyUsers::update_perm_templ_details($this->db, $_POST);

$this->setMessage('list_perm_templ', 'success', _('The permission template has been updated successfully.'));
$this->redirect('index.php', ['page'=> 'list_perm_templ');
$this->redirect('index.php', ['page'=> 'list_perm_templ']);
} else {
$this->showFirstError($v->errors());
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/EditRecordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class EditRecordController extends BaseController

private LegacyLogger $logger;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$this->logger = new LegacyLogger($this->db);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/ListLogUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class ListLogUsersController extends BaseController

private DbUserLogger $dbUserLogger;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$this->dbUserLogger = new DbUserLogger($this->db);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/ListLogZonesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class ListLogZonesController extends BaseController
{
private DbZoneLogger $dbZoneLogger;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$this->dbZoneLogger = new DbZoneLogger($this->db);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class LoginController extends BaseController
private LocaleService $localeService;
private LocalePresenter $localePresenter;

public function __construct()
public function __construct(array $request)
{
parent::__construct($authenticate = false);
parent::__construct($request, false);

$this->localeRepository = new LocaleRepository();
$this->localeService = new LocaleService();
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/LogoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class LogoutController extends BaseController
{
private AuthenticationService $authService;

public function __construct()
public function __construct(array $request)
{
parent::__construct();
parent::__construct($request);

$sessionService = new SessionService();
$redirectService = new RedirectService();
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/SwitchThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class SwitchThemeController extends BaseController
private array $get;
private array $server;

public function __construct()
public function __construct(array $request)
{
// parent::__construct(); FIXME: this doesn't work
// parent::__construct($request); FIXME: this doesn't work

$this->get = $_GET;
$this->server = $_SERVER;
Expand Down
2 changes: 1 addition & 1 deletion lib/Application/Routing/BasicRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function process(): void
throw new Exception("Class $controllerClassName not found");
}

$controller = new $controllerClassName();
$controller = new $controllerClassName($this->request);
$controller->run();
}

Expand Down
27 changes: 26 additions & 1 deletion lib/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,27 @@
use Poweradmin\Application\Presenter\ErrorPresenter;
use Poweradmin\Domain\Error\ErrorMessage;
use Poweradmin\Infrastructure\Web\ThemeManager;
use Valitron;

abstract class BaseController
{
private Application $app;
private LegacyApplicationInitializer $init;
protected PDOLayer $db;
private array $request;
private $validator;

abstract public function run(): void;

public function __construct(bool $authenticate = true)
public function __construct(array $request, bool $authenticate = true)
{
$this->app = AppFactory::create();

$this->init = new LegacyApplicationInitializer($authenticate);
$this->db = $this->init->getDb();

$this->request = $request;
$this->validator = new Valitron\Validator($this->getRequest());
}

public function isPost(): bool
Expand Down Expand Up @@ -225,4 +231,23 @@ private function renderFooter(): void

$this->db->disconnect();
}

public function getRequest(): array
{
return $this->request;
}

public function setRequestRules(array $rules): void
{
$this->validator->rules($rules);
}

public function doValidateRequest(): bool
{
return $this->validator->validate();
}

public function showFirstValidationError(): void {
$this->showFirstError($this->validator->errors());
}
}
59 changes: 59 additions & 0 deletions lib/Infrastructure/Repository/DbPermissionTemplateRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/* Poweradmin, a friendly web-based admin tool for PowerDNS.
* See <https://www.poweradmin.org> for more details.
*
* Copyright 2007-2010 Rejo Zenger <rejo@zenger.nl>
* Copyright 2010-2023 Poweradmin Development Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Poweradmin\Infrastructure\Repository;

class DbPermissionTemplateRepository
{
private object $db;

public function __construct($db)
{
$this->db = $db;
}

/**
* Add a Permission Template
*
* @param array $details Permission template details [templ_name,templ_descr,perm_id]
*
* @return boolean true on success, false otherwise
*/
public function addPermissionTemplate(array $details): bool
{
$query = "INSERT INTO perm_templ (name, descr)
VALUES (" . $this->db->quote($details['templ_name'], 'text') . ", " . $this->db->quote($details['templ_descr'], 'text') . ")";

$this->db->query($query);

$perm_templ_id = $this->db->lastInsertId();

if (isset($details['perm_id'])) {
foreach ($details['perm_id'] as $perm_id) {
$query = "INSERT INTO perm_templ_items (templ_id, perm_id) VALUES (" . $this->db->quote($perm_templ_id, 'integer') . "," . $this->db->quote($perm_id, 'integer') . ")";
$this->db->query($query);
}
}

return true;
}
}

0 comments on commit 970a91c

Please sign in to comment.