From 99601196d8e8438871fd5a814a0cd802c6fd5d68 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Fri, 23 May 2014 15:53:46 +0200 Subject: [PATCH] Dispatcher: Pro-Request-Initialisierung in eigene Sub verschoben MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Weiterhin optionale Initialisierung von Client und User in besagter Sub. Erleichert die Verwendung die Initialisierung vom Dispatcher in eigenen Scripten (z.B. der console oder rose_auto_generate_models.pl, auch wenn die noch nicht umgestellt sind), weil dann nicht in jedem Script der Initialiserungspfad nachgebaut werden muss. Beispiel ($client_id_or_name und $login können z.B. vorher aus einer Konfigurationsdatei gelesen werden): use SL::Dispatcher; use SL::DB::Customer; our $dispatcher = SL::Dispatcher->new('CGI'); $dispatcher->pre_startup; $dispatcher->pre_request_initialization( client => $client_id_or_name, login => $login, ); print join("\n", map { $_->id . ":" . $_->name } @{ SL::DB::Manager::Customer->get_all(limit => 5) }), "\n"; --- SL/Dispatcher.pm | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/SL/Dispatcher.pm b/SL/Dispatcher.pm index f9ad09a87..854b863a3 100644 --- a/SL/Dispatcher.pm +++ b/SL/Dispatcher.pm @@ -81,6 +81,38 @@ sub pre_request_checks { } } +sub pre_request_initialization { + my ($self, %params) = @_; + + $self->unrequire_bin_mozilla; + + $::locale = Locale->new($::lx_office_conf{system}->{language}); + $::form = Form->new; + $::instance_conf = SL::InstanceConfiguration->new; + $::request = SL::Request->new( + cgi => CGI->new({}), + layout => SL::Layout::None->new, + ); + + my $session_result = $::auth->restore_session; + $::auth->create_or_refresh_session; + + if ($params{client}) { + $::auth->set_client($params{client}) || die("cannot find client " . $params{client}); + + if ($params{login}) { + die "cannot find user " . $params{login} unless %::myconfig = $::auth->read_user(login => $params{login}); + die "cannot find locale for user " . $params{login} unless $::locale = Locale->new($::myconfig{countrycode}); + + $::form->{login} = $params{login}; # normaly implicit at login + + $::instance_conf->init; + } + } + + return $session_result; +} + sub render_error_ajax { my ($error) = @_; @@ -199,18 +231,7 @@ sub handle_request { my ($script, $path, $suffix, $script_name, $action, $routing_type); - $self->unrequire_bin_mozilla; - - $::locale = Locale->new($::lx_office_conf{system}->{language}); - $::form = Form->new; - $::instance_conf = SL::InstanceConfiguration->new; - $::request = SL::Request->new( - cgi => CGI->new({}), - layout => SL::Layout::None->new, - ); - - my $session_result = $::auth->restore_session; - $::auth->create_or_refresh_session; + my $session_result = $self->pre_request_initialization; $::form->read_cgi_input; -- 2.20.1