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;
13 use SL::Locale::String qw(t8);
16 use Rose::Object::MakeMethods::Generic (
17 'scalar --get_set_init' => [ qw(clients default_client_id) ],
20 __PACKAGE__->run_before('set_layout');
26 sub action_user_login {
29 # If the user is already logged in then redirect to the proper menu
31 return if $self->_redirect_to_main_script_if_already_logged_in;
33 # Otherwise show the login form.
34 $self->show_login_form(error => error_state($::form->{error}));
40 $::auth->destroy_session;
41 $::auth->create_or_refresh_session;
42 $self->show_login_form(error => $::locale->text('You are logged out!'));
48 my $login = $::form->{'{AUTH}login'} || $::auth->get_session_value('login');
49 my $client_id = $::form->{'{AUTH}client_id'} || $::auth->get_session_value('client_id');
50 my $error = t8('Incorrect username or password or no access to selected client!');
52 if (!$::auth->set_client($client_id)) {
53 $::auth->punish_wrong_login;
54 return $self->show_login_form(error => $error);
57 %::myconfig = $login ? $::auth->read_user(login => $login) : ();
58 $::form->{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 # Database update available?
68 ::end_of_request() if -2 == $result;
70 # Auth DB needs update? If so log the user out forcefully.
72 $::auth->destroy_session;
73 return $self->render('login_screen/auth_db_needs_update');
78 $::auth->punish_wrong_login;
79 return $self->show_login_form(error => $error);
83 $::auth->set_cookie_environment_variable();
85 # TODO: Employees anlegen/checken
86 # $self->_ensure_employees_for_authorized_users_exist;
88 $self->_redirect_to_main_script;
98 sub keep_auth_vars_in_form {
106 sub _redirect_to_main_script {
109 return $self->redirect_to($::form->{callback}) if $::form->{callback};
111 $self->redirect_to(controller => "login.pl", action => 'company_logo');
114 sub _redirect_to_main_script_if_already_logged_in {
117 # Get 'login' from valid session.
118 my $login = $::auth->get_session_value('login');
119 return unless $login;
121 # See whether or not the user exists in the database.
122 my %user = $::auth->read_user(login => $login);
123 return if ($user{login} || '') ne $login;
125 # Check if the session is logged in correctly.
126 return if SL::Auth::OK() != $::auth->authenticate($login, undef);
128 $::auth->create_or_refresh_session;
129 $::auth->delete_session_value('FLASH');
131 $self->_redirect_to_main_script(\%user);
138 session => $::locale->text('The session is invalid or has expired.'),
139 password => $::locale->text('Incorrect username or password or no access to selected client!'),
144 $::request->{layout} = SL::Layout::Dispatcher->new(style => 'login');
148 return SL::DB::Manager::AuthClient->get_all_sorted;
151 sub init_default_client_id {
153 my $default_client = first { $_->is_default } @{ $self->clients };
154 return $default_client ? $default_client->id : undef;
157 sub show_login_form {
158 my ($self, %params) = @_;
160 $::request->layout->focus('#auth_login');
161 $self->render('login_screen/user_login', %params);