-
Notifications
You must be signed in to change notification settings - Fork 75
/
SysConfig.pm
6393 lines (5009 loc) · 200 KB
/
SysConfig.pm
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
1000
# --
# OTOBO is a web-based ticketing system for service organisations.
# --
# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
# Copyright (C) 2019-2024 Rother OSS GmbH, https://otobo.io/
# --
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# --
package Kernel::System::SysConfig;
use v5.24;
use strict;
use warnings;
use namespace::autoclean;
use utf8;
use parent qw(Kernel::System::AsynchronousExecutor);
# core modules
use File::Path qw(make_path);
# CPAN modules
# OTOBO modules
use Kernel::System::VariableCheck qw(:all);
use Kernel::Language qw(Translatable);
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::Language',
'Kernel::Output::HTML::SysConfig',
'Kernel::System::Cache',
'Kernel::System::Encode',
'Kernel::System::Log',
'Kernel::System::Main',
'Kernel::System::Package',
'Kernel::System::Storable',
'Kernel::System::Storage::S3',
'Kernel::System::SysConfig::DB',
'Kernel::System::SysConfig::XML',
'Kernel::System::User',
'Kernel::System::YAML',
);
=head1 NAME
Kernel::System::SysConfig - Functions to manage system configuration settings.
=head1 DESCRIPTION
All functions to manage system configuration settings.
=head1 PUBLIC INTERFACE
=head2 new()
Don't use the constructor directly, use the ObjectManager instead:
my $SysConfigObject = $Kernel::OM->Get('Kernel::System::SysConfig');
=cut
sub new {
my ( $Type, %Param ) = @_;
# allocate new hash for object
my $Self = bless {}, $Type;
$Self->{ConfigObject} = $Kernel::OM->Get('Kernel::Config');
# get home directory and whether the S3 backend is active
$Self->{Home} = $Self->{ConfigObject}->Get('Home');
$Self->{S3Active} = $Kernel::OM->Get('Kernel::Config')->Get('Storage::S3::Active') ? 1 : 0;
# Kernel::Config is loaded because it was loaded by $Kernel::OM above.
$Self->{ConfigDefaultObject} = Kernel::Config->new( Level => 'Default' );
$Self->{ConfigObject} = Kernel::Config->new( Level => 'First' );
$Self->{ConfigClearObject} = Kernel::Config->new( Level => 'Clear' );
# Load base files.
my $BaseDir = $Self->{Home} . '/Kernel/System/SysConfig/Base/';
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
FILENAME:
for my $Filename (qw(Framework.pm SettingHistory.pm UserSetting.pm)) {
my $BaseFile = $BaseDir . $Filename;
next FILENAME unless -e $BaseFile;
$BaseFile =~ s{\A.*\/(.+?).pm\z}{$1}xms;
my $BaseClassName = "Kernel::System::SysConfig::Base::$BaseFile";
if ( !$MainObject->RequireBaseClass($BaseClassName) ) {
$Self->FatalDie(
Message => "Could not load class $BaseClassName.",
);
}
}
return $Self;
}
=head2 SettingGet()
Get SysConfig setting attributes.
my %Setting = $SysConfigObject->SettingGet(
Name => 'Setting::Name', # (required) Setting name
Default => 1, # (optional) Returns the default setting attributes only
ModifiedID => '123', # (optional) Get setting value for given ModifiedID.
TargetUserID => 1, # (optional) Get setting value for specific user.
Deployed => 1, # (optional) Get deployed setting value. Default 0.
OverriddenInXML => 1, # (optional) Consider changes made in perl files. Default 0.
Translate => 1, # (optional) Translate translatable strings in EffectiveValue. Default 0.
NoLog => 1, # (optional) Do not log error if a setting does not exist.
NoCache => 1, # (optional) Do not create cache.
UserID => 1, # Required only if OverriddenInXML is set.
);
Returns:
%Setting = (
DefaultID => 123,
ModifiedID => 456, # optional
Name => "ProductName",
Description => "Defines the name of the application ...",
Navigation => "ASimple::Path::Structure",
IsInvisible => 1, # 1 or 0
IsReadonly => 0, # 1 or 0
IsRequired => 1, # 1 or 0
IsModified => 1, # 1 or 0
IsValid => 1, # 1 or 0
HasConfigLevel => 200,
UserModificationPossible => 0, # 1 or 0
UserModificationActive => 0, # 1 or 0
UserPreferencesGroup => 'Advanced', # optional
XMLContentRaw => "The XML structure as it is on the config file",
XMLContentParsed => "XML parsed to Perl",
XMLFilename => "Framework.xml",
EffectiveValue => "Product 6",
IsDirty => 1, # 1 or 0
ExclusiveLockGUID => 'A32CHARACTERLONGSTRINGFORLOCKING',
ExclusiveLockUserID => 1,
ExclusiveLockExpiryTime => '2016-05-29 11:09:04',
CreateTime => "2016-05-29 11:04:04",
CreateBy => 1,
ChangeTime => "2016-05-29 11:04:04",
ChangeBy => 1,
DefaultValue => 'Old default value',
OverriddenFileName => '/opt/otobo/Kernel/Config/Files/ZZZ.pm',
);
=cut
sub SettingGet {
my ( $Self, %Param ) = @_;
# Check needed stuff.
if ( !$Param{Name} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => 'Need Name!',
);
return;
}
if ( $Param{OverriddenInXML} && !$Param{UserID} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => 'UserID is needed when OverriddenInXML is set!',
);
return;
}
$Param{Translate} //= 0; # don't translate by default
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $SysConfigDBObject = $Kernel::OM->Get('Kernel::System::SysConfig::DB');
# Get default setting.
my %Setting = $SysConfigDBObject->DefaultSettingGet(
Name => $Param{Name},
NoCache => $Param{NoCache},
);
# setting was not found
if ( !%Setting ) {
# do not log an error if parameter NoLog is true
if ( !$Param{NoLog} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Setting $Param{Name} is invalid!",
);
}
return;
}
$Setting{DefaultValue} = $Setting{EffectiveValue};
# Return default setting if specified (otherwise continue with modified setting).
if ( $Param{Default} ) {
return %Setting;
}
# Check if modified setting available
my %ModifiedSetting;
if ( $Param{ModifiedID} ) {
# Get settings with given ModifiedID.
%ModifiedSetting = $SysConfigDBObject->ModifiedSettingGet(
ModifiedID => $Param{ModifiedID},
IsGlobal => 1,
NoCache => $Param{NoCache},
);
# prevent using both parameters.
$Param{Deployed} = undef;
$Param{TargetUserID} = undef;
}
else {
# Get latest modified setting.
%ModifiedSetting = $SysConfigDBObject->ModifiedSettingGet(
Name => $Param{Name},
IsGlobal => 1,
NoCache => $Param{NoCache},
);
}
if ( $Param{TargetUserID} ) {
if ( IsHashRefWithData( \%ModifiedSetting ) ) {
# There is modified setting, but we need last deployed version.
%ModifiedSetting = $SysConfigDBObject->ModifiedSettingVersionGetLast(
Name => $ModifiedSetting{Name},
);
# Use global (deployed) modified settings as "default" (if any)
if ( IsHashRefWithData( \%ModifiedSetting ) ) {
%Setting = (
%Setting,
%ModifiedSetting,
);
}
}
# get user specific settings
%ModifiedSetting = $SysConfigDBObject->ModifiedSettingGet(
Name => $Param{Name},
TargetUserID => $Param{TargetUserID},
);
# prevent using both parameters.
$Param{Deployed} = undef;
}
if ( $Param{Deployed} ) {
# get the previous deployed state of this setting
my %SettingDeployed = $SysConfigDBObject->ModifiedSettingVersionGetLast(
Name => $Setting{Name},
);
if ( !IsHashRefWithData( \%SettingDeployed ) ) {
# if this setting was never deployed before, get the default state
# Get default version.
%SettingDeployed = $SysConfigDBObject->DefaultSettingGet(
DefaultID => $Setting{DefaultID},
NoCache => $Param{NoCache},
);
}
if ( IsHashRefWithData( \%SettingDeployed ) ) {
%Setting = (
%Setting,
%SettingDeployed
);
}
}
# default
$Setting{IsModified} = 0;
if ( IsHashRefWithData( \%ModifiedSetting ) ) {
my $IsModified = DataIsDifferent(
Data1 => \$Setting{EffectiveValue},
Data2 => \$ModifiedSetting{EffectiveValue},
) || 0;
$IsModified ||= $ModifiedSetting{IsValid} != $Setting{IsValid};
$IsModified ||= $ModifiedSetting{UserModificationActive} != $Setting{UserModificationActive};
$Setting{IsModified} = $IsModified ? 1 : 0;
if ( !$Param{Deployed} ) {
# Update setting attributes.
ATTRIBUTE:
for my $Attribute (
qw(ModifiedID IsValid UserModificationActive EffectiveValue IsDirty
CreateTime CreateBy ChangeTime ChangeBy SettingUID
)
)
{
next ATTRIBUTE if !defined $ModifiedSetting{$Attribute};
$Setting{$Attribute} = $ModifiedSetting{$Attribute};
}
}
}
if ( $Param{OverriddenInXML} ) {
# get the previous deployed state of this setting
my %SettingDeployed = $SysConfigDBObject->ModifiedSettingVersionGetLast(
Name => $Setting{Name},
);
if ( !IsHashRefWithData( \%SettingDeployed ) ) {
# if this setting was never deployed before, get the default state
# Get default version.
%SettingDeployed = $SysConfigDBObject->DefaultSettingGet(
DefaultID => $Setting{DefaultID},
NoCache => $Param{NoCache},
);
}
# Get real EffectiveValue - EffectiveValue from DB could be modified in the ZZZAbc.pm file.
my $LoadedEffectiveValue = $Self->GlobalEffectiveValueGet(
SettingName => $Setting{Name},
);
my $IsOverridden = DataIsDifferent(
Data1 => $SettingDeployed{EffectiveValue} // {},
Data2 => $LoadedEffectiveValue // {},
);
if ($IsOverridden) {
$Setting{OverriddenFileName} = $Self->OverriddenFileNameGet(
SettingName => $Setting{Name},
EffectiveValue => $Setting{EffectiveValue},
UserID => $Param{UserID},
);
# Update EffectiveValue.
if ( $Setting{OverriddenFileName} ) {
$Setting{EffectiveValue} = $LoadedEffectiveValue;
}
}
}
if ( $Param{Translate} ) {
if (%ModifiedSetting) {
$Setting{XMLContentParsed}->{Value} = $Self->SettingModifiedXMLContentParsedGet(
ModifiedSetting => {
EffectiveValue => $Setting{EffectiveValue},
},
DefaultSetting => {
XMLContentParsed => $Setting{XMLContentParsed},
},
);
}
# Update EffectiveValue with translated strings
$Setting{EffectiveValue} = $Self->SettingEffectiveValueGet(
Value => $Setting{XMLContentParsed}->{Value},
Translate => 1,
);
$Setting{Description} = $Kernel::OM->Get('Kernel::Language')->Translate(
$Setting{Description},
);
}
# If setting is overridden in the perl file, using the "delete" statement, EffectiveValue is undef.
$Setting{EffectiveValue} //= '';
# Return updated default.
return %Setting;
}
=head2 SettingUpdate()
Update an existing SysConfig Setting.
my %Result = $SysConfigObject->SettingUpdate(
Name => 'Setting::Name', # (required) setting name
IsValid => 1, # (optional) 1 or 0, modified 0
EffectiveValue => $SettingEffectiveValue, # (optional)
UserModificationActive => 0, # (optional) 1 or 0, modified 0
TargetUserID => 2, # (optional) ID of the user for which the modified setting is meant,
# leave it undef for global changes.
ExclusiveLockGUID => $LockingString, # the GUID used to locking the setting
UserID => 1, # (required) UserID
NoValidation => 1, # (optional) no value type validation.
);
Returns:
%Result = (
Success => 1, # or false in case of an error
Error => undef, # error message
);
=cut
sub SettingUpdate {
my ( $Self, %Param ) = @_;
for my $Needed (qw(Name UserID)) {
if ( !$Param{$Needed} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $Needed!",
);
# return an empty hash, checking the attribute 'Success' gives undef, which is false
return;
}
}
if ( !$Param{TargetUserID} && !$Param{ExclusiveLockGUID} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need TargetUserID or ExclusiveLockGUID!",
);
}
my $SysConfigDBObject = $Kernel::OM->Get('Kernel::System::SysConfig::DB');
# Get default setting
my %Setting = $SysConfigDBObject->DefaultSettingGet(
Name => $Param{Name},
);
# Make sure that required settings can't be disabled.
if ( $Setting{IsRequired} ) {
$Param{IsValid} = 1;
}
# Return if setting does not exists.
if ( !%Setting ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Setting $Param{Name} does not exist!",
);
my %Result = (
Success => 0,
Error => $Kernel::OM->Get('Kernel::Language')->Translate(
"Setting %s does not exists!",
$Param{Name},
),
);
return %Result;
}
# Default should be locked (for global updates).
my $LockedByUser;
if ( !$Param{TargetUserID} ) {
$LockedByUser = $SysConfigDBObject->DefaultSettingIsLockedByUser(
DefaultID => $Setting{DefaultID},
ExclusiveLockUserID => $Param{UserID},
ExclusiveLockGUID => $Param{ExclusiveLockGUID},
);
if ( !$LockedByUser ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Setting $Param{Name} is not locked to this user!",
);
my %Result = (
Success => 0,
Error => $Kernel::OM->Get('Kernel::Language')->Translate(
"Setting %s is not locked to this user!",
$Param{Name},
),
);
return %Result;
}
}
# Do not perform EffectiveValueCheck if user wants to disable the setting.
if ( $Param{IsValid} ) {
# Effective value must match in structure to the default and individual values should be
# valid according to its value types.
my %EffectiveValueCheck = $Self->SettingEffectiveValueCheck(
XMLContentParsed => $Setting{XMLContentParsed},
EffectiveValue => $Param{EffectiveValue},
NoValidation => $Param{NoValidation} //= 0,
UserID => $Param{UserID},
);
if ( !$EffectiveValueCheck{Success} ) {
my $Error = $EffectiveValueCheck{Error} || 'Unknown error!';
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "EffectiveValue is invalid! $Error",
);
my %Result = (
Success => 0,
Error => $Kernel::OM->Get('Kernel::Language')->Translate(
"Setting value is not valid!",
),
);
return %Result;
}
}
# Get modified setting (if any).
my %ModifiedSetting = $SysConfigDBObject->ModifiedSettingGet(
Name => $Param{Name},
IsGlobal => 1,
);
if ( !defined $Param{EffectiveValue} ) {
# In the case that we want only to enable/disable setting,
# old effective value will be preserved.
$Param{EffectiveValue} = $ModifiedSetting{EffectiveValue} // $Setting{EffectiveValue};
}
my $UserModificationActive = $Param{UserModificationActive} //= $Setting{UserModificationActive};
if ( $Param{TargetUserID} ) {
if ( IsHashRefWithData( \%ModifiedSetting ) ) {
# override default setting with global modified setting
%Setting = (
%Setting,
%ModifiedSetting,
);
}
%ModifiedSetting = $SysConfigDBObject->ModifiedSettingGet(
Name => $Param{Name},
TargetUserID => $Param{TargetUserID},
);
$UserModificationActive = undef; # prevent setting this value
my %GlobalSetting = $Self->SettingGet(
Name => $Param{Name},
OverriddenInXML => 1,
UserID => 1,
);
$Setting{EffectiveValue} = $GlobalSetting{EffectiveValue};
}
# Add new modified setting (if there wasn't).
if ( !%ModifiedSetting ) {
# Check if provided EffectiveValue is same as in Default
my $IsDifferent = DataIsDifferent(
Data1 => \$Setting{EffectiveValue},
Data2 => \$Param{EffectiveValue},
) || 0;
if ( defined $Param{IsValid} ) {
$IsDifferent ||= $Setting{IsValid} != $Param{IsValid};
}
$IsDifferent ||= $Setting{UserModificationActive} != $Param{UserModificationActive};
if ($IsDifferent) {
my $ModifiedID = $SysConfigDBObject->ModifiedSettingAdd(
DefaultID => $Setting{DefaultID},
Name => $Setting{Name},
IsValid => $Param{IsValid} //= $Setting{IsValid},
EffectiveValue => $Param{EffectiveValue},
UserModificationActive => $UserModificationActive,
TargetUserID => $Param{TargetUserID},
ExclusiveLockGUID => $Param{ExclusiveLockGUID},
UserID => $Param{UserID},
);
if ( !$ModifiedID ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Could not add modified setting!",
);
my %Result = (
Success => 0,
Error => $Kernel::OM->Get('Kernel::Language')->Translate(
"Could not add modified setting!",
),
);
return %Result;
}
}
}
else {
# Check if provided EffectiveValue is same as in last modified EffectiveValue
my $IsDifferent = DataIsDifferent(
Data1 => \$ModifiedSetting{EffectiveValue},
Data2 => \$Param{EffectiveValue},
) || 0;
if ( defined $Param{IsValid} ) {
$IsDifferent ||= $ModifiedSetting{IsValid} != $Param{IsValid};
}
$IsDifferent ||= $ModifiedSetting{UserModificationActive} != $Param{UserModificationActive};
if ($IsDifferent) {
my %ModifiedSettingVersion = $SysConfigDBObject->ModifiedSettingVersionGetLast(
Name => $ModifiedSetting{Name},
);
my $EffectiveValueModifiedSinceDeployment = 1;
if ( $ModifiedSettingVersion{ModifiedVersionID} ) {
my %ModifiedSettingLastDeployed = $SysConfigDBObject->ModifiedSettingVersionGet(
ModifiedVersionID => $ModifiedSettingVersion{ModifiedVersionID},
);
$EffectiveValueModifiedSinceDeployment = DataIsDifferent(
Data1 => \$ModifiedSettingLastDeployed{EffectiveValue},
Data2 => \$Param{EffectiveValue},
) || 0;
if ( defined $Param{IsValid} ) {
$EffectiveValueModifiedSinceDeployment ||= $ModifiedSettingLastDeployed{IsValid} != $Param{IsValid};
}
$EffectiveValueModifiedSinceDeployment
||= $ModifiedSettingLastDeployed{UserModificationActive} != $Param{UserModificationActive};
}
elsif ( !IsHashRefWithData( \%ModifiedSettingVersion ) ) {
$EffectiveValueModifiedSinceDeployment = DataIsDifferent(
Data1 => \$Setting{EffectiveValue},
Data2 => \$Param{EffectiveValue},
) || 0;
if ( defined $Param{IsValid} ) {
$EffectiveValueModifiedSinceDeployment ||= $Setting{IsValid} != $Param{IsValid};
}
$EffectiveValueModifiedSinceDeployment
||= $Setting{UserModificationActive} != $Param{UserModificationActive};
}
# Update the existing modified setting.
my $Success = $SysConfigDBObject->ModifiedSettingUpdate(
ModifiedID => $ModifiedSetting{ModifiedID},
DefaultID => $Setting{DefaultID},
Name => $Setting{Name},
IsValid => $Param{IsValid} //= $ModifiedSetting{IsValid},
EffectiveValue => $Param{EffectiveValue},
UserModificationActive => $UserModificationActive,
TargetUserID => $Param{TargetUserID} //= $ModifiedSetting{TargetUserID},
ExclusiveLockGUID => $Param{ExclusiveLockGUID},
UserID => $Param{UserID},
IsDirty => $EffectiveValueModifiedSinceDeployment ? 1 : 0,
);
if ( !$Success ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Could not update modified setting!",
);
my %Result = (
Success => 0,
Error => $Kernel::OM->Get('Kernel::Language')->Translate(
"Could not update modified setting!",
),
);
return %Result;
}
}
}
# When a setting is set to invalid all modified settings for users has to be removed.
if (
!$Param{IsValid}
&& !$Param{TargetUserID}
&& $Self->can('UserSettingValueDelete') # OTOBO Community Solution
)
{
$Self->UserSettingValueDelete(
Name => $Setting{Name},
ModifiedID => 'All',
UserID => $Param{UserID},
);
}
if ( !$Param{TargetUserID} ) {
# Unlock setting so it can be locked again afterwards.
my $Success = $SysConfigDBObject->DefaultSettingUnlock(
DefaultID => $Setting{DefaultID},
);
if ( !$Success ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Setting could not be unlocked!",
);
my %Result = (
Success => 0,
Error => $Kernel::OM->Get('Kernel::Language')->Translate(
"Setting could not be unlocked!",
),
);
return %Result;
}
}
my %Result = (
Success => 1,
);
return %Result;
}
=head2 SettingLock()
Lock setting(s) to the particular user.
my $ExclusiveLockGUID = $SysConfigObject->SettingLock(
DefaultID => 1, # the ID of the setting that needs to be locked
# or
Name => 'SettingName', # the Name of the setting that needs to be locked
# or
LockAll => 1, # system locks all settings
Force => 1, # (optional) Force locking (do not check if it's already locked by another user). Default: 0.
UserID => 1, # (required)
);
Returns:
$ExclusiveLockGUID = 'azzHab72wIlAXDrxHexsI5aENsESxAO7'; # Setting locked
or
$ExclusiveLockGUID = undef; # Not locked
=cut
sub SettingLock {
my ( $Self, %Param ) = @_;
return $Kernel::OM->Get('Kernel::System::SysConfig::DB')->DefaultSettingLock(%Param);
}
=head2 SettingUnlock()
Unlock particular or all Setting(s).
my $Success = $SysConfigObject->SettingUnlock(
DefaultID => 1, # the ID of the setting that needs to be unlocked
# or
Name => 'SettingName', # the name of the setting that needs to be locked
# or
UnlockAll => 1, # unlock all settings
);
Returns:
$Success = 1;
=cut
sub SettingUnlock {
my ( $Self, %Param ) = @_;
return $Kernel::OM->Get('Kernel::System::SysConfig::DB')->DefaultSettingUnlock(%Param);
}
=head2 SettingLockCheck()
Check setting lock status.
my %Result = $SysConfigObject->SettingLockCheck(
DefaultID => 1, # the ID of the setting that needs to be checked
ExclusiveLockGUID => 1, # lock GUID
ExclusiveLockUserID => 1, # UserID
);
Returns:
%Result = (
Locked => 1, # lock status;
# 0 - unlocked
# 1 - locked to another user
# 2 - locked to provided user
User => { # User data, provided only if Locked = 1
UserLogin => ...,
UserFirstname => ...,
UserLastname => ...,
...
},
);
=cut
sub SettingLockCheck {
my ( $Self, %Param ) = @_;
for my $Needed (qw(DefaultID ExclusiveLockGUID ExclusiveLockUserID)) {
if ( !$Param{$Needed} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $Needed!",
);
return;
}
}
my $SysConfigDBObject = $Kernel::OM->Get('Kernel::System::SysConfig::DB');
my %Result = (
Locked => 0,
);
my $LockedByUser = $SysConfigDBObject->DefaultSettingIsLockedByUser(
DefaultID => $Param{DefaultID},
ExclusiveLockUserID => $Param{ExclusiveLockUserID},
ExclusiveLockGUID => $Param{ExclusiveLockGUID},
);
if ($LockedByUser) {
# setting locked to the provided user
$Result{Locked} = 2;
}
else {
# check if setting is locked to another user
my $UserID = $SysConfigDBObject->DefaultSettingIsLocked(
DefaultID => $Param{DefaultID},
GetLockUserID => 1,
);
if ($UserID) {
# get user data
$Result{Locked} = 1;
my %User = $Kernel::OM->Get('Kernel::System::User')->GetUserData(
UserID => $UserID,
);
$Result{User} = \%User;
}
}
return %Result;
}
=head2 SettingEffectiveValueGet()
Calculate effective value for a given parsed XML structure.
my $Result = $SysConfigObject->SettingEffectiveValueGet(
Translate => 1, # (optional) Translate translatable strings. Default 0.
Value => [ # (required) parsed XML structure
{
'Item' => [
{
'ValueType' => 'String',
'Content' => '3600',
'ValueRegex' => ''
},
],
},
Objects => {
Select => { ... },
PerlModule => { ... },
# ...
}
];
);
Returns:
$Result = '3600';
=cut
sub SettingEffectiveValueGet {
my ( $Self, %Param ) = @_;
for my $Needed (qw(Value)) {
if ( !$Param{$Needed} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $Needed!",
);
return;
}
}
my %ForbiddenValueTypes = %{ $Self->{ForbiddenValueTypes} || {} };
if ( !%ForbiddenValueTypes ) {
%ForbiddenValueTypes = $Self->ForbiddenValueTypesGet();
$Self->{ForbiddenValueTypes} = \%ForbiddenValueTypes;
}
$Param{Translate} //= 0;
my $Result;
my %Objects;
if ( $Param{Objects} ) {
%Objects = %{ $Param{Objects} };
}
# Make sure structure is correct.
return $Result if !IsArrayRefWithData( $Param{Value} );
return $Result if !IsHashRefWithData( $Param{Value}->[0] );
my %Attributes;
if ( $Param{Value}->[0]->{Item} ) {
# Make sure structure is correct.
return $Result if !IsArrayRefWithData( $Param{Value}->[0]->{Item} );
return $Result if !IsHashRefWithData( $Param{Value}->[0]->{Item}->[0] );
# Set default ValueType.
my $ValueType = "String";
if ( $Param{Value}->[0]->{Item}->[0]->{ValueType} ) {
$ValueType = $Param{Value}->[0]->{Item}->[0]->{ValueType};
}
if ( !$Objects{$ValueType} ) {
# Make sure value type backend is available and syntactically correct.
my $Loaded = $Kernel::OM->Get('Kernel::System::Main')->Require(
"Kernel::System::SysConfig::ValueType::$ValueType",
);
return $Result if !$Loaded;
$Objects{$ValueType} = $Kernel::OM->Get(
"Kernel::System::SysConfig::ValueType::$ValueType",
);
}
# Create a local clone of the value to prevent any modification.
my $Value = $Kernel::OM->Get('Kernel::System::Storable')->Clone(
Data => $Param{Value}->[0]->{Item},
);
$Result = $Objects{$ValueType}->EffectiveValueGet(
Value => $Value,
Translate => $Param{Translate},
);
}
elsif ( $Param{Value}->[0]->{Hash} ) {
# Make sure structure is correct.
return {} if !IsArrayRefWithData( $Param{Value}->[0]->{Hash} );
return {} if !IsHashRefWithData( $Param{Value}->[0]->{Hash}->[0] );
return {} if !IsArrayRefWithData( $Param{Value}->[0]->{Hash}->[0]->{Item} );
# Check for additional attributes in the DefaultItem.
if (
$Param{Value}->[0]->{Hash}->[0]->{DefaultItem}
&& ref $Param{Value}->[0]->{Hash}->[0]->{DefaultItem} eq 'ARRAY'
&& scalar $Param{Value}->[0]->{Hash}->[0]->{DefaultItem}
&& ref $Param{Value}->[0]->{Hash}->[0]->{DefaultItem}->[0] eq 'HASH'
)
{
%Attributes = ();
my @ValueAttributeList = $Self->ValueAttributeList();
ATTRIBUTE:
for my $Attribute ( sort keys %{ $Param{Value}->[0]->{Hash}->[0]->{DefaultItem}->[0] } ) {