1 package SL::Controller::LoginScreen;
 
   5 use parent qw(SL::Controller::Base);
 
   7 use SL::Dispatcher::AuthHandler::User;
 
  10 __PACKAGE__->run_before('set_layout');
 
  15 sub action_user_login {
 
  18   # If the user is already logged in then redirect to the proper menu
 
  20   return if $self->_redirect_to_main_script_if_already_logged_in;
 
  22   # Otherwise show the login form.
 
  23   $self->render('login_screen/user_login', error => error_state($::form->{error}));
 
  29   $::auth->destroy_session;
 
  30   $::auth->create_or_refresh_session;
 
  31   $self->render('login_screen/user_login', error => $::locale->text('You are logged out!'));
 
  37   my $login        = $::form->{'{AUTH}login'} || $::auth->get_session_value('login');
 
  38   %::myconfig      = $login ? $::auth->read_user(login => $login) : ();
 
  39   SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
 
  40   $::form->{login} = $::myconfig{login};
 
  41   $::locale        = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
 
  42   my $user         = User->new(login => $::myconfig{login});
 
  43   $::request->{layout} = SL::Layout::Dispatcher->new(style => $user->{menustyle});
 
  45   # if we get an error back, bale out
 
  46   my $result = $user->login($::form);
 
  48   # Database update available?
 
  49   ::end_of_request() if -2 == $result;
 
  51   # Auth DB needs update? If so log the user out forcefully.
 
  53     $::auth->destroy_session;
 
  54     return $self->render('login_screen/auth_db_needs_update');
 
  59     $::auth->punish_wrong_login;
 
  60     return $self->render('login_screen/user_login', error => $::locale->text('Incorrect username or password!'));
 
  64   $::auth->set_cookie_environment_variable();
 
  66   $self->_redirect_to_main_script($user);
 
  76 sub keep_auth_vars_in_form {
 
  84 sub _redirect_to_main_script {
 
  85   my ($self, $user) = @_;
 
  87   return $self->redirect_to($::form->{callback}) if $::form->{callback};
 
  89   $self->redirect_to(controller => "login.pl", action => 'company_logo');
 
  92 sub _redirect_to_main_script_if_already_logged_in {
 
  95   # Get 'login' from valid session.
 
  96   my $login = $::auth->get_session_value('login');
 
  99   # See whether or not the user exists in the database.
 
 100   my %user = $::auth->read_user(login => $login);
 
 101   return if ($user{login} || '') ne $login;
 
 103   # Check if the session is logged in correctly.
 
 104   return if SL::Auth::OK() != $::auth->authenticate($login, undef);
 
 106   $::auth->create_or_refresh_session;
 
 107   $::auth->delete_session_value('FLASH');
 
 109   $self->_redirect_to_main_script(\%user);
 
 116     session  => $::locale->text('The session is invalid or has expired.'),
 
 117     password => $::locale->text('Incorrect password!'),
 
 122   $::request->{layout} = SL::Layout::Dispatcher->new(style => 'login');