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::Password;
10## nofilter(TidyAll::Plugin::OTRS::Perl::LayoutObject)
11
12use strict;
13use warnings;
14
15use Kernel::System::VariableCheck qw(:all);
16
17use parent qw(Kernel::System::SysConfig::BaseValueType);
18
19our @ObjectDependencies = (
20    'Kernel::Language',
21    'Kernel::Output::HTML::Layout',
22    'Kernel::System::Log',
23);
24
25=head1 NAME
26
27Kernel::System::SysConfig::ValueType::Password - System configuration password value type backed.
28
29=head1 PUBLIC INTERFACE
30
31=head2 new()
32
33Create an object. Do not use it directly, instead use:
34
35    use Kernel::System::ObjectManager;
36    local $Kernel::OM = Kernel::System::ObjectManager->new();
37    my $ValueTypeObject = $Kernel::OM->Get('Kernel::System::SysConfig::ValueType::Password');
38
39=cut
40
41sub new {
42    my ( $Type, %Param ) = @_;
43
44    # Allocate new hash for object.
45    my $Self = {};
46    bless( $Self, $Type );
47
48    return $Self;
49}
50
51=head2 SettingRender()
52
53Extracts the effective value from a XML parsed setting.
54
55    my $SettingHTML = $ValueTypeObject->SettingRender(
56        Name           => 'SettingName',
57        EffectiveValue => 'Product 6',      # (optional)
58        DefaultValue   => 'Product 5',      # (optional)
59        Class          => 'My class'        # (optional)
60        Item           => [                 # (optional) XML parsed item
61            {
62                'ValueType' => 'Password',
63                'Content' => 'Secret',
64            },
65        ],
66        RW => 1,                            # (optional) Allow editing. Default 0.
67        IsArray => 1,                       # (optional) Item is part of the array
68        IsHash  => 1,                       # (optional) Item is part of the hash
69        IDSuffix => 1,                      # (optional) Suffix will be added to the element ID
70        SkipEffectiveValueCheck => 1,       # (optional) If enabled, system will not perform effective value check.
71                                            #            Default: 1.
72    );
73
74Returns:
75
76    $SettingHTML = '<div class "Field"...</div>';
77
78=cut
79
80sub SettingRender {
81    my ( $Self, %Param ) = @_;
82
83    if ( !defined $Param{Name} ) {
84        $Kernel::OM->Get('Kernel::System::Log')->Log(
85            Priority => 'error',
86            Message  => "Need Name",
87        );
88        return;
89    }
90
91    my $IDSuffix = $Param{IDSuffix} || '';
92
93    my $EffectiveValue = $Param{EffectiveValue};
94    if (
95        !defined $EffectiveValue
96        && $Param{Item}
97        && $Param{Item}->[0]->{Content}
98        )
99    {
100        $EffectiveValue = $Param{Item}->[0]->{Content};
101    }
102
103    $Param{Class}        //= '';
104    $Param{DefaultValue} //= '';
105
106    my $DefaultValueStrg = $Kernel::OM->Get('Kernel::Language')->Translate('Default');
107
108    my %EffectiveValueCheck = (
109        Success => 1,
110    );
111
112    if ( !$Param{SkipEffectiveValueCheck} ) {
113        %EffectiveValueCheck = $Self->SettingEffectiveValueCheck(
114            EffectiveValue   => $EffectiveValue,
115            XMLContentParsed => {
116                Value => [
117                    {
118                        Item => $Param{Item},
119                    },
120                ],
121            },
122        );
123    }
124
125    my $HTML = "<div class='SettingContent'>\n";
126    $HTML .= "<input class=\"$Param{Class}\" type=\"password\" name=\"$Param{Name}\" id=\"$Param{Name}$IDSuffix\"";
127
128    my $HTMLValue = $Kernel::OM->Get('Kernel::Output::HTML::Layout')->Ascii2Html(
129        Text => $EffectiveValue,
130        Type => 'Normal',
131    );
132
133    $HTML .= "\" value=\"$HTMLValue\" ";
134
135    if ( !$Param{RW} ) {
136        $HTML .= "disabled='disabled' ";
137    }
138
139    $HTML .= " />\n";
140
141    if ( !$EffectiveValueCheck{Success} ) {
142        my $Message = $Kernel::OM->Get('Kernel::Language')
143            ->Translate("Value is not correct! Please, consider updating this field.");
144
145        $HTML .= $Param{IsValid} ? "<div class='BadEffectiveValue'>\n" : "<div>\n";
146        $HTML .= "<p>* $Message</p>\n";
147        $HTML .= "</div>\n";
148    }
149
150    $HTML .= "</div>\n";
151
152    return $HTML;
153}
154
155=head2 AddItem()
156
157Generate HTML for new array/hash item.
158
159    my $HTML = $ValueTypeObject->AddItem(
160        Name           => 'SettingName',    (required) Name
161        DefaultItem    => {                 (optional) DefaultItem hash, if available
162            Item => {
163                Content => 'Value',
164            },
165        },
166    );
167
168Returns:
169
170    $HTML = "<input type='password' id='Setting_ExampleArray'
171        value='Value' name='ExampleArray' class='Entry'/>";
172
173=cut
174
175sub AddItem {
176    my ( $Self, %Param ) = @_;
177
178    # Check needed stuff.
179    for my $Needed (qw(Name)) {
180        if ( !$Param{$Needed} ) {
181            $Kernel::OM->Get('Kernel::System::Log')->Log(
182                Priority => 'error',
183                Message  => "Need $Needed!",
184            );
185            return;
186        }
187    }
188
189    my $IDSuffix = $Param{IDSuffix} || '';
190    my $Class    = $Param{Class}    || '';
191
192    my $DefaultValue = '';
193    if ( $Param{DefaultItem} && $Param{DefaultItem}->{Item} ) {
194        $DefaultValue = $Param{DefaultItem} && $Param{DefaultItem}->{Item}->{Content} || '';
195    }
196    elsif ( $Param{DefaultItem} ) {
197        $DefaultValue = $Param{DefaultItem} && $Param{DefaultItem}->{Content} || '';
198    }
199
200    my $RemoveThisEntry = $Kernel::OM->Get('Kernel::Language')->Translate("Remove this entry");
201
202    my $Result = "<input type='password' id='$Param{Name}$IDSuffix'
203        value='$DefaultValue' name='$Param{Name}' class='$Class Entry'/>";
204
205    return $Result;
206}
207
2081;
209
210=head1 TERMS AND CONDITIONS
211
212This software is part of the OTRS project (L<https://otrs.org/>).
213
214This software comes with ABSOLUTELY NO WARRANTY. For details, see
215the enclosed file COPYING for license information (GPL). If you
216did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
217
218=cut
219