1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::GenericInterface::Event::Handler;
use strict;
use warnings;
use Kernel::System::VariableCheck qw(:all);
use Storable;
our @ObjectDependencies = (
'Kernel::GenericInterface::Requester',
'Kernel::System::Scheduler',
'Kernel::System::GenericInterface::Webservice',
'Kernel::System::Log',
'Kernel::System::Event',
'Kernel::System::Main',
'Kernel::Config',
'Kernel::System::Daemon::SchedulerDB',
'Kernel::System::DateTime',
);
=head1 NAME
Kernel::GenericInterface::Event::Handler - GenericInterface event handler
=head1 DESCRIPTION
This event handler intercepts all system events and fires connected GenericInterface
invokers.
=cut
sub new {
my ( $Type, %Param ) = @_;
# Allocate new hash for object.
my $Self = {};
bless( $Self, $Type );
return $Self;
}
sub Run {
my ( $Self, %Param ) = @_;
my $LogObject = $Kernel::OM->Get('Kernel::System::Log');
for my $Needed (qw(Data Event Config)) {
if ( !$Param{$Needed} ) {
$LogObject->Log(
Priority => 'error',
Message => "Need $Needed!"
);
return;
}
}
my $WebserviceObject = $Kernel::OM->Get('Kernel::System::GenericInterface::Webservice');
my $SchedulerObject = $Kernel::OM->Get('Kernel::System::Scheduler');
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
my $RequesterObject = $Kernel::OM->Get('Kernel::GenericInterface::Requester');
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my %WebserviceList = %{ $WebserviceObject->WebserviceList( Valid => 1 ) };
my %RegisteredEvents = $Kernel::OM->Get('Kernel::System::Event')->EventList();
# Loop over web services.
WEBSERVICEID:
for my $WebserviceID ( sort keys %WebserviceList ) {
my $WebserviceData = $WebserviceObject->WebserviceGet(
ID => $WebserviceID,
);
next WEBSERVICEID if !IsHashRefWithData( $WebserviceData->{Config} );
next WEBSERVICEID if !IsHashRefWithData( $WebserviceData->{Config}->{Requester} );
next WEBSERVICEID if !IsHashRefWithData( $WebserviceData->{Config}->{Requester}->{Invoker} );
# Check invokers of the web service, to see if some might be connected to this event.
INVOKER:
for my $Invoker ( sort keys %{ $WebserviceData->{Config}->{Requester}->{Invoker} } ) {
my $InvokerConfig = $WebserviceData->{Config}->{Requester}->{Invoker}->{$Invoker};
next INVOKER if ref $InvokerConfig->{Events} ne 'ARRAY';
INVOKEREVENT:
for my $InvokerEvent ( @{ $InvokerConfig->{Events} } ) {
# Check if the invoker is connected to this event.
next INVOKEREVENT if !IsHashRefWithData($InvokerEvent);
next INVOKEREVENT if !IsStringWithData( $InvokerEvent->{Event} );
next INVOKEREVENT if $InvokerEvent->{Event} ne $Param{Event};
# Prepare event type.
my $EventType;
# Set the event type (event object like Article or Ticket) and event condition
EVENTTYPE:
for my $Type ( sort keys %RegisteredEvents ) {
my $EventFound = grep { $_ eq $InvokerEvent->{Event} } @{ $RegisteredEvents{$Type} || [] };
next EVENTTYPE if !$EventFound;
$EventType = $Type;
last EVENTTYPE;
}
if (
$EventType
&& IsHashRefWithData( $InvokerEvent->{Condition} )
&& IsHashRefWithData( $InvokerEvent->{Condition}->{Condition} )
)
{
my $BackendObject = $Self->{EventTypeBackendObject}->{$EventType};
if ( !$BackendObject ) {
my $ObjectClass = "Kernel::GenericInterface::Event::ObjectType::$EventType";
my $Loaded = $MainObject->Require(
$ObjectClass,
);
if ( !$Loaded ) {
$LogObject->Log(
Priority => 'error',
Message =>
"Could not load $ObjectClass, skipping condition checks for event $InvokerEvent->{Event}!",
);
next INVOKEREVENT;
}
$BackendObject = $Kernel::OM->Get($ObjectClass);
$Self->{EventTypeBackendObject}->{$EventType} = $BackendObject;
}
# Get object data
my %EventData = $BackendObject->DataGet(
Data => $Param{Data},
);
if ( IsHashRefWithData( \%EventData ) ) {
my %ObjectData;
$Self->_SerializeConfig(
Data => \%EventData,
SHash => \%ObjectData,
);
# Check if the event condition matches.
my $ConditionCheckResult = $Self->_ConditionCheck(
%{ $InvokerEvent->{Condition} },
Data => \%ObjectData,
);
next INVOKEREVENT if !$ConditionCheckResult;
}
}
# create scheduler task for asynchronous tasks
if ( $InvokerEvent->{Asynchronous} ) {
my $Success = $SchedulerObject->TaskAdd(
Type => 'GenericInterface',
Name => 'Invoker-' . $Invoker,
Attempts => 10,
Data => {
WebserviceID => $WebserviceID,
Invoker => $Invoker,
Data => $Param{Data},
},
);
if ( !$Success ) {
$LogObject->Log(
Priority => 'error',
Message => 'Could not schedule task for Invoker-' . $Invoker,
);
}
next INVOKEREVENT;
}
# execute synchronous tasks directly
my $Result = $RequesterObject->Run(
WebserviceID => $WebserviceID,
Invoker => $Invoker,
Data => Storable::dclone( $Param{Data} ),
);
next INVOKEREVENT if $Result->{Success};
# check if rescheduling is requested on errors
next INVOKEREVENT if !IsHashRefWithData( $Result->{Data} );
next INVOKEREVENT if !$Result->{Data}->{ReSchedule};
# Use the execution time from the return data
my $ExecutionTime = $Result->{Data}->{ExecutionTime};
my $ExecutionDateTime;
# Check if execution time is valid.
if ( IsStringWithData($ExecutionTime) ) {
$ExecutionDateTime = $Kernel::OM->Create(
'Kernel::System::DateTime',
ObjectParams => {
String => $ExecutionTime,
},
);
if ( !$ExecutionDateTime ) {
my $WebServiceName = $WebserviceData->{Name} // 'N/A';
$LogObject->Log(
Priority => 'error',
Message =>
"WebService $WebServiceName, Invoker $Invoker returned invalid execution time $ExecutionTime. Falling back to default!",
);
}
}
# Set default execution time.
if ( !$ExecutionTime || !$ExecutionDateTime ) {
# Get default time difference from config.
my $FutureTaskTimeDiff
= int( $ConfigObject->Get('Daemon::SchedulerGenericInterfaceTaskManager::FutureTaskTimeDiff') )
|| 300;
$ExecutionDateTime = $Kernel::OM->Create('Kernel::System::DateTime');
$ExecutionDateTime->Add( Seconds => $FutureTaskTimeDiff );
}
# Create a new task that will be executed in the future.
my $Success = $SchedulerObject->TaskAdd(
ExecutionTime => $ExecutionDateTime->ToString(),
Type => 'GenericInterface',
Name => 'Invoker-' . $Invoker,
Attempts => 10,
Data => {
Data => $Param{Data},
PastExecutionData => $Result->{Data}->{PastExecutionData},
WebserviceID => $WebserviceID,
Invoker => $Invoker,
},
);
if ( !$Success ) {
$LogObject->Log(
Priority => 'error',
Message => 'Could not re-schedule a task in future for Invoker ' . $Invoker,
);
}
}
}
}
return 1;
}
=head2 _SerializeConfig()
returns a serialized hash/array of a given hash/array
my $ConditionCheck = $Self->_SerializeConfig(
Data => \%OldHash,
SHash => \%NewHash,
);
Modifies NewHash (SHash):
my %OldHash = (
Config => {
A => 1,
B => 2,
C => 3,
},
Config2 => 1
);
my %NewHash = (
Config_A => 1,
Config_B => 1,
Config_C => 1,
Config2 => 1,
);
=cut
sub _SerializeConfig {
my ( $Self, %Param ) = @_;
for my $Needed (qw(Data SHash)) {
if ( !$Param{$Needed} ) {
print "Got no $Needed!\n";
return;
}
}
my @ConfigContainer;
my $DataType = 'Hash';
if ( IsHashRefWithData( $Param{Data} ) ) {
@ConfigContainer = sort keys %{ $Param{Data} };
}
else {
@ConfigContainer = @{ $Param{Data} };
$DataType = 'Array';
}
# Prepare prefix.
my $Prefix = $Param{Prefix} || '';
my $ArrayCount = 0;
CONFIGITEM:
for my $ConfigItem (@ConfigContainer) {
next CONFIGITEM if !$ConfigItem;
# Check if param data is a hash or an array ref.
if ( $DataType eq 'Hash' ) {
# We got a hash ref.
if (
IsHashRefWithData( $Param{Data}->{$ConfigItem} )
|| IsArrayRefWithData( $Param{Data}->{$ConfigItem} )
)
{
$Self->_SerializeConfig(
Data => $Param{Data}->{$ConfigItem},
SHash => $Param{SHash},
Prefix => $Prefix . $ConfigItem . '_',
);
}
else {
$Prefix = $Prefix . $ConfigItem;
$Param{SHash}->{$Prefix} = $Param{Data}->{$ConfigItem};
$Prefix = $Param{Prefix} || '';
}
}
# We got an array ref
else {
if ( IsHashRefWithData($ConfigItem) || IsArrayRefWithData($ConfigItem) ) {
$Self->_SerializeConfig(
Data => $ConfigItem,
SHash => $Param{SHash},
Prefix => $Prefix . $ConfigItem . '_',
);
}
else {
$Prefix = $Prefix . $ArrayCount;
$Param{SHash}->{$Prefix} = $ConfigItem;
$Prefix = $Param{Prefix} || '';
}
$ArrayCount++;
}
}
return 1;
}
=head2 _ConditionCheck()
Checks if one or more conditions are true
my $ConditionCheck = $Self->_ConditionCheck(
ConditionLinking => 'and',
Condition => {
1 => {
Type => 'and',
Fields => {
DynamicField_Make => [ '2' ],
DynamicField_VWModel => {
Type => 'String',
Match => 'Golf',
},
DynamicField_A => {
Type => 'Hash',
Match => {
Value => 1,
},
},
DynamicField_B => {
Type => 'Regexp',
Match => qr{ [\n\r\f] }xms
},
DynamicField_C => {
Type => 'Module',
Match =>
'Kernel::GenericInterface::Event::Validation::MyModule',
},
Queue => {
Type => 'Array',
Match => [ 'Raw' ],
},
# ...
},
},
# ...
},
Data => {
Queue => 'Raw',
DynamicField1 => 'Value',
Subject => 'Testsubject',
# ...
},
);
Returns:
$CheckResult = 1; # 1 = process with Scheduler or Requester
# 0 = stop processing
=cut
sub _ConditionCheck {
my ( $Self, %Param ) = @_;
my $LogObject = $Kernel::OM->Get('Kernel::System::Log');
for my $Needed (qw(Condition Data)) {
if ( !defined $Param{$Needed} ) {
$LogObject->Log(
Priority => 'error',
Message => "Need $Needed!",
);
return;
}
}
# Check if we have Data to check against Condition.
if ( !IsHashRefWithData( $Param{Data} ) ) {
$LogObject->Log(
Priority => 'error',
Message => "Data has no values!",
);
return;
}
# Check if we have Condition to check against Data.
if ( !IsHashRefWithData( $Param{Condition} ) ) {
$LogObject->Log(
Priority => 'error',
Message => "Condition has no values!",
);
return;
}
my $ConditionLinking = $Param{ConditionLinking} || 'and';
# If there is something else than 'and', 'or', 'xor' log defect condition configuration
if (
$ConditionLinking ne 'and'
&& $ConditionLinking ne 'or'
&& $ConditionLinking ne 'xor'
)
{
$LogObject->Log(
Priority => 'error',
Message => "Invalid ConditionLinking!",
);
return;
}
my ( $ConditionSuccess, $ConditionFail ) = ( 0, 0 );
# Loop through all submitted conditions
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
CONDITIONNAME:
for my $ConditionName ( sort { $a cmp $b } keys %{ $Param{Condition} } ) {
next CONDITIONNAME if $ConditionName eq 'ConditionLinking';
# Get the condition data.
my $ActualCondition = $Param{Condition}->{$ConditionName};
# Check if we have Fields in our Condition
if ( !IsHashRefWithData( $ActualCondition->{Fields} ) )
{
$LogObject->Log(
Priority => 'error',
Message => "No Fields in Condition->$ConditionName found!",
);
return;
}
# If we don't have a Condition->$Condition->Type, set it to 'and' by default
my $CondType = $ActualCondition->{Type} || 'and';
# If there is something else than 'and', 'or', 'xor' log defect condition configuration
if ( $CondType ne 'and' && $CondType ne 'or' && $CondType ne 'xor' ) {
$LogObject->Log(
Priority => 'error',
Message => "Invalid Condition->$ConditionName->Type!",
);
return;
}
my ( $FieldSuccess, $FieldFail ) = ( 0, 0 );
FIELDLNAME:
for my $FieldName ( sort keys %{ $ActualCondition->{Fields} } ) {
# If we have just a String transform it into string check condition.
if ( ref $ActualCondition->{Fields}->{$FieldName} eq '' ) {
$ActualCondition->{Fields}->{$FieldName} = {
Type => 'String',
Match => $ActualCondition->{Fields}->{$FieldName},
};
}
# If we have an Array ref in "Fields" we deal with just values
# -> transform it into a { Type => 'Array', Match => [1,2,3,4] } structure
# to unify testing later on.
if ( ref $ActualCondition->{Fields}->{$FieldName} eq 'ARRAY' ) {
$ActualCondition->{Fields}->{$FieldName} = {
Type => 'Array',
Match => $ActualCondition->{Fields}->{$FieldName},
};
}
# If we don't have a Condition->$ConditionName->Fields->Field->Type
# set it to 'String' by default.
my $FieldType = $ActualCondition->{Fields}->{$FieldName}->{Type} || 'String';
# If there is something else than 'String', 'Regexp', 'Hash', 'Array', 'Module' log
# defect config.
if (
$FieldType ne 'String'
&& $FieldType ne 'Hash'
&& $FieldType ne 'Array'
&& $FieldType ne 'Regexp'
&& $FieldType ne 'Module'
)
{
$LogObject->Log(
Priority => 'error',
Message => "Invalid Condition->Type!",
);
return;
}
if ( $ActualCondition->{Fields}->{$FieldName}->{Type} eq 'String' ) {
# If our Check contains anything else than a string we can't check,
# Special Condition: if Match contains '0' we can check
if (
(
!$ActualCondition->{Fields}->{$FieldName}->{Match}
&& $ActualCondition->{Fields}->{$FieldName}->{Match} ne '0'
)
|| ref $ActualCondition->{Fields}->{$FieldName}->{Match}
)
{
$LogObject->Log(
Priority => 'error',
Message =>
"Condition->$ConditionName->Fields->$FieldName Match must"
. " be a String if Type is set to String!",
);
return;
}
# Make sure the data string is here and it isn't a ref (array or whatsoever)
# then compare it to our Condition configuration.
if (
defined $Param{Data}->{$FieldName}
&& defined $ActualCondition->{Fields}->{$FieldName}->{Match}
&& ( $Param{Data}->{$FieldName} || $Param{Data}->{$FieldName} eq '0' )
)
{
my $Match;
# Check if field data is a string and compare directly.
if (
ref $Param{Data}->{$FieldName} eq ''
&& $ActualCondition->{Fields}->{$FieldName}->{Match} eq $Param{Data}->{$FieldName}
)
{
$Match = 1;
}
# Otherwise check if field data is and array and compare each element until
# one match.
elsif ( ref $Param{Data}->{$FieldName} eq 'ARRAY' ) {
ITEM:
for my $Item ( @{ $Param{Data}->{$FieldName} } ) {
if ( $ActualCondition->{Fields}->{$FieldName}->{Match} eq $Item ) {
$Match = 1;
last ITEM;
}
}
}
if ($Match) {
$FieldSuccess++;
# Successful check if we just need one matching Condition to make this Condition valid.
return 1 if $ConditionLinking eq 'or' && $CondType eq 'or';
next CONDITIONNAME if $ConditionLinking ne 'or' && $CondType eq 'or';
}
else {
$FieldFail++;
# Failed check if we have all 'and' conditions.
return if $ConditionLinking eq 'and' && $CondType eq 'and';
# Try next Condition if all Condition Fields have to be true.
next CONDITIONNAME if $CondType eq 'and';
}
next FIELDLNAME;
}
my @ArrayFields = grep { $_ =~ m{ \A \Q$FieldName\E _ \d+ \z }xms } keys %{ $Param{Data} };
if ( @ArrayFields && defined $ActualCondition->{Fields}->{$FieldName}->{Match} ) {
ARRAYFIELD:
for my $ArrayField (@ArrayFields) {
next ARRAYFIELD if ref $Param{Data}->{$ArrayField} ne '';
if ( $Param{Data}->{$ArrayField} ne $ActualCondition->{Fields}->{$FieldName}->{Match} ) {
next ARRAYFIELD;
}
$FieldSuccess++;
# Successful check if we just need one matching Condition to make this Condition valid.
return 1 if $ConditionLinking eq 'or' && $CondType eq 'or';
next CONDITIONNAME if $ConditionLinking ne 'or' && $CondType eq 'or';
next FIELDLNAME;
}
}
# No match = fail.
$FieldFail++;
# Failed check if we have all 'and' conditions
return if $ConditionLinking eq 'and' && $CondType eq 'and';
# Try next Condition if all Condition Fields have to be true
next CONDITIONNAME if $CondType eq 'and';
next FIELDLNAME;
}
elsif ( $ActualCondition->{Fields}->{$FieldName}->{Type} eq 'Array' ) {
# 1. Go through each Condition->$ConditionName->Fields->$Field->Value (map).
# 2. Assign the value to $CheckValue.
# 3. Grep through $Data->{$Field} to find the "toCheck" value inside the Data->{$Field} Array
# 4. Assign all found Values to @CheckResults.
my $CheckValue;
my @CheckResults =
map {
$CheckValue = $_;
grep { $CheckValue eq $_ } @{ $Param{Data}->{$FieldName} }
}
@{ $ActualCondition->{Fields}->{$FieldName}->{Match} };
# If the found amount is the same as the "toCheck" amount we succeeded
if (
scalar @CheckResults
== scalar @{ $ActualCondition->{Fields}->{$FieldName}->{Match} }
)
{
$FieldSuccess++;
# Successful check if we just need one matching Condition to make this Condition valid.
return 1 if $ConditionLinking eq 'or' && $CondType eq 'or';
next CONDITIONNAME if $ConditionLinking ne 'or' && $CondType eq 'or';
}
else {
$FieldFail++;
# Failed check if we have all 'and' conditions.
return if $ConditionLinking eq 'and' && $CondType eq 'and';
# Try next Condition if all Condition Fields have to be true.
next CONDITIONNAME if $CondType eq 'and';
}
next FIELDLNAME;
}
elsif ( $ActualCondition->{Fields}->{$FieldName}->{Type} eq 'Hash' ) {
# if our Check doesn't contain a hash.
if ( ref $ActualCondition->{Fields}->{$FieldName}->{Match} ne 'HASH' ) {
$LogObject->Log(
Priority => 'error',
Message =>
"Condition->$ConditionName->Fields->$FieldName Match must"
. " be a Hash!",
);
return;
}
# If we have no data or Data isn't a hash, test failed.
if (
!$Param{Data}->{$FieldName}
|| ref $Param{Data}->{$FieldName} ne 'HASH'
)
{
$FieldFail++;
next FIELDLNAME;
}
# Find all Data Hash values that equal to the Condition Match Values.
my @CheckResults =
grep {
$Param{Data}->{$FieldName}->{$_} eq
$ActualCondition->{Fields}->{$FieldName}->{Match}->{$_}
}
keys %{ $ActualCondition->{Fields}->{$FieldName}->{Match} };
# If the amount of Results equals the amount of Keys in our hash this part matched.
if (
scalar @CheckResults
== scalar keys %{ $ActualCondition->{Fields}->{$FieldName}->{Match} }
)
{
$FieldSuccess++;
# Successful check if we just need one matching Condition to make this condition valid.
return 1 if $ConditionLinking eq 'or' && $CondType eq 'or';
next CONDITIONNAME if $ConditionLinking ne 'or' && $CondType eq 'or';
}
else {
$FieldFail++;
# Failed check if we have all 'and' conditions.
return if $ConditionLinking eq 'and' && $CondType eq 'and';
# Try next Condition if all Condition Fields have to be true.
next CONDITIONNAME if $CondType eq 'and';
}
next FIELDLNAME;
}
elsif ( $ActualCondition->{Fields}->{$FieldName}->{Type} eq 'Regexp' )
{
# If our Check contains anything else then a string we can't check.
if (
!$ActualCondition->{Fields}->{$FieldName}->{Match}
||
(
ref $ActualCondition->{Fields}->{$FieldName}->{Match} ne 'Regexp'
&& ref $ActualCondition->{Fields}->{$FieldName}->{Match} ne ''
)
)
{
$LogObject->Log(
Priority => 'error',
Message =>
"Condition->$ConditionName->Fields->$FieldName Match must"
. " be a Regular expression if Type is set to Regexp!",
);
return;
}
# Precompile Regexp if is a string.
if ( ref $ActualCondition->{Fields}->{$FieldName}->{Match} eq '' ) {
my $Match = $ActualCondition->{Fields}->{$FieldName}->{Match};
eval {
$ActualCondition->{Fields}->{$FieldName}->{Match} = qr{$Match};
};
if ($@) {
$LogObject->Log(
Priority => 'error',
Message => $@,
);
return;
}
}
# Make sure there is data to compare.
if ( $Param{Data}->{$FieldName} ) {
my $Match;
# Check if field data is a string and compare directly.
if (
ref $Param{Data}->{$FieldName} eq ''
&& $Param{Data}->{$FieldName} =~ $ActualCondition->{Fields}->{$FieldName}->{Match}
)
{
$Match = 1;
}
# Otherwise check if field data is and array and compare each element until one match.
elsif ( ref $Param{Data}->{$FieldName} eq 'ARRAY' ) {
ITEM:
for my $Item ( @{ $Param{Data}->{$FieldName} } ) {
if ( $Item =~ $ActualCondition->{Fields}->{$FieldName}->{Match} ) {
$Match = 1;
last ITEM;
}
}
}
if ($Match) {
$FieldSuccess++;
# Successful check if we just need one matching Condition to make this Transition valid.
return 1 if $ConditionLinking eq 'or' && $CondType eq 'or';
next CONDITIONNAME if $ConditionLinking ne 'or' && $CondType eq 'or';
}
else {
$FieldFail++;
# Failed check if we have all 'and' conditions.
return if $ConditionLinking eq 'and' && $CondType eq 'and';
# Try next Condition if all Condition Fields have to be true.
next CONDITIONNAME if $CondType eq 'and';
}
next FIELDLNAME;
}
my @ArrayFields = grep { $_ =~ m{ \A \Q$FieldName\E _ \d+ \z }xms } keys %{ $Param{Data} };
if ( @ArrayFields && defined $ActualCondition->{Fields}->{$FieldName}->{Match} ) {
ARRAYFIELD:
for my $ArrayField (@ArrayFields) {
next ARRAYFIELD if ref $Param{Data}->{$ArrayField} ne '';
if ( $Param{Data}->{$ArrayField} !~ $ActualCondition->{Fields}->{$FieldName}->{Match} ) {
next ARRAYFIELD;
}
$FieldSuccess++;
# Successful check if we just need one matching Condition to make this Condition valid.
return 1 if $ConditionLinking eq 'or' && $CondType eq 'or';
next CONDITIONNAME if $ConditionLinking ne 'or' && $CondType eq 'or';
next FIELDLNAME;
}
}
# No match = fail.
$FieldFail++;
# Failed check if we have all 'and' conditions
return if $ConditionLinking eq 'and' && $CondType eq 'and';
# Try next Condition if all Condition Fields have to be true
next CONDITIONNAME if $CondType eq 'and';
next FIELDLNAME;
}
elsif ( $ActualCondition->{Fields}->{$FieldName}->{Type} eq 'Module' ) {
# Load Validation Modules. Default location for validation modules:
# Kernel/GenericInterface/Event/Validation/
if (
!$MainObject->Require(
$ActualCondition->{Fields}->{$FieldName}->{Match}
)
)
{
$LogObject->Log(
Priority => 'error',
Message => "Can't load "
. $ActualCondition->{Fields}->{$FieldName}->{Type}
. "Module for Condition->$ConditionName->Fields->$FieldName validation!",
);
return;
}
# Create new ValidateModuleObject.
my $ValidateModuleObject = $Kernel::OM->Get(
$ActualCondition->{Fields}->{$FieldName}->{Match}
);
# Handle "Data" Param to ValidateModule's "Validate" subroutine.
if ( $ValidateModuleObject->Validate( Data => $Param{Data} ) ) {
$FieldSuccess++;
# Successful check if we just need one matching Condition to make this Condition valid.
return 1 if $ConditionLinking eq 'or' && $CondType eq 'or';
next CONDITIONNAME if $ConditionLinking ne 'or' && $CondType eq 'or';
}
else {
$FieldFail++;
# Failed check if we have all 'and' conditions.
return if $ConditionLinking eq 'and' && $CondType eq 'and';
# Try next Condition if all Condition Fields have to be true.
next CONDITIONNAME if $CondType eq 'and';
}
next FIELDLNAME;
}
}
# FIELDLNAME end.
if ( $CondType eq 'and' ) {
# If we had no failing check this condition matched.
if ( !$FieldFail ) {
# Successful check if we just need one matching Condition to make this Condition valid.
return 1 if $ConditionLinking eq 'or';
$ConditionSuccess++;
}
else {
$ConditionFail++;
# Failed check if we have all 'and' condition.s
return if $ConditionLinking eq 'and';
}
}
elsif ( $CondType eq 'or' )
{
# If we had at least one successful check, this condition matched.
if ( $FieldSuccess > 0 ) {
# Successful check if we just need one matching Condition to make this Condition valid.
return 1 if $ConditionLinking eq 'or';
$ConditionSuccess++;
}
else {
$ConditionFail++;
# Failed check if we have all 'and' conditions.
return if $ConditionLinking eq 'and';
}
}
elsif ( $CondType eq 'xor' )
{
# If we had exactly one successful check, this condition matched.
if ( $FieldSuccess == 1 ) {
# Successful check if we just need one matching Condition to make this Condition valid.
return 1 if $ConditionLinking eq 'or';
$ConditionSuccess++;
}
else {
$ConditionFail++;
}
}
}
# CONDITIONNAME end.
if ( $ConditionLinking eq 'and' ) {
# If we had no failing conditions this Condition matched.
return 1 if !$ConditionFail;
}
elsif ( $ConditionLinking eq 'or' )
{
# If we had at least one successful condition, this condition matched.
return 1 if $ConditionSuccess > 0;
}
elsif ( $ConditionLinking eq 'xor' )
{
# If we had exactly one successful condition, this condition matched.
return 1 if $ConditionSuccess == 1;
}
# If no condition matched till here, we failed.
return;
}
1;
=head1 TERMS AND CONDITIONS
This software is part of the OTRS project (L<https://otrs.org/>).
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (GPL). If you
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
=cut
|