Form::_dbconnect_options nach SL::DBConnect::get_options verschoben
[kivitendo-erp.git] / SL / Controller / LoginScreen.pm
index f561a39..13b7b8a 100644 (file)
@@ -7,6 +7,7 @@ use parent qw(SL::Controller::Base);
 use SL::Dispatcher::AuthHandler::User;
 use SL::User;
 
+__PACKAGE__->run_before('set_layout');
 #
 # actions
 #
@@ -14,7 +15,12 @@ use SL::User;
 sub action_user_login {
   my ($self) = @_;
 
-  $self->render('login_screen/user_login');
+  # If the user is already logged in then redirect to the proper menu
+  # script.
+  return if $self->_redirect_to_main_script_if_already_logged_in;
+
+  # Otherwise show the login form.
+  $self->render('login_screen/user_login', error => error_state($::form->{error}));
 }
 
 sub action_logout {
@@ -28,11 +34,13 @@ sub action_logout {
 sub action_login {
   my ($self) = @_;
 
-  %::myconfig      = $::form->{'{AUTH}login'} ? $::auth->read_user(login => $::form->{'{AUTH}login'}) : ();
-  %::myconfig      = SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
+  my $login        = $::form->{'{AUTH}login'} || $::auth->get_session_value('login');
+  %::myconfig      = $login ? $::auth->read_user(login => $login) : ();
+  SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
   $::form->{login} = $::myconfig{login};
   $::locale        = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
   my $user         = User->new(login => $::myconfig{login});
+  $::request->{layout} = SL::Layout::Dispatcher->new(style => $user->{menustyle});
 
   # if we get an error back, bale out
   my $result = $user->login($::form);
@@ -55,17 +63,7 @@ sub action_login {
   # Everything is fine.
   $::auth->set_cookie_environment_variable();
 
-  return $self->redirect_to($::form->{callback}) if $::form->{callback};
-
-  my %style_to_script_map = (
-    v3  => 'v3',
-    neu => 'new',
-    v4  => 'v4',
-  );
-
-  my $menu_script = $style_to_script_map{$user->{menustyle}} || '';
-
-  $self->redirect_to(controller => "menu${menu_script}.pl", action => 'display');
+  $self->_redirect_to_main_script($user);
 }
 
 #
@@ -79,4 +77,49 @@ sub keep_auth_vars_in_form {
   return 1;
 }
 
+#
+# private methods
+#
+
+sub _redirect_to_main_script {
+  my ($self, $user) = @_;
+
+  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 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 {
+  return {
+    session  => $::locale->text('The session is invalid or has expired.'),
+    password => $::locale->text('Incorrect password!'),
+  }->{$_[0]};
+}
+
+sub set_layout {
+  $::request->{layout} = SL::Layout::Dispatcher->new(style => 'login');
+}
+
 1;