1# --
2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
3# --
4# This software comes with ABSOLUTELY NO WARRANTY. For details, see
5# the enclosed file COPYING for license information (GPL). If you
6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
7# --
8
9package Kernel::System::SysConfig::ValueType::File;
10
11use strict;
12use warnings;
13
14use Kernel::System::VariableCheck qw(:all);
15
16use parent qw(Kernel::System::SysConfig::BaseValueType);
17
18our @ObjectDependencies = (
19    'Kernel::System::Log',
20);
21
22=head1 NAME
23
24Kernel::System::SysConfig::ValueType::File - System configuration file value type backed.
25
26=head1 PUBLIC INTERFACE
27
28=head2 new()
29
30Create an object. Do not use it directly, instead use:
31
32    use Kernel::System::ObjectManager;
33    local $Kernel::OM = Kernel::System::ObjectManager->new();
34    my $ValueTypeObject = $Kernel::OM->Get('Kernel::System::SysConfig::ValueType::File');
35
36=cut
37
38sub new {
39    my ( $Type, %Param ) = @_;
40
41    # Allocate new hash for object.
42    my $Self = {};
43    bless( $Self, $Type );
44
45    return $Self;
46}
47
48=head2 SettingEffectiveValueCheck()
49
50Check if provided EffectiveValue matches structure defined in XMLContentParsed.
51
52    my %Result = $ValueTypeObject->SettingEffectiveValueCheck(
53        XMLContentParsed => {
54            Value => [
55                {
56                    'Item' => [
57                        {
58                            'Content'       => '/etc/hosts',
59                            'ValueType'     => 'File',
60                        },
61                    ],
62                },
63            ],
64        },
65        EffectiveValue => '/etc/hosts',
66    );
67
68Result:
69    %Result = (
70        EffectiveValue => '/etc/hosts',   # Note for File ValueTypes EffectiveValue is not changed.
71        Success => 1,
72        Error   => undef,
73    );
74
75=cut
76
77sub SettingEffectiveValueCheck {
78    my ( $Self, %Param ) = @_;
79
80    for my $Needed (qw(XMLContentParsed)) {
81        if ( !$Param{$Needed} ) {
82            $Kernel::OM->Get('Kernel::System::Log')->Log(
83                Priority => 'error',
84                Message  => "Need $Needed!"
85            );
86
87            return;
88        }
89    }
90
91    my %Result = (
92        Success => 0,
93    );
94
95    my $Value = $Param{XMLContentParsed}->{Value};
96
97    for my $Parameter ( sort keys %{ $Param{Parameters} } ) {
98        if ( !defined $Value->[0]->{Item}->[0]->{$Parameter} ) {
99            $Value->[0]->{Item}->[0]->{$Parameter} = $Param{Parameters}->{$Parameter};
100        }
101    }
102
103    # Data should be scalar.
104    if ( ref $Param{EffectiveValue} ) {
105        $Result{Error} = 'EffectiveValue for File must be a scalar!';
106        return %Result;
107    }
108
109    if ( !-e $Param{EffectiveValue} ) {
110        $Result{Error} = "$Param{EffectiveValue} not exists!";
111        return %Result;
112    }
113
114    my $IsDirectory = -d $Param{EffectiveValue};
115
116    if ($IsDirectory) {
117        $Result{Error} = "$Param{EffectiveValue} is directory, but file is expected!";
118        return %Result;
119    }
120
121    $Result{Success}        = 1;
122    $Result{EffectiveValue} = $Param{EffectiveValue};
123
124    return %Result;
125}
126
1271;
128
129=head1 TERMS AND CONDITIONS
130
131This software is part of the OTRS project (L<https://otrs.org/>).
132
133This software comes with ABSOLUTELY NO WARRANTY. For details, see
134the enclosed file COPYING for license information (GPL). If you
135did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
136
137=cut
138