Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / Controller / LoginScreen.pm
1 package SL::Controller::LoginScreen;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use List::Util qw(first);
8
9 use SL::Dispatcher::AuthHandler::User;
10 use SL::DB::AuthClient;
11 use SL::DB::AuthGroup;
12 use SL::DB::AuthUser;
13 use SL::DB::Employee;
14 use SL::Locale::String qw(t8);
15 use SL::User;
16 use SL::Version;
17
18 use Rose::Object::MakeMethods::Generic (
19   'scalar --get_set_init' => [ qw(clients default_client_id) ],
20 );
21
22 __PACKAGE__->run_before('set_layout');
23
24 #
25 # actions
26 #
27
28 sub action_user_login {
29   my ($self) = @_;
30
31   # If the user is already logged in then redirect to the proper menu
32   # script.
33   return if $self->_redirect_to_main_script_if_already_logged_in;
34
35   # Otherwise show the login form.
36   $self->show_login_form(error_state($::form->{error}));
37 }
38
39 sub action_logout {
40   my ($self) = @_;
41
42   $::auth->destroy_session;
43   $::auth->create_or_refresh_session;
44   $self->show_login_form(info => $::locale->text('You are logged out!'));
45 }
46
47 sub action_login {
48   my ($self) = @_;
49
50   my $login     = $::form->{'{AUTH}login'}     || $::auth->get_session_value('login');
51   my $client_id = $::form->{'{AUTH}client_id'} || $::auth->get_session_value('client_id');
52   my $error     = t8('Incorrect username or password or no access to selected client!');
53
54   if (!$::auth->set_client($client_id)) {
55     $::auth->punish_wrong_login;
56     return $self->show_login_form(error => $error);
57   }
58
59   %::myconfig      = $login ? $::auth->read_user(login => $login) : ();
60   $::locale        = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
61   my $auth_result  = SL::Dispatcher::AuthHandler::User->new->handle(callback => $::form->{callback});
62
63   $::dispatcher->end_request unless $auth_result;
64
65   $::request->layout(SL::Layout::Dispatcher->new(style => $::myconfig{menustyle}));
66
67   # if we get an error back, bale out
68   my $result = User->new(login => $::myconfig{login})->login($::form);
69
70   # Auth DB needs update? If so log the user out forcefully.
71   if (User::LOGIN_AUTH_DBUPDATE_AVAILABLE() == $result) {
72     $::auth->destroy_session;
73     # must be without layout because menu rights might not exist yet
74     return $self->render('login_screen/auth_db_needs_update', { layout => 0 });
75   }
76
77   # Basic client tables available? If not tell the user to create them
78   # and log the user out forcefully.
79   if (User::LOGIN_BASIC_TABLES_MISSING() == $result) {
80     $::auth->destroy_session;
81     return $self->render('login_screen/basic_tables_missing');
82   }
83
84   # Database update available?
85   $::dispatcher->end_request if User::LOGIN_DBUPDATE_AVAILABLE() == $result;
86
87   # Other login errors.
88   if (User::LOGIN_OK() != $result) {
89     $::auth->punish_wrong_login;
90     return $self->show_login_form(error => $error);
91   }
92
93   # Everything is fine.
94   $::auth->set_cookie_environment_variable();
95
96   $self->_redirect_to_main_script;
97 }
98
99 #
100 # settings
101 #
102 sub get_auth_level {
103   return 'none';
104 }
105
106 sub keep_auth_vars_in_form {
107   return 1;
108 }
109
110 #
111 # private methods
112 #
113
114 sub _redirect_to_main_script {
115   my ($self) = @_;
116
117   return $self->redirect_to($::form->{callback}) if $::form->{callback};
118
119   $self->redirect_to(controller => "login.pl", action => 'company_logo');
120 }
121
122 sub _redirect_to_main_script_if_already_logged_in {
123   my ($self) = @_;
124
125   # Get 'login' from valid session.
126   my $login = $::auth->get_session_value('login');
127   return unless $login;
128
129   # See whether or not the user exists in the database.
130   my %user = $::auth->read_user(login => $login);
131   return if ($user{login} || '') ne $login;
132
133   # Check if there's a client set in the session -- and whether or not
134   # the user still has access to the client.
135   my $client_id = $::auth->get_session_value('client_id');
136   return if !$client_id;
137
138   if (!$::auth->set_client($client_id)) {
139     $::auth->punish_wrong_login;
140     $::auth->destroy_session;
141     $::auth->create_or_refresh_session;
142     $self->show_login_form(error => t8('Incorrect username or password or no access to selected client!'));
143     return 1;
144   }
145
146   # Check if the session is logged in correctly.
147   return if SL::Auth::OK() != $::auth->authenticate($login, undef);
148
149   $::auth->create_or_refresh_session;
150   $::auth->delete_session_value('FLASH');
151
152   $self->_redirect_to_main_script(\%user);
153
154   return 1;
155 }
156
157 sub error_state {
158   my %states = (
159     session  => { warning => t8('The session has expired. Please log in again.')                   },
160     password => { error   => t8('Incorrect username or password or no access to selected client!') },
161     action   => { warning => t8('The action is missing or invalid.')                               },
162   );
163
164   return %{ $states{$_[0]} || {} };
165 }
166
167 sub set_layout {
168   $::request->{layout} = $::request->is_mobile
169     ? SL::Layout::Dispatcher->new(style => 'mobile_login')
170     : SL::Layout::Dispatcher->new(style => 'login');
171 }
172
173 sub init_clients {
174   return SL::DB::Manager::AuthClient->get_all_sorted;
175 }
176
177 sub init_default_client_id {
178   my ($self)         = @_;
179   my $default_client = first { $_->is_default } @{ $self->clients };
180   return $default_client ? $default_client->id : undef;
181 }
182
183 sub show_login_form {
184   my ($self, %params) = @_;
185
186   $self->render('login_screen/user_login', %params, version => SL::Version->get_version, callback => $::form->{callback});
187 }
188
189 1;