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;
15 use Rose::Object::MakeMethods::Generic (
16 'scalar --get_set_init' => [ qw(clients default_client_id) ],
19 __PACKAGE__->run_before('set_layout');
25 sub action_user_login {
28 # If the user is already logged in then redirect to the proper menu
30 return if $self->_redirect_to_main_script_if_already_logged_in;
32 # Otherwise show the login form.
33 $self->render('login_screen/user_login', error => error_state($::form->{error}));
39 $::auth->destroy_session;
40 $::auth->create_or_refresh_session;
41 $self->render('login_screen/user_login', error => $::locale->text('You are logged out!'));
47 my $login = $::form->{'{AUTH}login'} || $::auth->get_session_value('login');
48 %::myconfig = $login ? $::auth->read_user(login => $login) : ();
49 SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
50 $::form->{login} = $::myconfig{login};
51 $::locale = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
52 my $user = User->new(login => $::myconfig{login});
53 $::request->{layout} = SL::Layout::Dispatcher->new(style => $user->{menustyle});
55 # if we get an error back, bale out
56 my $result = $user->login($::form);
58 # Database update available?
59 ::end_of_request() if -2 == $result;
61 # Auth DB needs update? If so log the user out forcefully.
63 $::auth->destroy_session;
64 return $self->render('login_screen/auth_db_needs_update');
69 $::auth->punish_wrong_login;
70 return $self->render('login_screen/user_login', error => $::locale->text('Incorrect username or password!'));
74 $::auth->set_cookie_environment_variable();
76 $self->_redirect_to_main_script($user);
86 sub keep_auth_vars_in_form {
94 sub _redirect_to_main_script {
95 my ($self, $user) = @_;
97 return $self->redirect_to($::form->{callback}) if $::form->{callback};
99 $self->redirect_to(controller => "login.pl", action => 'company_logo');
102 sub _redirect_to_main_script_if_already_logged_in {
105 # Get 'login' from valid session.
106 my $login = $::auth->get_session_value('login');
107 return unless $login;
109 # See whether or not the user exists in the database.
110 my %user = $::auth->read_user(login => $login);
111 return if ($user{login} || '') ne $login;
113 # Check if the session is logged in correctly.
114 return if SL::Auth::OK() != $::auth->authenticate($login, undef);
116 $::auth->create_or_refresh_session;
117 $::auth->delete_session_value('FLASH');
119 $self->_redirect_to_main_script(\%user);
126 session => $::locale->text('The session is invalid or has expired.'),
127 password => $::locale->text('Incorrect password!'),
132 $::request->{layout} = SL::Layout::Dispatcher->new(style => 'login');
136 return SL::DB::Manager::AuthClient->get_all_sorted;
139 sub init_default_client_id {
141 my $default_client = first { $_->is_default } @{ $self->clients };
142 return $default_client ? $default_client->id : undef;