77
88use Icinga \Application \Hook ;
99use Icinga \Application \Logger ;
10+ use Icinga \Exception \ConfigurationError ;
1011use Icinga \Exception \Http \HttpNotFoundException ;
1112use Icinga \Module \Notifications \Common \Auth ;
1213use Icinga \Module \Notifications \Common \Database ;
1314use Icinga \Module \Notifications \Common \Links ;
1415use Icinga \Module \Notifications \Forms \EventRuleConfigElements \NotificationConfigProvider ;
1516use Icinga \Module \Notifications \Forms \EventRuleConfigForm ;
1617use Icinga \Module \Notifications \Forms \EventRuleForm ;
17- use Icinga \Module \Notifications \Hook \V1 \SourceHook ;
18+ use Icinga \Module \Notifications \Hook \V2 \SourceHook ;
1819use Icinga \Module \Notifications \Model \Rule ;
1920use Icinga \Module \Notifications \Model \Source ;
21+ use Icinga \Module \Notifications \Util \RuleSerializer ;
2022use Icinga \Module \Notifications \Web \Control \SearchBar \ExtraTagSuggestions ;
2123use Icinga \Web \Notification ;
2224use Icinga \Web \Session ;
2325use ipl \Html \Contract \Form ;
2426use ipl \Html \Html ;
2527use ipl \Stdlib \Filter ;
2628use ipl \Web \Compat \CompatController ;
27- use ipl \Web \Compat \CompatForm ;
2829use ipl \Web \Url ;
2930use ipl \Web \Widget \Icon ;
3031use ipl \Web \Widget \Link ;
32+ use JsonException ;
3133use Psr \Http \Message \ServerRequestInterface ;
3234use Throwable ;
3335
@@ -217,7 +219,7 @@ public function searchEditorAction(): void
217219 }
218220
219221 $ hook = null ;
220- foreach (Hook::all ('Notifications/v1 /Source ' ) as $ h ) {
222+ foreach (Hook::all ('Notifications/v2 /Source ' ) as $ h ) {
221223 /** @var SourceHook $h */
222224 try {
223225 if ($ h ->getSourceType () === $ source ->type ) {
@@ -237,50 +239,37 @@ public function searchEditorAction(): void
237239 ), $ source ->type ));
238240 }
239241
240- if (! $ filter ) {
241- $ targets = $ hook ->getRuleFilterTargets ($ source ->id );
242- if (count ($ targets ) === 1 && ! is_array (reset ($ targets ))) {
243- $ filter = key ($ targets );
244- } else {
245- $ target = null ;
246- $ form = (new CompatForm ())
247- ->applyDefaultElementDecorators ()
248- ->setAction (Url::fromRequest ()->getAbsoluteUrl ())
249- ->addElement ('select ' , 'target ' , [
250- 'required ' => true ,
251- 'label ' => $ this ->translate ('Filter Target ' ),
252- 'options ' => ['' => ' - ' . $ this ->translate ('Please choose ' ) . ' - ' ] + $ targets ,
253- 'disabledOptions ' => ['' ]
254- ])
255- ->addElement ('submit ' , 'btn_submit ' , [
256- // translators: shown on a submit button to proceed to the next step of a form wizard
257- 'label ' => $ this ->translate ('Next ' )
258- ])
259- ->on (Form::ON_SUBMIT , function (CompatForm $ form ) use (&$ target ) {
260- $ target = $ form ->getValue ('target ' );
261- })
262- ->handleRequest ($ this ->getServerRequest ());
263-
264- if ($ target !== null ) {
265- $ filter = $ target ;
266- } else {
267- $ this ->addContent ($ form );
268- }
242+ $ queryStr = '' ;
243+ if ($ filter ) {
244+ try {
245+ $ data = json_decode ($ filter , true , flags: JSON_THROW_ON_ERROR );
246+ } catch (JsonException $ e ) {
247+ Logger::error ('Failed to parse rule filter configuration: %s (Error: %s) ' , $ filter , $ e );
248+ throw new ConfigurationError ($ this ->translate (
249+ 'Failed to parse rule filter configuration. Please contact your system administrator. '
250+ ));
269251 }
270- }
271252
272- if ($ filter ) {
273- $ form = $ hook ->getRuleFilterEditor ($ filter )
274- ->setAction (Url::fromRequest ()->with ('object_filter ' , $ filter )->getAbsoluteUrl ())
275- ->on (Form::ON_SUBMIT , function (Form $ form ) use ($ ruleId , $ hook ) {
276- $ this ->session ->set ('object_filter ' , $ hook ->serializeRuleFilter ($ form ));
277- $ this ->redirectNow (Links::eventRule ($ ruleId )->setParam ('_filterOnly ' ));
278- })
279- ->handleRequest ($ this ->getServerRequest ());
280-
281- $ this ->getDocument ()->addHtml ($ form );
253+ if (! isset ($ data ['filter ' ]) || $ data ['version ' ] !== 2 ) {
254+ Logger::error ('Invalid rule filter configuration: %s ' , $ filter );
255+ throw new ConfigurationError ($ this ->translate (
256+ 'Invalid rule filter configuration. Please contact your system administrator. '
257+ ));
258+ }
259+
260+ $ queryStr = $ data ['filter ' ];
282261 }
283262
263+ $ form = $ hook ->getRuleFilterEditor ($ queryStr )
264+ ->setAction (Url::fromRequest ()->with ('object_filter ' , $ filter )->getAbsoluteUrl ())
265+ ->on (Form::ON_SUBMIT , function (Form $ form ) use ($ ruleId , $ hook ) {
266+ $ this ->session ->set ('object_filter ' , (new RuleSerializer ($ form ->getFilter ()))->getJson ());
267+ $ this ->redirectNow (Links::eventRule ($ ruleId )->setParam ('_filterOnly ' ));
268+ })
269+ ->handleRequest ($ this ->getServerRequest ());
270+
271+ $ this ->getDocument ()->addHtml ($ form );
272+
284273 $ this ->setTitle ($ this ->translate ('Adjust Filter ' ));
285274 }
286275
0 commit comments