+#
+# private methods
+#
+
+sub _redirect_to_main_script {
+ my ($self) = @_;
+
+ return $self->redirect_to($::form->{callback}) if $::form->{callback};
+
+ $self->redirect_to(controller => "login.pl", action => 'company_logo');
+}
+
+sub _redirect_to_main_script_if_already_logged_in {
+ my ($self) = @_;
+
+ # Get 'login' from valid session.
+ my $login = $::auth->get_session_value('login');
+ return unless $login;
+
+ # See whether or not the user exists in the database.
+ my %user = $::auth->read_user(login => $login);
+ return if ($user{login} || '') ne $login;
+
+ # Check if there's a client set in the session -- and whether or not
+ # the user still has access to the client.
+ my $client_id = $::auth->get_session_value('client_id');
+ return if !$client_id;
+
+ if (!$::auth->set_client($client_id)) {
+ $::auth->punish_wrong_login;
+ $::auth->destroy_session;
+ $::auth->create_or_refresh_session;
+ $self->show_login_form(error => t8('Incorrect username or password or no access to selected client!'));
+ return 1;
+ }
+
+ # Check if the session is logged in correctly.
+ return if SL::Auth::OK() != $::auth->authenticate($login, undef);
+
+ $::auth->create_or_refresh_session;
+ $::auth->delete_session_value('FLASH');
+
+ $self->_redirect_to_main_script(\%user);
+
+ return 1;
+}
+
+sub error_state {
+ my %states = (
+ session => { warning => t8('The session has expired. Please log in again.') },
+ password => { error => t8('Incorrect username or password or no access to selected client!') },
+ action => { warning => t8('The action is missing or invalid.') },
+ );
+
+ return %{ $states{$_[0]} || {} };
+}
+
+sub set_layout {
+ $::request->{layout} = SL::Layout::Dispatcher->new(style => 'login');
+}
+
+sub init_clients {
+ return SL::DB::Manager::AuthClient->get_all_sorted;
+}
+
+sub init_default_client_id {
+ my ($self) = @_;
+ my $default_client = first { $_->is_default } @{ $self->clients };
+ return $default_client ? $default_client->id : undef;
+}
+
+sub show_login_form {
+ my ($self, %params) = @_;
+
+ $self->render('login_screen/user_login', %params, version => $::form->read_version);
+}
+