File: Update.pm

package info (click to toggle)
otrs2 6.0.32-6
  • links: PTS
  • area: non-free
  • in suites: bullseye
  • size: 197,336 kB
  • sloc: perl: 1,003,018; javascript: 75,060; xml: 70,883; php: 51,819; sql: 22,361; sh: 379; makefile: 51
file content (259 lines) | stat: -rw-r--r-- 7,341 bytes parent folder | download
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
# --
# 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::System::Console::Command::Admin::Config::Update;

use strict;
use warnings;

use Kernel::System::VariableCheck qw( IsHashRefWithData );

use parent qw(Kernel::System::Console::BaseCommand);

our @ObjectDependencies = (
    'Kernel::System::Main',
    'Kernel::System::SysConfig',
    'Kernel::System::YAML',
);

sub Configure {
    my ( $Self, %Param ) = @_;

    $Self->Description('Update the value of a setting.');
    $Self->AddOption(
        Name        => 'setting-name',
        Description => "The name of the setting.",
        Required    => 1,
        HasValue    => 1,
        ValueRegex  => qr/.*/,
    );
    $Self->AddOption(
        Name        => 'source-path',
        Description => "Specify the source location of the setting value YAML file.",
        Required    => 0,
        HasValue    => 1,
        ValueRegex  => qr/.*/smx,
    );
    $Self->AddOption(
        Name        => 'value',
        Description => "Specify single line setting value.",
        Required    => 0,
        HasValue    => 1,
        ValueRegex  => qr/.*/smx,
    );
    $Self->AddOption(
        Name        => 'valid',
        Description => "Specify validity of the setting ( 0 or 1 ).",
        Required    => 0,
        HasValue    => 1,
        ValueRegex  => qr/0|1/smx,
    );
    $Self->AddOption(
        Name        => 'reset',
        Description => "Reset setting to default value.",
        Required    => 0,
        HasValue    => 0,
    );
    $Self->AddOption(
        Name        => 'no-deploy',
        Description => "Specify that the update of this setting should not be deployed.",
        Required    => 0,
        HasValue    => 0,
        ValueRegex  => qr/.*/smx,
    );

    return;
}

sub PreRun {
    my ( $Self, %Param ) = @_;

    # Perform any custom validations here. Command execution can be stopped with die().

    my %Setting = $Kernel::OM->Get('Kernel::System::SysConfig')->SettingGet(
        Name    => $Self->GetOption('setting-name'),
        Default => 1,
    );

    if ( !%Setting ) {
        die "setting-name is invalid!";
    }

    return if $Self->GetOption('reset');
    return if defined $Self->GetOption('valid');

    my $SourcePath = $Self->GetOption('source-path');

    my $Value = $Self->GetOption('value');

    if ( $SourcePath && $Value ) {
        die "source-path or value is required but not both!";
    }

    if ( !$SourcePath && !defined $Value ) {
        die "source-path or value is required!";
    }

    if ( $SourcePath && !-e $SourcePath ) {
        die "File $SourcePath does not exists!";
    }

    return;
}

sub Run {
    my ( $Self, %Param ) = @_;

    my $SettingReset = $Self->GetOption('reset');
    my $SettingValid = $Self->GetOption('valid');

    if ($SettingReset) {
        $Self->Print("<yellow>Resetting setting value...</yellow>\n\n");
    }
    elsif ( defined $SettingValid ) {
        $Self->Print("<yellow>Updating setting valid state...</yellow>\n\n");
    }
    else {
        $Self->Print("<yellow>Updating setting value...</yellow>\n\n");
    }

    my $SourcePath = $Self->GetOption('source-path');

    my $EffectiveValue = $Self->GetOption('value');

    if ($SourcePath) {

        my $YAMLContentRef = $Kernel::OM->Get('Kernel::System::Main')->FileRead(
            Location        => $SourcePath,
            Mode            => 'utf8',
            Type            => 'Local',
            Result          => 'SCALAR',
            DisableWarnings => 1,
        );

        if ( !$YAMLContentRef ) {
            $Self->PrintError("Could not read $SourcePath!");
            return $Self->ExitCodeError();
        }

        $EffectiveValue = $Kernel::OM->Get('Kernel::System::YAML')->Load(
            Data => ${$YAMLContentRef},
        );

        if ( !defined $EffectiveValue ) {
            $Self->PrintError("The content of $SourcePath is invalid");
            return $Self->ExitCodeError();
        }
    }

    my $SysConfigObject = $Kernel::OM->Get('Kernel::System::SysConfig');

    my $SettingName = $Self->GetOption('setting-name');

    # Get default setting.
    my %Setting = $SysConfigObject->SettingGet(
        Name    => $SettingName,
        Default => 1,
    );

    if ( !IsHashRefWithData( \%Setting ) ) {
        $Self->PrintError("Setting doesn't exists!");
        return $Self->ExitCodeError();
    }

    my $ExclusiveLockGUID = $SysConfigObject->SettingLock(
        UserID    => 1,
        Force     => 1,
        DefaultID => $Setting{DefaultID},
    );

    my $Success;
    if ($SettingReset) {
        $Success = $SysConfigObject->SettingReset(
            Name              => $SettingName,
            TargetUserID      => 1,
            ExclusiveLockGUID => $ExclusiveLockGUID,
            UserID            => 1,
        );

        if ( !$Success ) {
            $Self->PrintError("Setting could not be resetted!");
            return $Self->ExitCodeError();
        }
    }
    elsif ( defined $SettingValid ) {

        # Get current setting value.
        my %Setting = $SysConfigObject->SettingGet(
            Name => $SettingName,
        );

        # Update setting with modified 'IsValid' param.
        $Success = $SysConfigObject->SettingUpdate(
            Name              => $SettingName,
            IsValid           => $SettingValid,
            EffectiveValue    => $Setting{EffectiveValue},
            ExclusiveLockGUID => $ExclusiveLockGUID,
            UserID            => 1,
        );
        if ( !$Success ) {
            $Self->PrintError("Setting valid state could not be updated!");
            return $Self->ExitCodeError();
        }
    }
    else {
        $Success = $SysConfigObject->SettingUpdate(
            Name              => $SettingName,
            EffectiveValue    => $EffectiveValue,
            ExclusiveLockGUID => $ExclusiveLockGUID,
            UserID            => 1,
        );

        if ( !$Success ) {
            $Self->PrintError("Setting could not be updated!");
            return $Self->ExitCodeError();
        }
    }

    $Success = $SysConfigObject->SettingUnlock(
        UserID    => 1,
        DefaultID => $Setting{DefaultID},
    );

    if ( $Self->GetOption('no-deploy') ) {
        $Self->Print("<green>Done.</green>\n");
        return $Self->ExitCodeOk();
    }

    my %DeploymentResult = $SysConfigObject->ConfigurationDeploy(
        Comments      => "Admin::Config::Update $SettingName",
        UserID        => 1,
        Force         => 1,
        DirtySettings => [$SettingName],
    );

    if ( !$DeploymentResult{Success} ) {
        $Self->PrintError("Deployment failed!\n");
        return $Self->ExitCodeError();
    }

    $Self->Print("<green>Done.</green>\n");
    return $Self->ExitCodeOk();
}

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