Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReportForm: Check whether the report name is unique #243

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions application/controllers/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function cloneAction(): void
$values[$name] = $value;
}

$form = (new ReportForm())
$form = (new ReportForm(Database::get()))
->setSubmitButtonLabel($this->translate('Clone Report'))
->setAction((string) Url::fromRequest())
->populate($values)
Expand Down Expand Up @@ -148,7 +148,7 @@ public function editAction(): void
$values[$name] = $value;
}

$form = ReportForm::fromId($this->report->getId())
$form = ReportForm::fromId($this->report->getId(), Database::get())
->setAction((string) Url::fromRequest())
->populate($values)
->on(ReportForm::ON_SUCCESS, function (ReportForm $form) {
Expand Down
2 changes: 1 addition & 1 deletion application/controllers/ReportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function newAction(): void
break;
}

$form = (new ReportForm())
$form = (new ReportForm(Database::get()))
->setAction((string) Url::fromRequest())
->setRenderCreateAndShowButton($class !== null)
->populate([
Expand Down
44 changes: 37 additions & 7 deletions library/Reporting/Web/Forms/ReportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@

use Icinga\Authentication\Auth;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Model\Report;
use Icinga\Module\Reporting\ProvidedReports;
use Icinga\Module\Reporting\RetryConnection;
use ipl\Html\Form;
use ipl\Html\HtmlDocument;
use ipl\Stdlib\Filter;
use ipl\Validator\CallbackValidator;
use ipl\Web\Compat\CompatForm;

class ReportForm extends CompatForm
{
use ProvidedReports;

/** @var ?int */
protected $id;

/** @var string Label to use for the submit button */
Expand All @@ -24,16 +28,25 @@ class ReportForm extends CompatForm
/** @var bool Whether to render the create and show submit button (is only used from DB Web's object detail) */
protected $renderCreateAndShowButton = false;

/** @var RetryConnection */
protected $db;

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

/**
* Create a new form instance with the given report id
*
* @param $id
* @param int $id
* @param RetryConnection $db
*
* @return static
*/
public static function fromId($id): self
public static function fromId(int $id, RetryConnection $db): self
{
$form = new static();
$form = new static($db);
$form->id = $id;

return $form;
Expand Down Expand Up @@ -95,7 +108,7 @@ public function hasBeenSubmitted(): bool
);
}

protected function assemble()
protected function assemble(): void
{
$this->addElement('text', 'name', [
'required' => true,
Expand All @@ -106,14 +119,32 @@ protected function assemble()
),
'validators' => [
'Callback' => function ($value, CallbackValidator $validator) {
if ($value !== null && strpos($value, '..') !== false) {
if (strpos($value, '..') !== false) {
$validator->addMessage(
$this->translate('Double dots are not allowed in the report name')
);

return false;
}

$filter = Filter::all(Filter::equal('name', $value));
if ($this->id) {
$filter->add(Filter::unequal('id', $this->id));
}

$report = Report::on($this->db)
->columns('1')
->filter($filter)
->first();

if ($report !== null) {
$validator->addMessage(
$this->translate('A report with this name already exists')
);

return false;
}

return true;
}
]
Expand Down Expand Up @@ -149,7 +180,6 @@ protected function assemble()

if (isset($values['reportlet'])) {
$config = new Form();
// $config->populate($this->getValues());

/** @var \Icinga\Module\Reporting\Hook\ReportHook $reportlet */
$reportlet = new $values['reportlet']();
Expand Down Expand Up @@ -188,7 +218,7 @@ protected function assemble()
}
}

public function onSuccess()
public function onSuccess(): void
{
$db = Database::get();

Expand Down
25 changes: 5 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ parameters:
count: 1
path: application/forms/SelectBackendForm.php

-
message: "#^Call to an undefined method React\\\\Promise\\\\PromiseInterface\\:\\:otherwise\\(\\)\\.$#"
count: 1
path: library/Reporting/Actions/SendMail.php

-
message: "#^Method Icinga\\\\Module\\\\Reporting\\\\Actions\\\\SendMail\\:\\:execute\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -680,31 +685,11 @@ parameters:
count: 1
path: library/Reporting/Web/Forms/ReportForm.php

-
message: "#^Method Icinga\\\\Module\\\\Reporting\\\\Web\\\\Forms\\\\ReportForm\\:\\:assemble\\(\\) has no return type specified\\.$#"
count: 1
path: library/Reporting/Web/Forms/ReportForm.php

-
message: "#^Method Icinga\\\\Module\\\\Reporting\\\\Web\\\\Forms\\\\ReportForm\\:\\:fromId\\(\\) has parameter \\$id with no type specified\\.$#"
count: 1
path: library/Reporting/Web/Forms/ReportForm.php

-
message: "#^Method Icinga\\\\Module\\\\Reporting\\\\Web\\\\Forms\\\\ReportForm\\:\\:listReports\\(\\) has no return type specified\\.$#"
count: 1
path: library/Reporting/Web/Forms/ReportForm.php

-
message: "#^Method Icinga\\\\Module\\\\Reporting\\\\Web\\\\Forms\\\\ReportForm\\:\\:onSuccess\\(\\) has no return type specified\\.$#"
count: 1
path: library/Reporting/Web/Forms/ReportForm.php

-
message: "#^Property Icinga\\\\Module\\\\Reporting\\\\Web\\\\Forms\\\\ReportForm\\:\\:\\$id has no type specified\\.$#"
count: 1
path: library/Reporting/Web/Forms/ReportForm.php

-
message: "#^Call to an undefined method object\\:\\:execute\\(\\)\\.$#"
count: 1
Expand Down