1#!/usr/local/bin/perl
2# --
3# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
4# --
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see https://www.gnu.org/licenses/gpl-3.0.txt.
17# --
18
19use strict;
20use warnings;
21
22# use ../../ as lib location
23use FindBin qw($Bin);
24use lib "$Bin/../..";
25use lib "$Bin/../../Kernel/cpan-lib";
26use lib "$Bin/../../Custom";
27
28use SOAP::Transport::HTTP;
29use Kernel::System::ObjectManager;
30
31SOAP::Transport::HTTP::CGI->dispatch_to('Core')->handle();
32
33package Core;
34
35sub new {
36    my $Self = shift;
37
38    my $Class = ref($Self) || $Self;
39    bless {} => $Class;
40
41    return $Self;
42}
43
44sub Dispatch {
45    my ( $Self, $User, $Pw, $Object, $Method, %Param ) = @_;
46
47    $User ||= '';
48    $Pw   ||= '';
49    local $Kernel::OM = Kernel::System::ObjectManager->new(
50        'Kernel::System::Log' => {
51            LogPrefix => 'OTRS-RPC',
52        },
53    );
54
55    my %CommonObject;
56
57    $CommonObject{ConfigObject}          = $Kernel::OM->Get('Kernel::Config');
58    $CommonObject{CustomerCompanyObject} = $Kernel::OM->Get('Kernel::System::CustomerCompany');
59    $CommonObject{CustomerUserObject}    = $Kernel::OM->Get('Kernel::System::CustomerUser');
60    $CommonObject{EncodeObject}          = $Kernel::OM->Get('Kernel::System::Encode');
61    $CommonObject{GroupObject}           = $Kernel::OM->Get('Kernel::System::Group');
62    $CommonObject{LinkObject}            = $Kernel::OM->Get('Kernel::System::LinkObject');
63    $CommonObject{LogObject}             = $Kernel::OM->Get('Kernel::System::Log');
64    $CommonObject{PIDObject}             = $Kernel::OM->Get('Kernel::System::PID');
65    $CommonObject{QueueObject}           = $Kernel::OM->Get('Kernel::System::Queue');
66    $CommonObject{SessionObject}         = $Kernel::OM->Get('Kernel::System::AuthSession');
67    $CommonObject{TicketObject}          = $Kernel::OM->Get('Kernel::System::Ticket');
68
69    # We want to keep providing the TimeObject as legacy API for now.
70    ## nofilter(TidyAll::Plugin::OTRS::Migrations::OTRS6::TimeObject)
71    $CommonObject{TimeObject} = $Kernel::OM->Get('Kernel::System::Time');
72    $CommonObject{UserObject} = $Kernel::OM->Get('Kernel::System::User');
73
74    my $RequiredUser     = $CommonObject{ConfigObject}->Get('SOAP::User');
75    my $RequiredPassword = $CommonObject{ConfigObject}->Get('SOAP::Password');
76
77    if (
78        !defined $RequiredUser
79        || !length $RequiredUser
80        || !defined $RequiredPassword || !length $RequiredPassword
81        )
82    {
83        $CommonObject{LogObject}->Log(
84            Priority => 'notice',
85            Message  => "SOAP::User or SOAP::Password is empty, SOAP access denied!",
86        );
87        return;
88    }
89
90    if ( $User ne $RequiredUser || $Pw ne $RequiredPassword ) {
91        $CommonObject{LogObject}->Log(
92            Priority => 'notice',
93            Message  => "Auth for user $User (pw $Pw) failed!",
94        );
95        return;
96    }
97
98    if ( !$CommonObject{$Object} ) {
99        $CommonObject{LogObject}->Log(
100            Priority => 'error',
101            Message  => "No such Object $Object!",
102        );
103        return "No such Object $Object!";
104    }
105
106    return $CommonObject{$Object}->$Method(%Param);
107}
108
109=item DispatchMultipleTicketMethods()
110
111to dispatch multiple ticket methods and get the TicketID
112
113    my $TicketID = $RPC->DispatchMultipleTicketMethods(
114        $SOAP_User,
115        $SOAP_Pass,
116        'TicketObject',
117        [ { Method => 'TicketCreate', Parameter => \%TicketData }, { Method => 'ArticleCreate', Parameter => \%ArticleData } ],
118    );
119
120=cut
121
122sub DispatchMultipleTicketMethods {
123    my ( $Self, $User, $Pw, $Object, $MethodParamArrayRef ) = @_;
124
125    $User ||= '';
126    $Pw   ||= '';
127
128    # common objects
129    local $Kernel::OM = Kernel::System::ObjectManager->new(
130        'Kernel::System::Log' => {
131            LogPrefix => 'OTRS-RPC',
132        },
133    );
134
135    my %CommonObject;
136
137    $CommonObject{ConfigObject}          = $Kernel::OM->Get('Kernel::Config');
138    $CommonObject{CustomerCompanyObject} = $Kernel::OM->Get('Kernel::System::CustomerCompany');
139    $CommonObject{CustomerUserObject}    = $Kernel::OM->Get('Kernel::System::CustomerUser');
140    $CommonObject{EncodeObject}          = $Kernel::OM->Get('Kernel::System::Encode');
141    $CommonObject{GroupObject}           = $Kernel::OM->Get('Kernel::System::Group');
142    $CommonObject{LinkObject}            = $Kernel::OM->Get('Kernel::System::LinkObject');
143    $CommonObject{LogObject}             = $Kernel::OM->Get('Kernel::System::Log');
144    $CommonObject{PIDObject}             = $Kernel::OM->Get('Kernel::System::PID');
145    $CommonObject{QueueObject}           = $Kernel::OM->Get('Kernel::System::Queue');
146    $CommonObject{SessionObject}         = $Kernel::OM->Get('Kernel::System::AuthSession');
147    $CommonObject{TicketObject}          = $Kernel::OM->Get('Kernel::System::Ticket');
148    $CommonObject{TimeObject}            = $Kernel::OM->Get('Kernel::System::Time');
149    $CommonObject{UserObject}            = $Kernel::OM->Get('Kernel::System::User');
150
151    my $RequiredUser     = $CommonObject{ConfigObject}->Get('SOAP::User');
152    my $RequiredPassword = $CommonObject{ConfigObject}->Get('SOAP::Password');
153
154    if (
155        !defined $RequiredUser
156        || !length $RequiredUser
157        || !defined $RequiredPassword || !length $RequiredPassword
158        )
159    {
160        $CommonObject{LogObject}->Log(
161            Priority => 'notice',
162            Message  => "SOAP::User or SOAP::Password is empty, SOAP access denied!",
163        );
164        return;
165    }
166
167    if ( $User ne $RequiredUser || $Pw ne $RequiredPassword ) {
168        $CommonObject{LogObject}->Log(
169            Priority => 'notice',
170            Message  => "Auth for user $User (pw $Pw) failed!",
171        );
172        return;
173    }
174
175    if ( !$CommonObject{$Object} ) {
176        $CommonObject{LogObject}->Log(
177            Priority => 'error',
178            Message  => "No such Object $Object!",
179        );
180        return "No such Object $Object!";
181    }
182
183    my $TicketID;
184    my $Counter;
185
186    for my $MethodParamEntry ( @{$MethodParamArrayRef} ) {
187
188        my $Method    = $MethodParamEntry->{Method};
189        my %Parameter = %{ $MethodParamEntry->{Parameter} };
190
191        # push ticket id to params if there is no ticket id
192        if ( !$Parameter{TicketID} && $TicketID ) {
193            $Parameter{TicketID} = $TicketID;
194        }
195
196        my $ReturnValue = $CommonObject{$Object}->$Method(%Parameter);
197
198        # remember ticket id if method was TicketCreate
199        if ( !$Counter && $Object eq 'TicketObject' && $Method eq 'TicketCreate' ) {
200            $TicketID = $ReturnValue;
201        }
202
203        $Counter++;
204    }
205
206    return $TicketID;
207}
208
2091;
210