Skip to content

Commit 2c0d71c

Browse files
jhoxhaayhabteab
authored andcommitted
Add support to restrict users using defined prefix
* Introduce new restriction 'reporting/prefix' * Introduce new permission 'reporting/reports/modify'
1 parent 73dae07 commit 2c0d71c

File tree

5 files changed

+94
-17
lines changed

5 files changed

+94
-17
lines changed

application/controllers/ReportController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function cloneAction()
9797

9898
public function editAction()
9999
{
100-
$this->assertPermission('reporting/reports');
100+
$this->assertPermission('reporting/reports/modify');
101101
$this->addTitleTab($this->translate('Edit Report'));
102102

103103
$values = [
@@ -234,7 +234,7 @@ protected function assembleActions()
234234

235235
$actions = new ActionBar();
236236

237-
if ($this->hasPermission('reporting/reports')) {
237+
if ($this->hasPermission('reporting/reports/modify')) {
238238
$actions->addHtml(
239239
new ActionLink(
240240
$this->translate('Modify'),

application/controllers/ReportsController.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function indexAction()
2626
{
2727
$this->createTabs()->activate('reports');
2828

29-
if ($this->hasPermission('reporting/reports')) {
29+
if ($this->hasPermission('reporting/reports/modify')) {
3030
$this->addControl(new ButtonLink(
3131
$this->translate('New Report'),
3232
Url::fromPath('reporting/reports/new'),
@@ -43,6 +43,8 @@ public function indexAction()
4343
$reports = Report::on($this->getDb())
4444
->withColumns(['report.timeframe.name']);
4545

46+
$this->applyRestriction($reports, 'name');
47+
4648
$sortControl = $this->createSortControl(
4749
$reports,
4850
[
@@ -58,13 +60,16 @@ public function indexAction()
5860
foreach ($reports as $report) {
5961
$url = Url::fromPath('reporting/report', ['id' => $report->id])->getAbsoluteUrl('&');
6062

61-
$tableRows[] = Html::tag('tr', ['href' => $url], [
63+
$content = [
6264
Html::tag('td', null, $report->name),
6365
Html::tag('td', null, $report->author),
6466
Html::tag('td', null, $report->timeframe->name),
6567
Html::tag('td', null, $report->ctime->format('Y-m-d H:i')),
6668
Html::tag('td', null, $report->mtime->format('Y-m-d H:i')),
67-
Html::tag('td', ['class' => 'icon-col'], [
69+
];
70+
71+
if ($this->hasPermission('reporting/reports/modify')) {
72+
$content[] = Html::tag('td', ['class' => 'icon-col'], [
6873
new Link(
6974
new Icon('edit'),
7075
Url::fromPath('reporting/report/edit', ['id' => $report->id]),
@@ -73,8 +78,10 @@ public function indexAction()
7378
'data-no-icinga-ajax' => true
7479
]
7580
)
76-
])
77-
]);
81+
]);
82+
}
83+
84+
$tableRows[] = Html::tag('tr', ['href' => $url], $content);
7885
}
7986

8087
if (! empty($tableRows)) {
@@ -110,7 +117,7 @@ public function indexAction()
110117

111118
public function newAction()
112119
{
113-
$this->assertPermission('reporting/reports');
120+
$this->assertPermission('reporting/reports/modify');
114121
$this->addTitleTab($this->translate('New Report'));
115122

116123
switch ($this->params->shift('report')) {

configuration.php

+10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
$this->translate('Allow managing reports')
3737
);
3838

39+
$this->providePermission(
40+
'reporting/reports/modify',
41+
$this->translate('Allow creating, editing and removing reports')
42+
);
43+
3944
$this->providePermission(
4045
'reporting/schedules',
4146
$this->translate('Allow managing schedules')
@@ -50,4 +55,9 @@
5055
'reporting/timeframes',
5156
$this->translate('Allow managing timeframes')
5257
);
58+
59+
$this->provideRestriction(
60+
'reporting/prefix',
61+
$this->translate('Restrict access to reports with the given prefix')
62+
);
5363
}

library/Reporting/Web/Controller.php

+25
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,33 @@
44

55
namespace Icinga\Module\Reporting\Web;
66

7+
use Icinga\Authentication\Auth;
8+
use ipl\Orm\Query;
9+
use ipl\Stdlib\Filter;
710
use ipl\Web\Compat\CompatController;
811

912
class Controller extends CompatController
1013
{
14+
/**
15+
* @param Query $query
16+
* @param string $column
17+
* @return void
18+
*/
19+
protected function applyRestriction(Query $query, string $column)
20+
{
21+
$restrictions = Auth::getInstance()->getRestrictions('reporting/prefix');
22+
$prefixes = [];
23+
foreach ($restrictions as $restriction) {
24+
$prefixes = array_merge(
25+
$prefixes,
26+
explode(', ', trim($restriction))
27+
);
28+
}
29+
30+
if (! empty($prefixes)) {
31+
foreach ($prefixes as $prefix) {
32+
$query->orFilter(Filter::like($column, $prefix . '*'));
33+
}
34+
}
35+
}
1136
}

library/Reporting/Web/Forms/ReportForm.php

+44-9
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,52 @@ protected function assemble()
8383
. ' and also when listing the reports in the cli'
8484
),
8585
'validators' => [
86-
'Callback' => function ($value, CallbackValidator $validator) {
87-
if ($value !== null && strpos($value, '..') !== false) {
88-
$validator->addMessage(
89-
$this->translate('Double dots are not allowed in the report name')
90-
);
86+
new CallbackValidator(
87+
function ($value, CallbackValidator $validator) {
88+
if ($value !== null && strpos($value, '..') !== false) {
89+
$validator->addMessage(
90+
$this->translate('Double dots are not allowed in the report name')
91+
);
9192

92-
return false;
93-
}
93+
return false;
94+
}
9495

95-
return true;
96-
}
96+
return true;
97+
}
98+
),
99+
new CallbackValidator(
100+
function ($value, $validator) {
101+
/** @var CallbackValidator $validator */
102+
$restrictions = Auth::getInstance()->getRestrictions('reporting/prefix');
103+
$prefixes = [];
104+
foreach ($restrictions as $restriction) {
105+
$prefixes = array_merge(
106+
$prefixes,
107+
explode(',', trim($restriction))
108+
);
109+
}
110+
111+
if (! empty($prefixes)) {
112+
foreach ($prefixes as $prefix) {
113+
$prefix = trim($prefix);
114+
if (substr($value, 0, strlen($prefix)) === $prefix) {
115+
return true;
116+
}
117+
}
118+
119+
$validator->addMessage(
120+
sprintf(
121+
$this->translate('Please prefix the name with "%s"'),
122+
implode(' | ', $prefixes)
123+
)
124+
);
125+
126+
return false;
127+
}
128+
129+
return true;
130+
}
131+
)
97132
]
98133
]);
99134

0 commit comments

Comments
 (0)