1 package SL::Controller::LoginScreen;
 
   5 use parent qw(SL::Controller::Base);
 
   7 use List::Util qw(first);
 
   9 use SL::Dispatcher::AuthHandler::User;
 
  10 use SL::DB::AuthClient;
 
  11 use SL::DB::AuthGroup;
 
  14 use SL::Locale::String qw(t8);
 
  17 use Rose::Object::MakeMethods::Generic (
 
  18   'scalar --get_set_init' => [ qw(clients default_client_id) ],
 
  21 __PACKAGE__->run_before('set_layout');
 
  27 sub action_user_login {
 
  30   # If the user is already logged in then redirect to the proper menu
 
  32   return if $self->_redirect_to_main_script_if_already_logged_in;
 
  34   # Otherwise show the login form.
 
  35   $self->show_login_form(error_state($::form->{error}));
 
  41   $::auth->destroy_session;
 
  42   $::auth->create_or_refresh_session;
 
  43   $self->show_login_form(info => $::locale->text('You are logged out!'));
 
  49   my $login     = $::form->{'{AUTH}login'}     || $::auth->get_session_value('login');
 
  50   my $client_id = $::form->{'{AUTH}client_id'} || $::auth->get_session_value('client_id');
 
  51   my $error     = t8('Incorrect username or password or no access to selected client!');
 
  53   if (!$::auth->set_client($client_id)) {
 
  54     $::auth->punish_wrong_login;
 
  55     return $self->show_login_form(error => $error);
 
  58   %::myconfig      = $login ? $::auth->read_user(login => $login) : ();
 
  59   $::locale        = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
 
  60   SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
 
  62   $::request->layout(SL::Layout::Dispatcher->new(style => $::myconfig{menustyle}));
 
  64   # if we get an error back, bale out
 
  65   my $result = User->new(login => $::myconfig{login})->login($::form);
 
  67   # Auth DB needs update? If so log the user out forcefully.
 
  68   if (User::LOGIN_AUTH_DBUPDATE_AVAILABLE() == $result) {
 
  69     $::auth->destroy_session;
 
  70     # must be without layout because menu rights might not exist yet
 
  71     return $self->render('login_screen/auth_db_needs_update', { layout => 0 });
 
  74   # Basic client tables available? If not tell the user to create them
 
  75   # and log the user out forcefully.
 
  76   if (User::LOGIN_BASIC_TABLES_MISSING() == $result) {
 
  77     $::auth->destroy_session;
 
  78     return $self->render('login_screen/basic_tables_missing');
 
  81   # Database update available?
 
  82   ::end_of_request() if User::LOGIN_DBUPDATE_AVAILABLE() == $result;
 
  85   if (User::LOGIN_OK() != $result) {
 
  86     $::auth->punish_wrong_login;
 
  87     return $self->show_login_form(error => $error);
 
  91   $::auth->set_cookie_environment_variable();
 
  93   $self->_redirect_to_main_script;
 
 103 sub keep_auth_vars_in_form {
 
 111 sub _redirect_to_main_script {
 
 114   return $self->redirect_to($::form->{callback}) if $::form->{callback};
 
 116   $self->redirect_to(controller => "login.pl", action => 'company_logo');
 
 119 sub _redirect_to_main_script_if_already_logged_in {
 
 122   # Get 'login' from valid session.
 
 123   my $login = $::auth->get_session_value('login');
 
 124   return unless $login;
 
 126   # See whether or not the user exists in the database.
 
 127   my %user = $::auth->read_user(login => $login);
 
 128   return if ($user{login} || '') ne $login;
 
 130   # Check if there's a client set in the session -- and whether or not
 
 131   # the user still has access to the client.
 
 132   my $client_id = $::auth->get_session_value('client_id');
 
 133   return if !$client_id;
 
 135   if (!$::auth->set_client($client_id)) {
 
 136     $::auth->punish_wrong_login;
 
 137     $::auth->destroy_session;
 
 138     $::auth->create_or_refresh_session;
 
 139     $self->show_login_form(error => t8('Incorrect username or password or no access to selected client!'));
 
 143   # Check if the session is logged in correctly.
 
 144   return if SL::Auth::OK() != $::auth->authenticate($login, undef);
 
 146   $::auth->create_or_refresh_session;
 
 147   $::auth->delete_session_value('FLASH');
 
 149   $self->_redirect_to_main_script(\%user);
 
 156     session  => { warning => t8('The session has expired. Please log in again.')                   },
 
 157     password => { error   => t8('Incorrect username or password or no access to selected client!') },
 
 158     action   => { warning => t8('The action is missing or invalid.')                               },
 
 161   return %{ $states{$_[0]} || {} };
 
 165   $::request->{layout} = SL::Layout::Dispatcher->new(style => 'login');
 
 169   return SL::DB::Manager::AuthClient->get_all_sorted;
 
 172 sub init_default_client_id {
 
 174   my $default_client = first { $_->is_default } @{ $self->clients };
 
 175   return $default_client ? $default_client->id : undef;
 
 178 sub show_login_form {
 
 179   my ($self, %params) = @_;
 
 181   $self->render('login_screen/user_login', %params, version => $::form->read_version);