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->show_login_form(error => error_state($::form->{error}));
39 $::auth->destroy_session;
40 $::auth->create_or_refresh_session;
41 $self->show_login_form(error => $::locale->text('You are logged out!'));
47 my $login = $::form->{'{AUTH}login'} || $::auth->get_session_value('login');
48 my $client_id = $::form->{'{AUTH}client_id'} || $::auth->get_session_value('client_id');
49 my $error = t8('Incorrect username or password or no access to selected client!');
51 if (!$::auth->set_client($client_id)) {
52 $::auth->punish_wrong_login;
53 return $self->show_login_form(error => $error);
56 %::myconfig = $login ? $::auth->read_user(login => $login) : ();
57 SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
58 $::form->{login} = $::myconfig{login};
59 $::locale = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
60 my $user = User->new(login => $::myconfig{login});
61 $::request->{layout} = SL::Layout::Dispatcher->new(style => $user->{menustyle});
63 # if we get an error back, bale out
64 my $result = $user->login($::form);
66 # Database update available?
67 ::end_of_request() if -2 == $result;
69 # Auth DB needs update? If so log the user out forcefully.
71 $::auth->destroy_session;
72 return $self->render('login_screen/auth_db_needs_update');
77 $::auth->punish_wrong_login;
78 return $self->show_login_form(error => $error);
82 $::auth->set_cookie_environment_variable();
84 # TODO: Employees anlegen/checken
85 # $self->_ensure_employees_for_authorized_users_exist;
87 $self->_redirect_to_main_script($user);
97 sub keep_auth_vars_in_form {
105 sub _redirect_to_main_script {
106 my ($self, $user) = @_;
108 return $self->redirect_to($::form->{callback}) if $::form->{callback};
110 $self->redirect_to(controller => "login.pl", action => 'company_logo');
113 sub _redirect_to_main_script_if_already_logged_in {
116 # Get 'login' from valid session.
117 my $login = $::auth->get_session_value('login');
118 return unless $login;
120 # See whether or not the user exists in the database.
121 my %user = $::auth->read_user(login => $login);
122 return if ($user{login} || '') ne $login;
124 # Check if the session is logged in correctly.
125 return if SL::Auth::OK() != $::auth->authenticate($login, undef);
127 $::auth->create_or_refresh_session;
128 $::auth->delete_session_value('FLASH');
130 $self->_redirect_to_main_script(\%user);
137 session => $::locale->text('The session is invalid or has expired.'),
138 password => $::locale->text('Incorrect password!'),
143 $::request->{layout} = SL::Layout::Dispatcher->new(style => 'login');
147 return SL::DB::Manager::AuthClient->get_all_sorted;
150 sub init_default_client_id {
152 my $default_client = first { $_->is_default } @{ $self->clients };
153 return $default_client ? $default_client->id : undef;
156 sub show_login_form {
157 my ($self, %params) = @_;
159 $::request->layout->focus('#auth_login');
160 $self->render('login_screen/user_login', %params);