Skip to content

Commit 58560cf

Browse files
committed
Add support to restrict users using defined prefix
* Introduce new restriction 'reporting/prefix' * Introduce new permission 'reporting/reports/modify'
1 parent b10e452 commit 58560cf

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
@@ -62,7 +62,7 @@ public function indexAction()
6262

6363
public function editAction()
6464
{
65-
$this->assertPermission('reporting/reports');
65+
$this->assertPermission('reporting/reports/modify');
6666
$this->addTitleTab($this->translate('Edit Report'));
6767

6868
$values = [
@@ -199,7 +199,7 @@ protected function assembleActions()
199199

200200
$actions = new ActionBar();
201201

202-
if ($this->hasPermission('reporting/reports')) {
202+
if ($this->hasPermission('reporting/reports/modify')) {
203203
$actions->addHtml(
204204
new ActionLink(
205205
$this->translate('Modify'),

application/controllers/ReportsController.php

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

27-
if ($this->hasPermission('reporting/reports')) {
27+
if ($this->hasPermission('reporting/reports/modify')) {
2828
$this->addControl(new ButtonLink(
2929
$this->translate('New Report'),
3030
Url::fromPath('reporting/reports/new'),
@@ -41,16 +41,21 @@ public function indexAction()
4141
$reports = Report::on($this->getDb())
4242
->withColumns(['report.timeframe.name']);
4343

44+
$this->applyRestriction($reports, 'name');
45+
4446
foreach ($reports as $report) {
4547
$url = Url::fromPath('reporting/report', ['id' => $report->id])->getAbsoluteUrl('&');
4648

47-
$tableRows[] = Html::tag('tr', ['href' => $url], [
49+
$content = [
4850
Html::tag('td', null, $report->name),
4951
Html::tag('td', null, $report->author),
5052
Html::tag('td', null, $report->timeframe->name),
5153
Html::tag('td', null, $report->ctime->format('Y-m-d H:i')),
5254
Html::tag('td', null, $report->mtime->format('Y-m-d H:i')),
53-
Html::tag('td', ['class' => 'icon-col'], [
55+
];
56+
57+
if ($this->hasPermission('reporting/reports/modify')) {
58+
$content[] = Html::tag('td', ['class' => 'icon-col'], [
5459
new Link(
5560
new Icon('edit'),
5661
Url::fromPath('reporting/report/edit', ['id' => $report->id]),
@@ -59,8 +64,10 @@ public function indexAction()
5964
'data-no-icinga-ajax' => true
6065
]
6166
)
62-
])
63-
]);
67+
]);
68+
}
69+
70+
$tableRows[] = Html::tag('tr', ['href' => $url], $content);
6471
}
6572

6673
if (! empty($tableRows)) {
@@ -96,7 +103,7 @@ public function indexAction()
96103

97104
public function newAction()
98105
{
99-
$this->assertPermission('reporting/reports');
106+
$this->assertPermission('reporting/reports/modify');
100107
$this->addTitleTab($this->translate('New Report'));
101108

102109
$form = (new ReportForm())

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
@@ -52,17 +52,52 @@ protected function assemble()
5252
. ' and also when listing the reports in the cli'
5353
),
5454
'validators' => [
55-
'Callback' => function ($value, CallbackValidator $validator) {
56-
if ($value !== null && strpos($value, '..') !== false) {
57-
$validator->addMessage(
58-
$this->translate('Double dots are not allowed in the report name')
59-
);
55+
new CallbackValidator(
56+
function ($value, CallbackValidator $validator) {
57+
if ($value !== null && strpos($value, '..') !== false) {
58+
$validator->addMessage(
59+
$this->translate('Double dots are not allowed in the report name')
60+
);
6061

61-
return false;
62-
}
62+
return false;
63+
}
6364

64-
return true;
65-
}
65+
return true;
66+
}
67+
),
68+
new CallbackValidator(
69+
function ($value, $validator) {
70+
/** @var CallbackValidator $validator */
71+
$restrictions = Auth::getInstance()->getRestrictions('reporting/prefix');
72+
$prefixes = [];
73+
foreach ($restrictions as $restriction) {
74+
$prefixes = array_merge(
75+
$prefixes,
76+
explode(',', trim($restriction))
77+
);
78+
}
79+
80+
if (! empty($prefixes)) {
81+
foreach ($prefixes as $prefix) {
82+
$prefix = trim($prefix);
83+
if (substr($value, 0, strlen($prefix)) === $prefix) {
84+
return true;
85+
}
86+
}
87+
88+
$validator->addMessage(
89+
sprintf(
90+
$this->translate('Please prefix the name with "%s"'),
91+
implode(' | ', $prefixes)
92+
)
93+
);
94+
95+
return false;
96+
}
97+
98+
return true;
99+
}
100+
)
66101
]
67102
]);
68103

0 commit comments

Comments
 (0)